1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_XmlConnect_Model_Simplexml_Form_Element_Validator_Abstract
35: extends Mage_XmlConnect_Model_Simplexml_Form_Element_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_validatorTypeMessages = array();
43:
44: 45: 46: 47: 48:
49: protected $_mainNode = 'validator';
50:
51: 52: 53: 54: 55:
56: public function __construct($attributes = array())
57: {
58: parent::__construct($attributes);
59: $this->_renderer = Mage_XmlConnect_Model_Simplexml_Form::getValidatorRuleRenderer();
60: $this->_setDefaultValidatorTypeMessages();
61: if (isset($attributes['type'])) {
62: $this->setType($attributes['type']);
63: }
64: }
65:
66: 67: 68: 69: 70: 71:
72: protected function _setDefaultValidatorTypeMessages()
73: {
74: $this->_validatorTypeMessages = array(
75: 'min_length' => Mage::helper('xmlconnect')->__('Text length does not satisfy specified min text range.'),
76: 'max_length' => Mage::helper('xmlconnect')->__('Text length does not satisfy specified max text range.'),
77: 'alphanumeric' => Mage::helper('xmlconnect')->__('Please use only letters (a-z or A-Z) or numbers (0-9) only in this field. No spaces or other characters are allowed.'),
78: 'email' => Mage::helper('xmlconnect')->__('Please enter a valid email address. For example johndoe@domain.com.'),
79: 'required' => Mage::helper('xmlconnect')->__('This is a required field.'),
80: 'required_select' => Mage::helper('xmlconnect')->__('Please select an option.'),
81: 'numeric' => Mage::helper('xmlconnect')->__('Please use numbers only in this field. Please avoid spaces or other characters such as dots or commas.'),
82: 'alpha' => Mage::helper('xmlconnect')->__('Please use letters only (a-z or A-Z) in this field.'),
83: 'url' => Mage::helper('xmlconnect')->__('Please enter a valid URL. Protocol is required (http://, https:// or ftp://)'),
84: 'date' => Mage::helper('xmlconnect')->__('Please enter a valid date.'),
85: 'max_file_size' => Mage::helper('xmlconnect')->__('\'%s\' exceeds the allowed file size: %d (bytes)', $this->getFieldLabel(), $this->getValue()),
86: 'file_extensions' => Mage::helper('xmlconnect')->__('\'%s\' is not a valid file extension. Allowed extensions: %s', $this->getFieldLabel(), $this->getValue()),
87: 'max_image_width' => Mage::helper('xmlconnect')->__('\'%s\' width exceeds allowed value of %d px', $this->getFieldLabel(), $this->getValue()),
88: 'max_image_height' => Mage::helper('xmlconnect')->__('\'%s\' height exceeds allowed value of %d px', $this->getFieldLabel(), $this->getValue())
89: );
90: return $this;
91: }
92:
93: 94: 95: 96: 97: 98: 99: 100:
101: protected function _addRequiredAttributes(Mage_XmlConnect_Model_Simplexml_Element $xmlObj)
102: {
103: $this->_addId($xmlObj);
104:
105: foreach ($this->getRequiredXmlAttributes() as $attribute => $defValue) {
106: $data = $this->getData($this->_underscore($attribute));
107:
108: if ($data) {
109: $xmlObj->addAttribute($attribute, $xmlObj->xmlAttribute($data));
110: } elseif(null !== $defValue){
111: $xmlObj->addAttribute($attribute, $xmlObj->xmlAttribute($defValue));
112: } else {
113: Mage::throwException(Mage::helper('xmlconnect')->__('%s attribute is required.', $attribute));
114: }
115: }
116: $this->_addMessage($xmlObj);
117: return $this;
118: }
119:
120: 121: 122: 123: 124: 125: 126:
127: protected function _addMessage(Mage_XmlConnect_Model_Simplexml_Element $xmlObj)
128: {
129: if ($this->getMessage()) {
130: $message = $this->getMessage();
131: } elseif (array_key_exists($this->getType(), $this->getValidatorTypeMessages())) {
132: $message = $this->_validatorTypeMessages[$this->getType()];
133: } else {
134: Mage::throwException(
135: Mage::helper('xmlconnect')->__('"message" attribute is required for "%s" validator rule.', $this->getType())
136: );
137: }
138: $xmlObj->addAttribute('message', $xmlObj->xmlAttribute($message));
139: return $this;
140: }
141:
142: 143: 144: 145: 146:
147: public function getXmlAttributes()
148: {
149: return array('relation');
150: }
151:
152: 153: 154: 155: 156:
157: public function getRequiredXmlAttributes()
158: {
159: return array('type' => null);
160: }
161:
162: 163: 164: 165: 166:
167: public function getValidatorTypeMessages()
168: {
169: return $this->_validatorTypeMessages;
170: }
171:
172: 173: 174: 175: 176: 177:
178: public function addValidatorTypeMessages(array $validatorTypeMessages)
179: {
180: $this->_validatorTypeMessages = array_merge($this->_validatorTypeMessages, $validatorTypeMessages);
181: return $this;
182: }
183: }
184: