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

无法在cakephp 2.x验证中显示错误消息,php,cakephp,Php,Cakephp,我正在开发简单的注册表表单。并开发模型、控制器、视图类。但我无法在我的应用程序中显示错误消息 User.php(模型类) userscocontroller.php <?php class UsersController extends AppController { public $helpers = array('Html', 'Form'); array('action' => 'edit') public function regist

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

User.php(模型类)


userscocontroller.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');
            }
}
}
?>
<?php  
    echo $this->Form->create('User',array('action'=>'register')); 
    echo $this->Form->input('username'); 
    echo $this->Form->input('email');    
    echo $this->Form->end('Register');  
 ?>
<?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'
                )
               );

}

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

寄存器.ctp

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

}

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


单击上面的注册按钮时,代码不会显示错误消息。

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

    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');
         }
   }
希望有帮助。

试试这个: 添加助手

var $helpers = 'session';
然后将flash消息添加为

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

最后我得到了我问题的答案。i、 e

User.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');
            }
}
}
?>
<?php  
    echo $this->Form->create('User',array('action'=>'register')); 
    echo $this->Form->input('username'); 
    echo $this->Form->input('email');    
    echo $this->Form->end('Register');  
 ?>
<?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'
                )
               );

}

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

userscocontroller.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');
            }
}
}
?>
<?php  
    echo $this->Form->create('User',array('action'=>'register')); 
    echo $this->Form->input('username'); 
    echo $this->Form->input('email');    
    echo $this->Form->end('Register');  
 ?>
<?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'
                )
               );

}

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

寄存器.ctp

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

}

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


这是上述问题的正确答案。我在上述问题中犯的主要错误是我被赋予了必需的'=>'真',现在在register.ctp类中被赋予了必需的'=>'假。现在问题解决了。

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

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


它会显示错误消息,如语法错误、意外的“if”(T_if)它不会显示错误消息。它只会在必填字段中工作\my bad它是分号,我编辑了答案,请再试一次。我尝试过了,它会工作。但是,当我单击注册表按钮时,错误消息不会显示。只有必填属性工作。