0

again alot of similar questions out there but none of them really help me. HTML5 form validation seems to be triggering with messages "Please fill in this field" instead of the model validation messages which should be "Please enter the model"

I have a form to add Computers to the database.

Here is my form:

echo $this->Form->create('Computer');
echo $this->Form->input('Computer.model', array('label' => 'Model'));
echo $this->Form->input('Computer.memory', array('label' => 'memory'));
echo $this->Form->input('Computer.hdd', array('label' => 'hdd'));
echo $this->Form->input('Computer.price', array('label' => 'price'));    
echo $this->Form->end('Save Computer');

Here is the full controller code with index and add actions

<?php
class ComputersController extends AppController {

    public $helpers = array('Html', 'Form', 'Session');
    public $components = array('Session');


    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('add');
    }
    public function index() {


        $this->set('computers', $this->Computer->find('all'));

    }

    public function add() {

        if ($this->request->is('post')) {
            if (!empty($this->request->data)) {

                $this->Computer->save($this->request->data);
                $this->Session->setFlash(__('Your Computer has been saved, or so it seems.....'));
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(__('Not sure why we got here 1.'));
        } else {
            $this->Session->setFlash(__('By right, this should be the index page'));


        }

    }

}
?>

Here's the model

<?php
class Computer extends AppModel {


public $validate = array(

    'model' => array(
        'Please enter model name'=> array(
            'rule'=>'notEmpty',
            'message'=>'Please enter model'
        )
    )

);
}

?>

I read from other forms that triggering the model save function, which I do, will automatically trigger the model validation. How can i get the model validation to work?

Thanks Kevin

aDvo
  • 894
  • 4
  • 15
  • 43
  • Note: the model field defaults as I discovered with some troublehshooting that it is made "required" because there is a model validation rule 'notEmpty'. This is great. However, it still does not display the Model validation message, but shows the HTML5 message instead. – aDvo Mar 06 '14 at 04:46
  • 2
    Cake only adds `required="required"`, which is handled by the browser. If you 'inspect element' and manually remove the 'required' attribute, this will get to the server and you'll get the message from the model. Cake does not change the browser validation message. This can be changed manually (http://stackoverflow.com/questions/5272433/html5-form-required-attribute-set-custom-validation-message) – cornelb Mar 06 '14 at 06:30
  • similar to this http://stackoverflow.com/a/21094082/2776508 – arilia Mar 06 '14 at 07:51
  • @AD7six I did, and added a potential solution. – cornelb Mar 06 '14 at 08:57

3 Answers3

1

As you were saying, if you have the notEmpty validation in the model, CakePHP adds required="required" on the input attributes. This is handled by the browser, so you see the default Please enter this field message when you try to submit an empty value. An advantage is that if you are using the browser in a different language, the message will be displayed in that language.

If you want to change that message, you can try a solution like the ones from this question. (this is probably not what you want)

If you want to remove that client-side message, you can disable it using novalidate

echo $this->Form->create('Computer', array('novalidate' => 'novalidate'));

This way, the HTML5 required property will be ignored, and you will get the message from the model.

I am not sure if there is a way to tell Cake to use the server-side value on the client.

Community
  • 1
  • 1
cornelb
  • 6,046
  • 3
  • 19
  • 30
  • not sure what `a way to tell Cake to use the server-side value on the client.` means (validation by ajax? no, there isn't). +1. – AD7six Mar 06 '14 at 09:03
0

$this->{Model}->save() returns false if the validation fails, but in your case you're redirecting with a flash message after save function. so first check the form is saving perfectly or not, if perfectly saving then redirect to listing page other wise render your view file with a flash message where you can view the validation messages.

if ($this->Computer->save($this->request->data)) {
    $this->Session->setFlash(__('Your Computer has been saved, or so it seems.....'));
    return $this->redirect(array('action' => 'index'));
} else {
    $this->Session->setFlash(__('Unable to save form'));
}

Note: To disable html validation just do

$this->Form->inputDefaults(array(
    'required' => false
)); 

in your view file

Hope this helps you.

Anil kumar
  • 4,107
  • 1
  • 21
  • 36
  • Thanks, this was the answer i was looking for. The other answers were helpful still. It wasn't obvious that a failed call to the models save method and then redirecting to the index page would result in the models error messages showing. It just wasn't obvious.... – aDvo Mar 06 '14 at 13:03
0

Set 'novalidate' => true in options for FormHelper::create()

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

For more information, go to http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html

vincentcjl
  • 46
  • 4