【问题标题】:zend form validation errorzend 表单验证错误
【发布时间】:2015-01-14 23:47:03
【问题描述】:

我试图验证一个 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 )

谁能帮我发现这个问题?

谢谢,

M.

【问题讨论】:

  • 请发布准确的错误信息和控制器操作代码。以便我能提供帮助。
  • @Techgyani :我在问题中添加了错误消息。请看一下
  • 请分享您处理此表单的控制器代码。

标签: php forms validation zend-framework


【解决方案1】:

首先,您尚未在Zend_form 中为您的国家、州和城市下拉列表设置options。您只设置了一个值为 balnk "" 的选项,并且您已对 required 字段应用了验证,因此它给出了上述错误。

您将收到另一个错误(“在大海捞针中找不到值”),因为您的 Zend 表单将匹配选择框的预先指定列表中发布的值。由于验证器不会找到您在POST 数据中发送的任何此类选项,因此它将引发错误。

您有以下方法来解决这些问题。

  1. 停用验证器以验证预指定数组中的已发布值,如下所示并删除 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
    
  2. 在您的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 的数组。

【讨论】:

  • @s-ruplali:谢谢回复。但我已经从控制器设置了国家和城市的所有选项。这就是为什么我在 Posted Data Array 中获取国家、州和城市的原因。我正在使用 jquery 动态填充字段
  • 那么您能否也发布您的控制器的代码片段?
  • 没有什么特别像 if(is valid){ save data }else{ populate form }
  • 我想看看您是如何设置下拉菜单的选项的?通过表格或任何其他方式!
  • 来自控制器:设置国家 $formObj->addMultiOptions(国家数组作为键,值)并通过 jquery 填充州和城市
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多