10

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'
               ));
2
  • 3
    Your rules array should be $validate, not $check, I believe. Commented Jul 16, 2011 at 15:40
  • Maybe overkill, but you can also leverage a Confirmable Behavior. Commented Sep 12, 2013 at 12:05

6 Answers 6

17

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')); ?>
Sign up to request clarification or add additional context in comments.

Comments

1

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')); ?>

Comments

1

basically, you add the rule "notEmpty" for this field to the public $validate array of the model. this way an error will get triggered on Model->validates() if the checkbox has not been checked.

maybe some overhead in your case, but if you happen to use it more often try a DRY (dont repeat yourself) approach. you can also use a behavior for this to use this clean and without tempering too much with the model/controller:

// view form
echo $this->Form->input('confirmation', array('type'=>'checkbox', 'label'=>__('Yes, I actually read it', true)));

and in the controller action

// if posted
$this->Model->Behaviors->attach(array('Tools.Confirmable'=>array('field'=>'confirmation', 'message'=>'My custom message')));
$this->Model->set($this->data);
if ($this->Model->validates()) {
    // OK
} else {
    // Error flash message here
}

1.x: https://github.com/dereuromark/tools/blob/1.3/models/behaviors/confirmable.php

for 2.x: https://github.com/dereuromark/cakephp-tools/blob/2.x/Model/Behavior/ConfirmableBehavior.php

3.x: https://github.com/dereuromark/cakephp-tools/blob/master/src/Model/Behavior/ConfirmableBehavior.php

details: http://www.dereuromark.de/2011/07/05/introducing-two-cakephp-behaviors/

Comments

0

I believe you need try save it into your model to catch your tos rules. I should do something like =

if(!$mymodel->save()){
 // catch error tos.
}

Comments

0

$this->ModelName->invalidFields() returns an array of fields that failed validation.

You could try and search this for the tos field, and output a message if the key exists.

~untested (I'm not sure off the top of my head the exact structure of the invalidFields return array.

$failed_fields = $this->ModelName->invalidFields();

if(array_key_exists('tos', $failed_fields)) {
    $this->Session->setFlash('Please accept the terms and conditions');
}

Comments

0

you don't even have to have validation rule for tos, just check that at the controller before saving the data.

if($this->data['Model']['tos']==1){
     // save data
  }else{
     //set flash
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.