Issues

ZF-5399: Checkbox validation problem

Issue Type: Bug Created: 2009-01-03T02:05:59.000+0000 Last Updated: 2009-10-15T09:14:17.000+0000 Status: Resolved Fix version(s): Reporter: Zladivliba (zladivliba) Assignee: Matthew Weier O'Phinney (matthew) Tags: - Zend_Form

Related issues: - ZF-5325

Attachments:

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

Posted by Mike Willbanks (digitalstruct) on 2009-06-24T22:21:45.000+0000

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.

<pre class="highlight">
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:

<pre class="highlight">
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

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts