I have an extension where customer attributes are created (magento 1.9.4). except for the validation of the passed variable, the extension works fine. in the controller there is a regex-test. Unfortunately, i have no idea about regular expressions. here is the problem (saveAction):
public function saveAction() {
$data = $this->getRequest()->getPost();
if ($data) {
/** @var $session Mage_Admin_Model_Session */
$session = Mage::getSingleton('adminhtml/session');
$redirectBack = $this->getRequest()->getParam('back', false);
/* @var $model Mage_Catalog_Model_Entity_Attribute */
$model = Mage::getModel('customerattribute/customerattribute');
/* @var $helper Mage_Catalog_Helper_Product */
$helper = Mage::helper('customerattribute');
$id = $this->getRequest()->getParam('attribute_id');
//validate attribute_code
if (isset($data['attribute_code'])) {
// Dudit 2019 Validierung hat Fehler
if (version_compare(Mage::getVersion(), '1.4.2', '>=')) {
$validatorAttrCode = new Zend_Validate_Regex(array('pattern' => '/^[a-z_]{1,255}$/'));
if (!$validatorAttrCode->isValid($data['attribute_code'])) {
$session->addError(
$helper->__('Attribute code is invalid. Please use only letters (a-z), numbers (0-9) or underscore(_) in this field, first character should be a letter.'));
$this->_redirect('*/*/edit', array('attribute_id' => $id, '_current' => true));
return;
}
} else {
if (!preg_match('/^[a-z_]{1,255}$/', $data['attribute_code'])) {
$session->addError(
$helper->__('Attribute code is invalid. Please use only letters (a-z), numbers (0-9) or underscore(_) in this field, first character should be a letter.'));
$this->_redirect('*/*/edit', array('attribute_id' => $id, '_current' => true));
return;
}
}
}
//validate frontend_input
if (isset($data['frontend_input'])) {
if (version_compare(Mage::getVersion(), '1.5.1', '>=')) {
/** @var $validatorInputType Mage_Eav_Model_Adminhtml_System_Config_Source_Inputtype_Validator */
$validatorInputType = Mage::getModel('eav/adminhtml_system_config_source_inputtype_validator');
if (!$validatorInputType->isValid($data['frontend_input'])) {
foreach ($validatorInputType->getMessages() as $message) {
$session->addError($message);
}
$this->_redirect('*/*/edit', array('attribute_id' => $id, '_current' => true));
return;
}
} else {
if (!in_array($data['frontend_input'], array('text', 'textarea', 'date', 'boolean', 'multiselect', 'select'))) {
$session->addError('Error1');
$this->_redirect('*/*/edit', array('attribute_id' => $id, '_current' => true));
return;
}
}
}
if ($id) {
$model->load($id);
if (!$model->getId()) {
$session->addError(
Mage::helper('customerattribute')->__('This Attribute no longer exists'));
$this->_redirect('*/*/');
return;
}
// entity type check
if ($model->getEntityTypeId() != $this->_entityTypeId) {
$session->addError(
Mage::helper('customerattribute')->__('This attribute cannot be updated.'));
$session->setAttributeData($data);
$this->_redirect('*/*/');
return;
}
$data['attribute_code'] = $model->getAttributeCode();
$data['is_user_defined'] = $model->getIsUserDefined();
$data['frontend_input'] = $model->getFrontendInput();
} else {
/**
* @todo add to helper and specify all relations for properties
*/
$data['source_model'] = $helper->getAttributeSourceModelByInputType($data['frontend_input']);
$data['backend_model'] = $helper->getAttributeBackendModelByInputType($data['frontend_input']);
}
if (!isset($data['is_configurable'])) {
$data['is_configurable'] = 0;
}
if (!isset($data['is_filterable'])) {
$data['is_filterable'] = 0;
}
if (!isset($data['is_filterable_in_search'])) {
$data['is_filterable_in_search'] = 0;
}
if (is_null($model->getIsUserDefined()) || $model->getIsUserDefined() != 0) {
$data['backend_type'] = $model->getBackendTypeByInput($data['frontend_input']);
}
$defaultValueField = $model->getDefaultValueByInput($data['frontend_input']);
if ($defaultValueField) {
$data['default_value'] = $this->getRequest()->getParam($defaultValueField);
}
if (!isset($data['apply_to'])) {
$data['apply_to'] = array();
}
//filter
$data = $this->_filterPostData($data);
$model->addData($data);
if (!$id) {
$model->setEntityTypeId($this->_entityTypeId);
$model->setIsUserDefined(1);
}
if ($this->getRequest()->getParam('set') && $this->getRequest()->getParam('group')) {
// For creating product attribute on product page we need specify attribute set and group
$model->setAttributeSetId($this->getRequest()->getParam('set'));
$model->setAttributeGroupId($this->getRequest()->getParam('group'));
}
try {
$model->save();
$used_in_forms = $this->getRequest()->getParam('used_in_forms');
if (version_compare(Mage::getVersion(), '1.4.2', '>=')) {
if ($model->getId() && is_array($used_in_forms)) {
Mage::getSingleton('eav/config')
->getAttribute('customer', $model->getAttributeCode())
->setData('used_in_forms', $used_in_forms)
->save();
}
} else {
if ($model->getId()) {
$connection = Mage::getSingleton('core/resource')->getConnection('core_write');
$condition = $connection->quoteInto('attribute_code = ?', $data['attribute_code']);
$table = Mage::getSingleton('core/resource')->getTableName('officustomerattribute');
$connection->delete($table, $condition);
foreach ($used_in_forms as $valueattr) {
$connection = Mage::getSingleton('core/resource')->getConnection('core_write');
$fields = array('form_code' => $valueattr, 'attribute_code' => $data['attribute_code']);
$connection->insert($table, $fields);
$connection->commit();
}
}
}
$session->addSuccess(
Mage::helper('customerattribute')->__('The product attribute has been saved.'));
/**
* Clear translation cache because attribute labels are stored in translation
*/
Mage::app()->cleanCache(array(Mage_Core_Model_Translate::CACHE_TAG));
$session->setAttributeData(false);
if ($this->getRequest()->getParam('popup')) {
$this->_redirect('adminhtml/catalog_product/addAttribute', array(
'id' => $this->getRequest()->getParam('product'),
'attribute' => $model->getId(),
'_current' => true
));
} elseif ($redirectBack) {
$this->_redirect('*/*/edit', array('attribute_id' => $model->getId(), '_current' => true));
} else {
$this->_redirect('*/*/', array());
}
return;
} catch (Exception $e) {
$session->addError($e->getMessage());
$session->setAttributeData($data);
$this->_redirect('*/*/edit', array('attribute_id' => $id, '_current' => true));
return;
}
}
$this->_redirect('*/*/');
}
if I comment out the following code, then everything works:
//validate attribute_code
if (isset($data['attribute_code'])) {
// Dudit 2019 Validierung hat Fehler
if (version_compare(Mage::getVersion(), '1.4.2', '>=')) {
$validatorAttrCode = new Zend_Validate_Regex(array('pattern' => '/^[a-z_]{1,255}$/'));
if (!$validatorAttrCode->isValid($data['attribute_code'])) {
$session->addError(
$helper->__('Attribute code is invalid. Please use only letters (a-z), numbers (0-9) or underscore(_) in this field, first character should be a letter.'));
$this->_redirect('*/*/edit', array('attribute_id' => $id, '_current' => true));
return;
}
} else {
if (!preg_match('/^[a-z_]{1,255}$/', $data['attribute_code'])) {
$session->addError(
$helper->__('Attribute code is invalid. Please use only letters (a-z), numbers (0-9) or underscore(_) in this field, first character should be a letter.'));
$this->_redirect('*/*/edit', array('attribute_id' => $id, '_current' => true));
return;
}
}
}
that's why i think there is a mistake in the regular expressions. an example of 'attribute_code':
s24_testfield
this string is valid, but still generates the error message in the regular expression
can someone help me? what's wrong here? Of course, I can do without the validation, but that would be a dirty solution.
1 Answer 1
I'll answer it myself. I do not understand the reason yet, but there is still a flag to set:
/^[a-z\_0-9]{1,255}$/m
I do not really understand the "why", but it works. The input can actually only be single-line and not multiline, but it must be due to my delimiters "^" and "$".