1

I am doing model validation in my admin panel login so there is only two fields username and password. Validation is working but custom message which I have written in my model is not shown.

Model

public $validate = array(
    'username' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'Please Enter Your Username'
        )
    ),
    'password' => array(
        'required' => array(
            'rule' => array ('notEmpty'),
            'message' => 'Please Enter Your Password'
        )
    )
);

Controller

function login(){
    $this->layout = 'admin_login';  

    if ($this->request->is('post')) {         
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirect());
        }
        $this->Session->setFlash(__('Invalid username or password, try again'));
    }
}

View

echo $this->Form->create('Admin',array('autocomplete'=>"off"));

echo '<div style="width:294px;float:left;position:relative;">';
echo $this->Form->input('username' , array('label' => '', 'placeholder' =>'Enter your username','div' => false));
echo $this->Form->input('password' , array('label' => '', 'value' =>'', 'div' => false,'placeholder'=>'Enter Your Password'));
echo '</div>';
echo '<div style="padding-left:0px;">'; 

echo $this->Form->end(__('Login' ,true));   

enter image description here

I have already tried a few things like which is mentioned in this link, but it's not working for me.

CakePHP : Validation message not displaying

Community
  • 1
  • 1
Doitnow
  • 73
  • 3
  • 13
  • What version of cakePHP did you used? – Ikong Aug 28 '13 at 08:12
  • Duplicate: http://stackoverflow.com/questions/14723685/cakephp-validation-message-not-displaying – Ikong Aug 28 '13 at 08:17
  • You can say its duplicate question , but I have also mentioned the link for which you are saying its a duplicate question but all the solution which are given there is not working for me.I am using cakephp 2.3.9 version. – Doitnow Aug 28 '13 at 08:52
  • Then please show what exactly you did, the examples in the linked question are working fine. – ndm Aug 28 '13 at 09:14
  • When I use 'required'=>false inside my username or password input field , it is submitting the form without any validation. – Doitnow Aug 28 '13 at 09:23
  • What validation are you talking about, browser or server validation? The latter wont be affected by that. For custom browser validation see for example http://stackoverflow.com/a/5276722 or http://afarkas.github.io/webshim/demos/demos/webforms/1-webforms-lang-custom-bubble.html – ndm Aug 28 '13 at 10:08
  • I am talking about validation which I have written in my model. I just want to replace "Please fill out this field" with a message which I have mentioned in my model that is "Please Enter Your Username" – Doitnow Aug 28 '13 at 10:30

3 Answers3

0

That looks like a message from the browser and not CakePHP.

CakePHP now adds a required attribute which modern browsers can use to trigger an error.

You can do one of three things here:

One: Set up your form to leave validation to the server:

 $this->Form->create(array('novalidate'=>true));

Two: Set a custom validation message in the browser: http://www.whatwg.org/specs/web-apps/current-work/multipage/association-of-controls-and-forms.html#dom-cva-setcustomvalidity

Three: tolerate it

timstermatic
  • 1,710
  • 14
  • 32
  • Thanks for your suggestion, But I just want to know it's possible to use cakephp Model validation message instead on default message.If it not work then I will write javascript validation code instead of cakephp model validation. – Doitnow Aug 28 '13 at 10:52
  • 1
    I wouldn't "write javascript validation code instead of cakephp model validation." You should be doing both as client side validation is just bells and whistles and can easily be circumvented. – timstermatic Aug 28 '13 at 11:33
0

You get that message because the "username" field is flagged as "required". Maybe you've not defined it in the Form->input() function, but the "required" flag has been automatically added from the Model (due to your validation rules). As timstermatic said, it's a browser validation message caused by the required attribute.

To solve this issue (and show the CakePHP validation message) you've to force for avoiding the addition of the "required" flag on your field:

$this->Form->input('username', array('required' => FALSE));

This will override the Model automatic additions. Happy coding ;)

*Edited => It's important to clarify that the inline override removes only the required flag on the field: you'll take advantage of the Model validation anyway (just because if an empty field is sent to the server, it will not pass the validation rule you entered.

Andrea Alhena
  • 986
  • 6
  • 9
0

keep this code it will bypass the html5 validation and add your custom validations

view echo $this->form->create('Post',array('action'=>'add'));
echo $this->form->input('title'); echo $this->form->input('body'); echo $this->form->submit('Save Post',array('formnovalidate'=>true)); echo $this->form->end();//Creates ending form tag

Model var $validate=array(
'title'=>array(
'title_must_not_be_empty'=>array('rule'=>'notEmpty','message'=>'Please enter a title),
'title_must_be_unique'=>array('rule'=>'isUnique','message'=>'Title name already exists') ),
'body'=>array( 'body_must_not_be_empty'=>array( 'rule'=>'notEmpty', 'message'=>'Please enter body' ) ) );

This will work just the way you want