3

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

Marty
  • 146
  • 1
  • 13
Arif
  • 1,222
  • 6
  • 29
  • 60

3 Answers3

16

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.

Ross
  • 18,117
  • 7
  • 44
  • 64
  • @ross YES! I already thought I recognized the layout of the message, but couldn't place it. That explains my suggestions not working because the *browser* is blocking the submit of the browser. Great finding! – thaJeztah Feb 07 '13 at 23:15
  • @Ross, sorry for silly question but I tried these solutions but nothing is working for me, when i try an of the solution then it turns off the browsers HTML5 validation and cakephp validation as well...please help me. – Vikas Sharma Jul 23 '14 at 18:35
  • Then your model validation isn't set up correctly; this is entirely client side. – Ross Jul 23 '14 at 20:30
0

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..

Php Geek
  • 1,107
  • 1
  • 15
  • 36
  • 1
    The Session-component is enabled by default (inherited from 'lib/Cake/Controller/Controller.php') The Helpers will be auto-loaded in CakePHP 2.x if they are not explicitly set. – thaJeztah Feb 06 '13 at 07:35
  • Helper must be mentioned even in cakephp2.o as far as i knw – Php Geek Feb 06 '13 at 07:36
  • @Shams have you tried my answer? I suspect it's because you've left out the 'Model' name when creating the form – thaJeztah Feb 06 '13 at 07:38
  • check the view part in my question I have updated, also i put the image of the error I am facing but i think its default error message not mine – Arif Feb 06 '13 at 07:44
0

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();
}
Marty
  • 146
  • 1
  • 13
thaJeztah
  • 27,738
  • 9
  • 73
  • 92
  • i changed the view as you mentioned but not working, aslo plz check the question, I put error pic aslo – Arif Feb 06 '13 at 07:39
  • So you DON't see a message on your screen? In that case your Post-model may be named incorrectly; the filename should be 'app/Model/Post.php'. As a final check you could check what class CakePHP is using for the model by putting this at the start of your 'add()' method in the controller: `debug(get_class($this->Post));` this should output ` 'Post' ` – thaJeztah Feb 06 '13 at 08:12
  • But you don't see the debug from the 'Post->beforeValidate()' on your screen? Have you tried debugging $this->request->data at the start of your add() method? If $this->request->data doesn't get propagated, Post->save() will never get called. Are you using CakePHP 2.x or 1.x? For CakePHP 2.x 'post.php' should start with a capital, although that should not be the problem – thaJeztah Feb 06 '13 at 09:06
  • I'm using 2.3.0 and I think it havn't any issue with capital or smaller P – Arif Feb 06 '13 at 19:30