复选框的自定义Zend错误消息

4 validation zend-framework

我在基于Zend的网站上有一个表单,其中包含所需的"条款和条件"复选框.

我已经设置了一条自定义消息,上面写着"您必须同意条款和条件".

但是,因为复选框是"presence ='required'",它会返回

Field 'terms' is required by rule 'terms', but the field is missing
Run Code Online (Sandbox Code Playgroud)

这是Zend框架中定义的常量:

self::MISSING_MESSAGE     => "Field '%field%' is required by rule '%rule%', but the field is missing",
Run Code Online (Sandbox Code Playgroud)

我可以编辑此常量,但这会更改所有必需复选框的错误报告.

如何影响此特定案例的错误报告?

gna*_*arf 14

如果您正在使用,则Zend_Form_Element_Checkbox可以Zend_Validate验证器上自定义错误消息.

$form->addElement('checkbox', 'terms', array(
  'label'=>'Terms and Services',
  'uncheckedValue'=> '',
  'checkedValue' => 'I Agree',
  'validators' => array(
    // array($validator, $breakOnChainFailure, $options)
    array('notEmpty', true, array(
      'messages' => array(
        'isEmpty'=>'You must agree to the terms'
      )
    ))
   ),
   'required'=>true,
);
Run Code Online (Sandbox Code Playgroud)

您要确保未选中的值为"空白"并且该字段为"必需"