无法在cakephp 2.x验证中显示错误消息

时间:2014-04-08 11:37:30

标签: php cakephp

我正在开发简单的寄存器表单。并开发模型,控制器,视图类。但我无法在我的应用程序中显示错误消息。

User.php(模型类)

<?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');  
 ?>

当我点击上面的注册按钮代码时没有显示错误消息。

4 个答案:

答案 0 :(得分:0)

您需要在用户控制器中更改注册操作,如下所示:

    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');
         }
   }

希望它有所帮助。

答案 1 :(得分:0)

试试这个: 添加助手

var $helpers = 'session';

然后将Flash消息添加为

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

答案 2 :(得分:0)

最后我得到了我的问题的答案。即

<强> 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');  
 ?>

这是上述问题的正确答案。上述问题中的主要错误是我被赋予必需&#39; =&gt;&#39; true。&#39; ,现在,在register.ctp class.now问题解决了 required&#39; =&gt;&#39; false。&#39;

答案 3 :(得分:0)

您真正想要的是显示验证错误,而不是

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

待办事项

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