1

In my CakePHP project, I want to display error messages but it's not working.

When I'm submitting, I want it to display an error message like: "This field is required", but by default it's showing "Please fill out this field". I changed the message, but it's not changing from the core.

My View, Model, and Controller codes are as following:

add_district.ctp:

        <div class="pg_title txtLeft">Add District</div>

               <?php echo $this->Form->create('Admins', array('action' => 'add_district'));?>
                <table>
                    <tbody>

                        <tr>
                            <td><label>District Name<span class="red required">*</span></label></td>
                            <td><?php echo $this->Form->input('District.district_name',array('label'=>false,'div'=>false,'size'=>50,'style'=>'width:330px;')); ?></td>
                        </tr>

                        <tr>
                            <td colspan="2" align="right" style="padding-right: 113px;"><input type="reset" value="Reset"> | <?php echo $this->Form->submit('ADD', array('div' => false));?></td>
                        </tr>
                    </tbody>
                </table>  
            <?php print $this->Form->end();?>   

<div class="clear"></div>

Model : District.php

<?php
App::uses('AppModel', 'Model');
/**
 * Admin Login Model
 *
 */
 class District extends AppModel
 {
    public $name='District';
    public $usetables='districts';

     public $validate = array(
     'district_name' => array(
                     'rule' => 'notEmpty',
                     'allowEmpty' => false,
                     'message' => 'This field is required'));

 }
?>

And my controller Code is (AdminController.php):

public function add_district()
        {

            $this->layout='common';
            $this->District->create();
            $this->District->set($this->data);
          if(empty($this->data) == false)
            {
                    if($this->District->save($this->data))
                    {
                        $this->Session->setFlash('District Added Successfully.');
                        $this->redirect('add_district');
                    }

            }
            else
            {
                $this->set('errors', $this->District->invalidFields());     
            }
        }
J. Steen
  • 15,470
  • 15
  • 56
  • 63
Wolverine
  • 71
  • 3
  • 11
  • This may not be directly related to your problem, but You have specified the wrong Model for your Form; `echo $this->Form('Admins', .....);` should be `echo $this->Form('District', .....);` – thaJeztah Mar 24 '13 at 20:28

4 Answers4

5

That message "Please fill out this field" is from the browser itself, not from CakePHP.

So Firefox and Chrome (and possibly other browsers?) automatically check for fields marked as required, and then when you submit a form, before even sending that form data to the server, if a required field is not filled in, the browser will give you that default popup message: "Please fill out this field".

This is the browsers default behaviour, and has nothing to do with your code. If you'd like to check that your Cake error message is working OK, then try in Safari (which, as of version 6.0.2, doesn't seem to have that 'auto detect required fields' feature). In Safari, you should get your own CakePHP error message back.

You can also prevent error checking in the browser by passing the autovalidate attribute as false for the form itself - see Disable validation of HTML5 form elements

Community
  • 1
  • 1
joshua.paling
  • 13,762
  • 4
  • 45
  • 60
3

Actually CakePHP is triggering this by adding the HTML5 required="required" attribute via the FormHelper. Previous versions of CakePHP did not do this. You can prevent this by adding 'required' => false to your form input atributes array if you wish.

Philip
  • 71
  • 5
1

The problem is the browser validation. On Chrome, at least, if input fields have been set to 'required' you are going to be frustrated. It seems to work without a problem on Safari.

Even if you add 'required'=>false from the Model, it still might not work.

What I did to get it working was on the View, I added 'required'=>'false'

e.g. I have a Users View and an add.ctp

echo $this->Form->input('fname', array('label' => 'First Name','required'=>'false'));

after that it all worked out. but I had to do it on all input fields for the cake validation to work.

0

In add_district.ctp write

<?php echo $this->Form->create('Admins', array('action' => 'add_district', 'novalidate' => true));?>

I think this will solve your problem.

Rajeev
  • 60
  • 8