Checkbox validation never validates

With the latest snapshop of 1.0, the validation for checkbox never validate. There is no error message, but validate() still is false.

minimal example[spoiler]controllers/TestController.php


<?php

class TestController extends BaseController

{

    public function actionIndex()

    {

        $form = new TestForm;

        if(isset($_POST['TestForm']))

        {

            $form->attributes = $_POST['TestForm'];

          if ($form->validate())

            {

                $this->renderText('ok');

                return;

            }

        }

  $this->render('test', array('form'=>$form));

    }

}

?>

models/testForm.php


<?php

class TestForm extends CFormModel {

    public $check;

    public function rules() {

        return array(

            array('check', 'required', 'requiredValue'=>true),

        );

    }

    

    public function safeAttributes() {

        return array( 'check', );

    }

}

?>

views/test/test.php


<?php

echo CHtml::beginForm();

echo CHtml::errorSummary($form);

echo CHtml::ActiveCheckBox($form, 'check');

echo CHtml::submitButton('Submit');

echo CHtml::endForm();

?>

[/spoiler]

Works fine with v1.0.11.

the problem looks come back at version 1.1.4

By default checkbox field return ‘1’ when checked and ‘0’ when not checked. This is why ‘required’ validator passes without error. To work properly, you need to force checking for value ‘1’




function rules() {

  array( 'check', 'required', 'requiredValue'=>1 )

}



or change the default ‘uncheckValue’ to empty string:




echo $form->checkbox( $model, 'check', array( 'uncheckValue'=>'' ) );



Both solutions work, but they will give different error messages - your choice.

@redguy thank you. I’d found this solution, too… but, I forgot to post here :)