user3301561
user3301561

Reputation:

Unable to Show error message in cakephp 2.x validations

I am developing the simple register form.And develope the model,controller,view classes.but i am unable to show the error messages in my application.

User.php(Model class)

<?php
class User extends AppModel
{
 var $name='User';
 public $validate= array(
                'username'=>array(
                        'rule'=>'notEmpty',
                        'required'=>true,
                        'message'=>'Enter your name'
                ),
                'email' => array(
                        'rule' => 'notEmpty',
                        'message' => 'Please enter your email'
                )
               );
}
?>

UsersController.php

<?php
    class UsersController extends AppController {

    public $helpers = array('Html', 'Form');
     array('action' => 'edit')
    public function register() {

            if ($this->User->validates()) {
         $this->User->set($this->request->data);
                $name = $this->request->data['User']['username'];
                $email = $this->request->data['User']['email'];
            }
            else{
            $this->flash('register fail');
            }
}
}
?>

register.ctp

<?php  
    echo $this->Form->create('User',array('action'=>'register')); 
    echo $this->Form->input('username'); 
    echo $this->Form->input('email');    
    echo $this->Form->end('Register');  
 ?>

when i am click the register button above code is not showing the error messages.

Upvotes: 1

Views: 1110

Answers (4)

Nemesis
Nemesis

Reputation: 17

What you actually want is to show the validation errors, so instead of

$this->flash('register fail');

Do

$this->Session->setflash($this->User->validationErrors);
$this->redirect('/users/register');

Upvotes: 0

user3301561
user3301561

Reputation:

Finally i got the answer to my question. i.e

User.php

<?php
class User extends AppModel
{

 public $validate= array(
                'username'=>array(
                        'rule'=>'notEmpty',
                        'required'=>false,
                        'message'=>'Enter your name'
                ),
                'email' => array(
                        'rule' => 'notEmpty',
                        'required'=>false,
                        'message' => 'Please enter your email'
                )
               );

}

?>

UsersController.php

<?php
    class UsersController extends AppController {

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

    public function register() {
         $this->User->create();
         if ($this->User->save($this->request->data)) {

          $this->redirect(array('controller'=>'users', 'action'=>'welcome'));
         }
         else{
       echo"register fail";
         }
   }
   public function welcome(){

    $this->flash('register successfully','/users/register');
   }
}
?>

register.ctp

<?php  
    echo $this->Form->create('User'); 
    echo $this->Form->input('username',array('required'=>'false')); 
    echo $this->Form->input('email',array('required'=>'false'));    
    echo $this->Form->end('Register');  
 ?>

this is the correct answer for above asking question.The main mistake done in my above question was i am given required'=>'true.',now given required'=>'false.' in register.ctp class.now problem resolved.

Upvotes: 1

Louis Kgole
Louis Kgole

Reputation: 52

Try this: add a helper

var $helpers = 'session';

then add the flash message as

$this->Session->setFlash(__('error'));

Upvotes: 0

Miheretab Alemu
Miheretab Alemu

Reputation: 976

You need to change you register action in user controller like this:

    public function register() {
         $this->User->create();
         if ($this->User->save($this->request->data)) { //this way it automatically save or show error message if there is one
            $this->flash('register success');
         }
         else{
            $this->flash('register fail');
         }
   }

Hope it helps.

Upvotes: 0

Related Questions