Issues

ZF-5399: Checkbox validation problem

Description

Validators don't seem to apply to checkboxes

// ACCEPT CONDITIONS
     $this->addElement(
                'CheckBox',
                'contract',
                array(
                    'label'        => 'I accept the terms of service',
                    'style'            =>'margin-left:100px;',
                    'required'   => true,
                    'checkedValue' => '1',
                    'validators'     =>  array(array('NotEmpty', true)),
                )
            );

This seems considered valid ; and whatever validator is put in the validation never seems to occur.

Comments

I cannot reproduce this in the event of a different validator. NotEmpty is by default added when 'required' => true. However, checkbox has '0' or '1' as a value which would be correct since there was indeed a value set. If you would like the NotEmpty validator or required to trigger an error please use uncheckedValue = null as the following will evaludate to false.


require_once 'Zend/Form.php';

$_GET['test'] = 0;

$f = new Zend_Form();
$f->addElement(
    'Checkbox',
    'test',
    array(
        'required' => true,
        'uncheckedValue' => null
    )   
);
var_dump($f->isValid($_GET)); // is and should be false

To show you how you could utilize InArray to be able to utilize this event as well:


require_once 'Zend/Form.php';

$_GET['test'] = 1;

$f = new Zend_Form();
$f->addElement(
    'Checkbox',
    'test',
    array(
        'validators' => array(
            array('InArray', true, array(array(2)))
        )   
    )   
);
var_dump($f->isValid($_GET)); // is and should be false