zend framework - Zend_Form - How to addValidator after the form has been submitted -


i have 2 text fields in form.

  1. textfielda - not required
  2. textfieldb - not required

after user submitted form, how add validator / setrequired(true) textfieldb if value of textfiela not empty?

i see 2 approaches in addition @marcin's idea.

  1. conditionally call setrequired() on relevant elements creating prevalidate() method on form , calling in controller. [really same idea @marcin, pushed down form itself, keeping controller bit leaner.]

  2. create custom validator called conditionallyrequired accepts option fieldname of "other field". attach validator each element, configuring name of "other" element. in validator's isvalid($value, $context) method, conditionally test $value if $context['otherfield'] non-empty.


Comments