Zend 框架:条件验证
我需要根据另一个字段的值对表单字段设置一些验证。
例如,如果职业是医生,则要求专业不能为空('')或无('none')。
$professionOptions = array(
'' => 'Choose Profession',
'Dr.' => 'Dr.',
'zzz' => 'zzz',
'None' => 'None');
$this->validator->field('profession')->inArray(array_keys($professionOptions)) ->message('Invalid profession.');
$specialtySelectOptions = array(
'' => 'Choose Specialty',
'Heart' => 'Heart',
'Lungs' => 'Lungs',
'Feet' => 'Feet',
'Nose' => 'Nose');
我如何使以下内容取决于职业?
$this->validator->field('specialty')->inArray(array_keys($specialtySelectOptions))
->message('Invalid salutation.');
I need to set up some validation for a form field based on a value of another field.
For instance if profession is Doctor, then require specialty not to be blank ('') or none ('none').
$professionOptions = array(
'' => 'Choose Profession',
'Dr.' => 'Dr.',
'zzz' => 'zzz',
'None' => 'None');
$this->validator->field('profession')->inArray(array_keys($professionOptions)) ->message('Invalid profession.');
$specialtySelectOptions = array(
'' => 'Choose Specialty',
'Heart' => 'Heart',
'Lungs' => 'Lungs',
'Feet' => 'Feet',
'Nose' => 'Nose');
How do i make the following dependent on the profession?
$this->validator->field('specialty')->inArray(array_keys($specialtySelectOptions))
->message('Invalid salutation.');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在表单类中扩展 isValid() 方法,例如
You can extend the isValid() method in your form class, e.g.
我通常走这条路
I usually go this way
这家伙通过自定义验证类想出了一个巧妙的方法。
http://jeremykendall.net/2008/12/24 /条件形式验证-with-zend_form/
This guy figured out a slick way via a custom validation class.
http://jeremykendall.net/2008/12/24/conditional-form-validation-with-zend_form/