验证消息未显示在cakephp 2.4.x中

程序员

这是我在User.php中的验证规则

public $validate = array(
    'username' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'User name is required'
        ),
        'alphaNumeric'=>array(
            'rule' => 'alphaNumeric',
            'required' => true,
            'message' => 'Alphabets and numbers only'
        )
    ))

这是我的查看页面代码

<?php
      echo $this->Form->create('User');
      echo $this->Form->input('username', array('label' => 'Username'));
      echo $this->Form->input('email', array('label' => 'Email'));
      echo $this->Form->input('password', array('label' => 'Password'));
      echo $this->Form->submit('Sign Up');
      echo $this->Form->end();
?>

这是我的控制器代码

public function register() {
$this->layout = 'starter';
//debug($this->validationErrors);
if ($this->request->is('post')) {
    if ($this->User->validates()) {
        $this->User->save($this->request->data);
        $this->Session->setFlash(__('Please login your account'));
        $this->redirect('/users/login');
      } else {
        $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
      }
 }
}

但未显示验证消息。我的代码有什么问题?...

标记

您的代码是错误的。

if ($this->request->is('post')) {
    if ($this->User->validates()) {
        $this->User->save($this->request->data);

这是不可能的,因为在验证之前没有传递数据。

您需要首先传递数据,然后进行验证,然后选择保存(或一起保存并验证):

if ($this->request->is('post')) {
    if ($this->User->save($this->request->data)) {}

,请小心不要再次触发验证两次:

if ($this->request->is('post')) {
    $this->User->set($this->request->data);
    if ($this->User->validates()) {
        $success = $this->User->save(null, array('validate' => false));

但这是有据可查的。

仅当您确实需要分两步执行此操作时,后者才有意义。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

cakephp 4-身份验证2-未识别时如何显示消息

Angular(2/4/5/6)自定义验证器错误消息未显示

验证错误消息未显示为Angular 2中的自定义验证

在mvc4中单击按钮时未显示验证消息

Angular 4 中的模型驱动表单未显示验证错误

使用Flexbox以2x2网格的形式在ul中显示4张图像

在Spring Form验证中未显示给定的错误消息

Spring MVC验证器中未显示错误消息

表单验证错误消息未显示在ModelForm中

验证消息未显示为模型在ViewModel中的属性

Vuejs 中未显示密码字段的验证错误消息?

如何在.NET Core 2.0中显示数据注释错误消息?验证消息未显示

2x2 形式的 Bootstrap 4 显示按钮

CakePHP 2中的表单验证

如何强制在 QT WIDGET 的 2x2 VideoWall(4 台显示器)中拉伸内容?

如何始终在ZF2验证器中显示单个验证消息?

yii2 gridview搜索字段未触发验证,未显示错误消息

在CakePHP中显示错误消息

张量流中的4X + 2 = 0

Highchart未显示在cakephp中

在CodeIgniter 4中显示错误消息

ROS2 colcon 构建在 Windows 中未显示任何错误消息

如何使用Validators类在Angular2中显示用于电子邮件验证的不同验证消息?

如何在Log4J 2中修改消息?

如何创建这个序列 "x-^-1" "x-^-1" "x-^-1" "x-^-1" "x-^-2" "x-^-2" "x- ^-2" "x-^-3" "x-^-3" "x-^-4" 在 R 中?

MVC4验证摘要未显示

如何在角度4中显示来自自定义验证器功能的自定义错误消息?

Laravel 验证错误消息未显示在所需选项卡中

如何敬酒验证错误消息显示未选择微调器中的任何项目?