Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP "Agree TOS" Checkbox Validation

I am trying to have a Checkbox "Agree TOS".

If the Checkbox is not checked, I want to put out a Flash Message.

How do I do this?

My View:

<?php   
        echo $form->create('Item', array('url' => array_merge(array('action' => 'find'), $this->params['pass'])));
        echo $form->input('Search', array('div' => false));
        echo $form->submit(__('Search', true), array('div' => false));
        echo $form->checkbox('tos', array('label' => false, 'value'=>1)).' Agree TOS'; 
        echo $form->error('tos');
        echo $form->end();
?>

My Model:

var $check = array(
            'tos' => array(
               'rule' => array('comparison', 'equal to', 1),
               'required' => true,
               'allowEmpty' => false,
               'on' => 'index',
               'message' => 'You have to agree TOS'
               ));
like image 547
grosseskino Avatar asked Jul 16 '11 14:07

grosseskino


2 Answers

This seems working for me. Hope it will help.

In model:

            'tos' => array(
                'notEmpty' => array(
                    'rule'     => array('comparison', '!=', 0),
                    'required' => true,
                    'message'  => 'Please check this box if you want to proceed.'
                )

In view:

    <?php echo $this->Form->input('tos', array('type'=>'checkbox', 'label'=>__('I confirm I have read the <a href="/privacy-statement">privacy statement</a>.', true), 'hiddenField' => false, 'value' => '0')); ?>
like image 80
Invincible Avatar answered Nov 16 '22 04:11

Invincible


Model

'agreed' => array(
       'notempty' => array(
            'rule' => array('comparison', '!=', 0),//'checkAgree',
            'message' => ''You have to agree TOS'',
            'allowEmpty' => false,
            'required' => true,
            'last' => true, // Stop validation after this rule
            'on' => 'signup', // Limit validation to 'create' or 'update' operations
        ),
    ),

View

<?php echo $this->Form->input('agreed',array('value'=>'0'),array('type'=>'checkbox', 'label'=>'Agree to TOS')); ?>
like image 31
Fury Avatar answered Nov 16 '22 04:11

Fury