/** * Product custom layout update attribute validate function. * In case invalid data throws exception. * * @param Varien_Object $object * @throws Mage_Eav_Model_Entity_Attribute_Exception */ public function validate($object) { $attributeName = $this->getAttribute()->getName(); $updates = trim($object->getData($attributeName)); if (!$this->getAttribute()->getIsRequired() && empty($updates)) { return true; } try { new Varien_Simplexml_Element($updates); } catch (Exception $e) { $eavExc = new Mage_Eav_Model_Entity_Attribute_Exception(Mage::helper('catalog/product')->__('Please enter valid XML data.')); $eavExc->setAttributeCode($attributeName); throw $eavExc; } }
/** * Product custom layout update attribute validate function. * In case invalid data throws exception. * * @param Varien_Object $object * @throws Mage_Eav_Model_Entity_Attribute_Exception */ public function validate($object) { $attributeName = $this->getAttribute()->getName(); $xml = trim($object->getData($attributeName)); if (!$this->getAttribute()->getIsRequired() && empty($xml)) { return true; } /** @var $validator Mage_Adminhtml_Model_LayoutUpdate_Validator */ $validator = Mage::getModel('adminhtml/layoutUpdate_validator'); if (!$validator->isValid($xml)) { $messages = $validator->getMessages(); //Add first message to exception $massage = array_shift($messages); $eavExc = new Mage_Eav_Model_Entity_Attribute_Exception($massage); $eavExc->setAttributeCode($attributeName); throw $eavExc; } return true; }
public function validate($object) { $attributeName = $this->getAttribute()->getName(); $sUseConfig = $object->getData('use_config_' . $attributeName); if ($sUseConfig == '1') { return true; } $sValue = trim($object->getData($attributeName)); if ($sValue == '') { return true; } $validator = Mage::getModel('aitunits/validate_number'); $result = ctype_alpha($sValue); if ($result == false) { $message = 'Please, enter alfabetical characters only.'; $eavExc = new Mage_Eav_Model_Entity_Attribute_Exception(Mage::helper('aitunits')->__($message)); $eavExc->setAttributeCode($attributeName); throw $eavExc; } return true; }
public function validate($object) { $attributeName = $this->getAttribute()->getName(); $sUseConfig = $object->getData('use_config_' . $attributeName); if ($sUseConfig == '1') { return true; } $sValues = trim($object->getData($attributeName)); if ($sValues == '') { return true; } $sValues = str_replace(' ', '', $sValues); $validator = Mage::getModel('aitunits/validate_number_list'); $result = $validator->validate($sValues); if ($result == false) { $message = 'Please, enter correct numbers.'; $eavExc = new Mage_Eav_Model_Entity_Attribute_Exception(Mage::helper('aitunits')->__($message)); $eavExc->setAttributeCode($attributeName); throw $eavExc; } return true; }
/** * Product from date attribute validate function. * In case invalid data throws exception. * * @param Varien_Object $object * @throws Mage_Eav_Model_Entity_Attribute_Exception * @return bool */ public function validate($object) { $attr = $this->getAttribute(); $maxDate = $attr->getMaxValue(); $startDate = $this->_getValueForSave($object); if ($startDate === false) { return true; } if ($maxDate) { $date = Mage::getModel('core/date'); $value = $date->timestamp($startDate); $maxValue = $date->timestamp($maxDate); if ($value > $maxValue) { $message = Mage::helper('catalog')->__('The From Date value should be less than or equal to the To Date value.'); $eavExc = new Mage_Eav_Model_Entity_Attribute_Exception($message); $eavExc->setAttributeCode($attr->getName()); throw $eavExc; } } return true; }
/** * Walk through the attributes and run method with optional arguments * * Returns array with results for each attribute * * if $method is in format "part/method" will run method on specified part * for example: $this->walkAttributes('backend/validate'); * * @param string $method * @param array $args * @param array $part attribute, backend, frontend, source * @return array */ public function walkAttributes($partMethod, array $args = array()) { $methodArr = explode('/', $partMethod); switch (sizeof($methodArr)) { case 1: $part = 'attribute'; $method = $methodArr[0]; break; case 2: $part = $methodArr[0]; $method = $methodArr[1]; break; } $results = array(); foreach ($this->getAttributesByCode() as $attrCode => $attribute) { if (isset($args[0]) && is_object($args[0]) && !$this->_isApplicableAttribute($args[0], $attribute)) { continue; } switch ($part) { case 'attribute': $instance = $attribute; break; case 'backend': $instance = $attribute->getBackend(); break; case 'frontend': $instance = $attribute->getFrontend(); break; case 'source': $instance = $attribute->getSource(); break; } if (!$this->_isCallableAttributeInstance($instance, $method, $args)) { continue; } try { $results[$attrCode] = call_user_func_array(array($instance, $method), $args); } catch (Mage_Eav_Model_Entity_Attribute_Exception $e) { throw $e; } catch (Exception $e) { $exception = new Mage_Eav_Model_Entity_Attribute_Exception($e->getMessage()); $exception->setAttributeCode($attrCode)->setPart($part); throw $exception; } } return $results; }