CakePHP: validation message not showing

I am new to cakePHP and I made one simple form after some tutorial. In this html form I used validation. Now the problem is that the validation is working but the message is not displaying what I want to display. I am trying the code below,

Model

 public $validate = array(
        'title' => array(
            'title_required' => array(
                'rule' => 'notEmpty',
                'message' => 'This is required field'
            ),
            'title_unique' => array(
                'rule' => 'isUnique',
                'message' => 'This should be unique title'
            )
        )
    );

      

controller

public function add() {
        if ($this->request->data) {
            if ($this->Post->save($this->request->data)) {
                $this->Session->setFlash('Post has been added successfully');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Error occured, Please try agan later!');
            }
        }
    }

      

View

<h2>Add New Post</h2>
<?php
echo $this->Form->create('Post', array('action'=>'add'));
echo $this->Form->input('title');
echo $this->Form->input('body');
echo $this->Form->end('Create Post');
?>

      

The validation error I saw, but not the message I mentioned in the controller

enter image description here

+3


source to share


3 answers


This is a built-in browser check.

As of 2.3, the required HTML5 attribute will also be added to the input based on validation rules.

Yours title

has a rule notEmpty

, so Cake outputs

<input type="text" required="required" ..

and your browser fires this message.



Edit: to override this behavior, you can do:

$this->Form->input('title', array('required'=>false));

      

or

$this->Form->submit('Submit', array('formnovalidate' => true));

      

When you submit the form, your validation model will fire.

+16


source


From your code, I can see that you did not include helpers.

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

      



Just add your controllers and give it a try.

0


source


Form-create () parameters are invalid, the first argument is the model name, the second is for parameters:

<h2>Add New Post</h2>
<?php
echo $this->Form->create('Post', array('action'=>'add'));
echo $this->Form->input('title');
echo $this->Form->input('body');
echo $this->Form->end('Create Post');
?>

      

If the form helper does not know which "model" creates the form for, I will not validate the field in the correct place, hence it will not throw validation errors for "title"

Decision

[update] above did not solve the problem. OP changed the question

Some ideas:

  • Be sure to enable "debug" (app / Config / core.php set Configure::write('debug', 2);

    Otherwise CakePHP may use a "cached" version of your model.

  • If you mis-specify your model, Cake can automatically generate a model for you, in which case your own model is never actually used, try this for debugging to make sure we even get to your model:

Add this to your model;

public function beforeValidate($options = array)
{
     debug($this->data); exit();
}

      

0


source







All Articles