开发者

validate form which is conditional on multiple elements (they all can not be empty)

Does anyone have a way to validate a zend form, within the form? In my case multiple elements can not be empty?

In Symfony, if I remember right, there is post validator which you can get all the submitted form values and use those to validate开发者_C百科. I am not able to find anything like that with Zend (1.10).

Any advice is greatly appreciated!


Just use a custom validator attached to the element:

class My_Validator extends Zend_Validate_Abstract {
    public function isValid( $value, $context = null ) {}
}

You'll be able to access values of all elements through $context array.
More info here, scroll to 'Note: Validation Context'.


I ended up overriding Zend_Form::isValid.

 public function isValid($data)
 {
  $valid = parent::isValid($data);

  $elements = array('username','firstname','lastname');
  foreach($elements as $elementname)
  {
   $element = $this->getElement($elementname);
   if($element->getValue())
   {
    return $valid;
   }
  }
  $this->addErrorMessage('EMPTY_ELEMENTS');
  return false;
 }

PS: Instead of looping through my elements I could have also used the $data array where element values where stored.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜