Arif
Arif

Reputation: 1211

CakePHP : Validation message not displaying

I'm new to cakePHP and I've made a simple form following some tutorial. On this html form I've used validation. Now the problem is that the validation is working but the message is not displaying what I want it to display. I tried 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 which I've seen is not the message I mentioned in my controller.

enter image description here

Upvotes: 3

Views: 14582

Answers (3)

thaJeztah
thaJeztah

Reputation: 29007

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

<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' it is creating a form for, I won't check for field validation in the right place, hence, it won't output the validation errors for 'title'

[update] solution above didn't solve the problem. OP has modified the question

Some ideas:

  1. Be sure to enable 'debug' (App/Config/core.php set Configure::write('debug', 2); Otherwise CakePHP may be using a 'cached' version of your model.

  2. If you've named your Model incorrectly, Cake may be automatically generating a model for you, in which case your own model is never actually used, try this for debugging to see if we even 'get' to your model:

Add this to your model;

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

Upvotes: 0

Ross
Ross

Reputation: 17967

That's built-in browser validation.

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

Your title has the notEmpty rule, so Cake is outputting

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

and your browser is triggering that message.

Edit: to override this behaviour, you can do:

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

or

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

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

Upvotes: 16

Php Geek
Php Geek

Reputation: 1107

From your code what i can see is that you havent included helpers.

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

Just add to your controllers and try..

Upvotes: 0

Related Questions