zend 表单验证错误
zend form validation error
我正在尝试验证 zend 表单。但问题是,在表单中有三个字段,名称分别为 country、state 和 city。我正在为这些字段发送有效数据,但它给我验证错误。仅适用于国家、州和城市。错误消息是:
请输入国家名称。
请输入州名。
请输入城市名称
这是我的表单字段:
$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
字段应用验证,因此它给出了提到的错误。
您将收到另一个错误 ("value was not found in the haystack" ),因为您的 Zend 表单将与 select 框中预先指定的列表中的张贴值相匹配。由于验证器不会找到您在 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
的数组传递给其中。
我正在尝试验证 zend 表单。但问题是,在表单中有三个字段,名称分别为 country、state 和 city。我正在为这些字段发送有效数据,但它给我验证错误。仅适用于国家、州和城市。错误消息是:
请输入国家名称。 请输入州名。 请输入城市名称
这是我的表单字段:
$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
字段应用验证,因此它给出了提到的错误。
您将收到另一个错误 ("value was not found in the haystack" ),因为您的 Zend 表单将与 select 框中预先指定的列表中的张贴值相匹配。由于验证器不会找到您在 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
的数组传递给其中。