i have 2 text fields in form.
- textfielda - not required
- 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.
conditionally call
setrequired()
on relevant elements creatingprevalidate()
method on form , calling in controller. [really same idea @marcin, pushed down form itself, keeping controller bit leaner.]create custom validator called
conditionallyrequired
accepts option fieldname of "other field". attach validator each element, configuring name of "other" element. in validator'sisvalid($value, $context)
method, conditionally test$value
if$context['otherfield']
non-empty.
Comments
Post a Comment