+ Reply to Thread
Results 1 to 2 of 2

Thread: Zend_Filter_Input doesn't validate radio and checkbox if not checked

  1. #1
    oi_antz is offline Junior Member
    Join Date
    Aug 2009
    Posts
    2

    Default Zend_Filter_Input doesn't validate radio and checkbox if not checked

    I have a form:

    [HTML]<form method="post" id="validatable-form" runat="vdaemon">
    <label>Name: </label>
    <input type="text" name="name" value="<?php echo $_POST['name'] ?>" /><br />

    <label>Date of birth: </label>
    <input type="text" name="dob" value="<?php echo $_POST['dob'] ?>" /><br />

    <input type="radio" name="sex" value="m" />Male<br />
    <input type="radio" name="sex" value="f" />Female<br />

    <label>Phone</label>
    <input type="text" name="phone" value="<?php echo $_POST['phone'] ?>" /><br />

    <input type="submit" value="Go!" />

    </form>[/HTML]

    And I have some validation:

    [PHP]/** input filter */
    $validators = array(
    'name' => array(
    'Alpha'
    ),
    'dob' => array(
    array('Regex', '/[0-9\/\- ]/'),
    array('StringLength', 8, 10)
    ),
    'sex' => array(
    array('NotEmpty'),
    array('StringLength', 1, 1),
    array('Regex', '/[fm]/')
    )
    );
    $filters = array(
    '*' => 'StringTrim'
    );

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

    echo '<h2>Value: '.$input->getUnescaped('name').' - '.$input->getUnescaped('sex').'</h2>';

    if($input->isValid()){
    echo 'The data is valid!';
    }else{
    echo 'The data is invalid, failed with reasons: ';
    echo '<pre>';
    var_dump($input->getMessages());
    echo '</pre>';

    }[/PHP]

    Problem is when the form is submitted with no sex selected (both radios are un-selected), Zend_Filter_Input fails to validate that sex is not empty, it does not report an error. This is due to the post array not containing a key called "sex", and the same happens when checkboxes are not checked.

    One way I might be able to get it working is

    [PHP]$_POST = array_merge($defaultParams, $_POST);
    $input = new Zend_Filter_Input($filters, $validators, $_POST);[/PHP]

    But this seems a bit hack-ish, is there a better solution for this problem?

  2. #2
    oi_antz is offline Junior Member
    Join Date
    Aug 2009
    Posts
    2

    Default

    Found it here: Common Zend_Filter_Input problems | CodeUtopia - The blog of Jani Hartikainen

    The answer is presence => required

    [PHP]/** input filter */
    $validators = array(
    'name' => array(
    'Alpha'
    ),
    'dob' => array(
    array('Regex', '/[0-9\/\- ]/'),
    array('StringLength', 8, 10)
    ),
    'sex' => array(
    'presence' => 'required',
    'NotEmpty',
    array('StringLength', 1, 1),
    array('Regex', '/[fm]/')
    )
    );[/PHP]

+ Reply to Thread

Similar Threads

  1. multicheckbox return empty array when no boxes checked
    By LictorLyons in forum Model-View-Controller (MVC)
    Replies: 7
    Last Post: 02-10-2010, 11:02 PM
  2. checkbox behaviour and validating
    By jujhimup in forum Model-View-Controller (MVC)
    Replies: 0
    Last Post: 11-19-2009, 11:31 AM
  3. isChecked() Checkbox
    By altergothen in forum Core Infrastructure
    Replies: 0
    Last Post: 04-24-2009, 01:01 PM
  4. Multicheckbox: trigger a javascript fn only when specified option is checked
    By nandana in forum General Q&A on Zend Framework
    Replies: 3
    Last Post: 03-10-2009, 09:04 AM
  5. checkbox array validator
    By webeks in forum General Q&A on Zend Framework
    Replies: 0
    Last Post: 02-09-2009, 06:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts