code_junkie

Zend_Form: how to check 2 fields are identical

Nov 12th, 2011
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. $password = new Zend_Form_Element_Password('password', array(
  2. 'validators'=> array(
  3. 'Alnum',
  4. array('StringLength', array(6,20))
  5. ),
  6. 'filters' => array('StringTrim'),
  7. 'label' => 'Wachtwoord:'
  8. ));
  9.  
  10. $password->addFilter(new Ivo_Filters_Sha1Filter());
  11.  
  12. $password2 = new Zend_Form_Element_Password('password', array(
  13. 'validators'=> array(
  14. 'Alnum',
  15. array('StringLength', array(6,20))
  16. ),
  17. 'filters' => array('StringTrim'),
  18. 'required' => true,
  19. 'label' => 'Wachtwoord:'
  20. ));
  21. $password2->addFilter(new Ivo_Filters_Sha1Filter());
  22.  
  23. <?php
  24.  
  25. class Zend_Validate_IdenticalField extends Zend_Validate_Abstract {
  26. const NOT_MATCH = 'notMatch';
  27. const MISSING_FIELD_NAME = 'missingFieldName';
  28. const INVALID_FIELD_NAME = 'invalidFieldName';
  29.  
  30. /**
  31. * @var array
  32. */
  33. protected $_messageTemplates = array(
  34. self::MISSING_FIELD_NAME =>
  35. 'DEVELOPMENT ERROR: Field name to match against was not provided.',
  36. self::INVALID_FIELD_NAME =>
  37. 'DEVELOPMENT ERROR: The field "%fieldName%" was not provided to match against.',
  38. self::NOT_MATCH =>
  39. 'Does not match %fieldTitle%.'
  40. );
  41.  
  42. /**
  43. * @var array
  44. */
  45. protected $_messageVariables = array(
  46. 'fieldName' => '_fieldName',
  47. 'fieldTitle' => '_fieldTitle'
  48. );
  49.  
  50. /**
  51. * Name of the field as it appear in the $context array.
  52. *
  53. * @var string
  54. */
  55. protected $_fieldName;
  56.  
  57. /**
  58. * Title of the field to display in an error message.
  59. *
  60. * If evaluates to false then will be set to $this->_fieldName.
  61. *
  62. * @var string
  63. */
  64. protected $_fieldTitle;
  65.  
  66. /**
  67. * Sets validator options
  68. *
  69. * @param string $fieldName
  70. * @param string $fieldTitle
  71. * @return void
  72. */
  73. public function __construct($fieldName, $fieldTitle = null) {
  74. $this->setFieldName($fieldName);
  75. $this->setFieldTitle($fieldTitle);
  76. }
  77.  
  78. /**
  79. * Returns the field name.
  80. *
  81. * @return string
  82. */
  83. public function getFieldName() {
  84. return $this->_fieldName;
  85. }
  86.  
  87. /**
  88. * Sets the field name.
  89. *
  90. * @param string $fieldName
  91. * @return Zend_Validate_IdenticalField Provides a fluent interface
  92. */
  93. public function setFieldName($fieldName) {
  94. $this->_fieldName = $fieldName;
  95. return $this;
  96. }
  97.  
  98. /**
  99. * Returns the field title.
  100. *
  101. * @return integer
  102. */
  103. public function getFieldTitle() {
  104. return $this->_fieldTitle;
  105. }
  106.  
  107. /**
  108. * Sets the field title.
  109. *
  110. * @param string:null $fieldTitle
  111. * @return Zend_Validate_IdenticalField Provides a fluent interface
  112. */
  113. public function setFieldTitle($fieldTitle = null) {
  114. $this->_fieldTitle = $fieldTitle ? $fieldTitle : $this->_fieldName;
  115. return $this;
  116. }
  117.  
  118. /**
  119. * Defined by Zend_Validate_Interface
  120. *
  121. * Returns true if and only if a field name has been set, the field name is available in the
  122. * context, and the value of that field name matches the provided value.
  123. *
  124. * @param string $value
  125. *
  126. * @return boolean
  127. */
  128. public function isValid($value, $context = null) {
  129. $this->_setValue($value);
  130. $field = $this->getFieldName();
  131.  
  132. if (empty($field)) {
  133. $this->_error(self::MISSING_FIELD_NAME);
  134. return false;
  135. } elseif (!isset($context[$field])) {
  136. $this->_error(self::INVALID_FIELD_NAME);
  137. return false;
  138. } elseif (is_array($context)) {
  139. if ($value == $context[$field]) {
  140. return true;
  141. }
  142. } elseif (is_string($context) && ($value == $context)) {
  143. return true;
  144. }
  145. $this->_error(self::NOT_MATCH);
  146. return false;
  147. }
  148. }
  149. ?>
  150.  
  151. $form->addElement('password', 'elementOne');
  152. $form->addElement('password', 'elementTwo', array(
  153. 'validators' => array(
  154. array('identical', false, array('token' => 'elementOne'))
  155. )
  156. ));
  157.  
  158. $password_2->addValidator('identical', false, $this->_request->getPost('password'));
Add Comment
Please, Sign In to add comment