[英]Zend_Form radio element
Below is sample code to create a radio button element with Yes/No options in Zend_Form. 下面是在Zend_Form中创建带有Yes / No选项的单选按钮元素的示例代码。 Any ideas on how to set the required answer to Yes, so if No is selected, it'll fail validation? 关于如何将所需答案设置为是的任何想法,所以如果选择否,它将无法验证? The code below will accept either Yes or No. 以下代码将接受是或否。
$question= new Zend_Form_Element_Radio('question');
$question->setRequired(true)
->setLabel('Are you sure?')
->setMultiOptions(array('Yes', 'No'));
Not sure if this is the best way, but it worked for me: 不确定这是否是最佳方式,但它对我有用:
$questionValid = new Zend_Validate_InArray(array('Yes'));
$questionValid->setMessage('Yes is required!');
$question = new Zend_Form_Element_Radio('question');
$question->setRequired(true)
->setLabel('Are you sure?')
->setMultiOptions(array('Yes'=>'Yes', 'No'=>'No'))
->addValidator($questionValid);
A quicker way, though this wouldn't work for other situations: 更快的方法,虽然这不适用于其他情况:
$question = new Zend_Form_Element_Radio('question');
$question->setRequired(true)
->setLabel('Are you sure?')
->setMultiOptions(array('Yes'=>'Yes', 'No'=>'No'))
->addValidator('StringLength', false, array('min' => 3, 'messages' => "You must be sure."));
Since "no" is less than 3 characters, this will fail unless "yes" is selected. 由于“否”小于3个字符,除非选择“是”,否则将失败。 It's a little "hacky", but I like this way because it uses less code and also makes use of the built-in validators. 它有点“hacky”,但我喜欢这种方式,因为它使用较少的代码并且还使用了内置的验证器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.