我试图验证一个zend表单。但问题是,在形式上有三个字段,分别是国家、州和城市。我正在为这些字段发送有效数据,但它给了我验证错误。仅适用于国家、州和城市。错误消息是:
请输入国家名称。请输入州名称。请输入城市名称
这是我的表单字段:
$country = new Zend_Form_Element_Select('country');
$country->setRequired(true)
->setAttrib('placeholder', 'Country')
->removeDecorator('DtDdWrapper')
->addErrorMessage('Please enter country name.')
->removeDecorator('HtmlTag')
->removeDecorator('Label');
$state = new Zend_Form_Element_Select('state');
$state->setRequired(true)
->setAttrib('placeholder', 'State')
->removeDecorator('DtDdWrapper')
->addErrorMessage('Please enter state name.')
->removeDecorator('HtmlTag')
->addMultiOptions(array("" => "Select State"))
->removeDecorator('Label');
$city = new Zend_Form_Element_Select('city');
$city->setRequired(true)
->setAttrib('placeholder', 'City')
->removeDecorator('DtDdWrapper')
->addErrorMessage('Please enter city name.')
->removeDecorator('HtmlTag')
->addMultiOptions(array("" => "Select City"))
->removeDecorator('Label');
这是发布的数据:
Array (
[full_name] => Test User
[dob] => 2015-01-15
[gender] => 1
[address] => ddewewe
[country] => DZK
[state] => 26
[city] => 403564
[mobile_number] => 4535345345
[submit] => Save )
任何人都可以帮助我发现这个问题吗?
谢谢
米.
首先,您尚未在Zend_form
中为国家/地区、州和城市下拉列表设置options
。 您只设置了一个值为 balnk " 的选项,并且您已经对required
字段应用了验证,因此它给出了提到的错误。
您将收到另一个错误("大海捞针中找不到值"),因为您的Zend表单将与选择框的预先指定列表中的发布值匹配。由于验证器找不到您在POST
数据中发送的任何此类选项,因此它会抛出错误。
您可以通过以下方法解决这些问题。
-
停用验证器以验证预先指定的数组中的发布值,如下所示,并删除
setRequired(true)
验证:$country = new Zend_Form_Element_Select('country'); $country->setAttrib('placeholder', 'Country') $country->setAttrib('placeholder', 'Country') ->removeDecorator('DtDdWrapper') ->removeDecorator('HtmlTag') ->removeDecorator('Label') ->setRegisterInArrayValidator(false); //This line will not check posted data in list
-
在
zend_form
本身中设置选项数组。class RegistrationForm extends Zend_Form { public function __construct($options = null) { $country = new Zend_Form_Element_Select('country'); $country->setRequired(true) ->setAttrib('placeholder', 'Country') ->removeDecorator('DtDdWrapper') ->addErrorMessage('Please enter country name.') ->removeDecorator('HtmlTag') ->removeDecorator('Label'); foreach($options['countries'] as $option) $country->addMultiOption(trim($option->code),trim($option->country_name)); } }
并在
controller
中创建Zend形式的对象时,在其中传递countries
数组。