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

1
  • I added some more ideas to my answer Commented Feb 6, 2013 at 7:54

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@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!
@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.
Then your model validation isn't set up correctly; this is entirely client side.
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..

4 Comments

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.
Helper must be mentioned even in cakephp2.o as far as i knw
@Shams have you tried my answer? I suspect it's because you've left out the 'Model' name when creating the form
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
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();
}

4 Comments

i changed the view as you mentioned but not working, aslo plz check the question, I put error pic aslo
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' `
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
I'm using 2.3.0 and I think it havn't any issue with capital or smaller P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.