views:

1799

answers:

2

I have a form in a Zend-based site which has a required "Terms and Conditions" checkbox.

I have set a custom message which says "you must agree with terms and conditions".

however, because the checkbox is "presence='required'", it returns

Field 'terms' is required by rule 'terms', but the field is missing

which is this constant defined in the Zend framework:

self::MISSING_MESSAGE     => "Field '%field%' is required by rule '%rule%', but the field is missing",

I could edit this constant, but this would change the error reporting for all required checkboxes.

How can I affect the error reporting for this specific case?

+1  A: 

You can override the default message like this:

$options = array(
                'missingMessage' => "Field '%field%' is required by rule '%rule%', dawg!"
            );

And then:

$input = new Zend_Filter_Input($filters, $validators, $myData);

Or

$input = new Zend_Filter_Input($filters, $validators, $myData);
$input->setOptions($options);

...and finally:

if ($input->hasInvalid() || $input->hasMissing()) {
  $messages = $input->getMessages();
}

It's mentioned on the Zend_Filter_Input manual page.

karim79
+5  A: 

If you are using the Zend_Form_Element_Checkbox you can customize the error messages on the Zend_Validate validators.

$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,
);

You want to make sure the unchecked value is "blank" and that the field is "required"

gnarf