【问题标题】:How to display validation errors in CakePHP 3.6?如何在 CakePHP 3.6 中显示验证错误?
【发布时间】:2018-05-23 20:32:16
【问题描述】:

我在使用自定义验证器显示表单的验证错误时遇到问题。 调试方法显示的错误确实存在,只是不会显示在表单中。

我希望能够在该字段下方(或上方,或任何地方)显示错误消息。

我尝试过的

好吧,documentation 确实说明了:

当使用 View\Helper\FormHelper::control() 时,错误由 默认,所以你不需要使用 isFieldError() 或调用 error() 手动。

尽管如此,我在表单中添加了以下内容(就在电子邮件控件下方),但并没有做更多的事情。没有消息显示。

if ($this->Form->isFieldError('email')) {
    echo $this->Form->error('email', 'Yes, it fails!');
}

我还在 SO 上找到了几个关于这个问题的问题和答案,但它们看起来已经过时(从 '09 到 '13)并且似乎不符合今天的 CakePHP 语法。

我做了什么

用户/forgot_password.ctp

<?= $this->Form->create() ?>
<?= $this->Form->control('email', ['type' => 'email']) ?>
<?= $this->Form->button(__('Reset my password')) ?>
<?= $this->Form->end() ?>

UsersController.php

(注意具体的验证集,如documentation中所述)

public function forgotPassword()
{
    if ($this->request->is('post')) {

        $user = $this->Users->newEntity($this->request->getData(), ['validate' => 'email']);
        if ($user->errors()) {
            debug($user->errors()); // <- shows the validation error
            $this->Flash->error(__('An error occurred.'));
        } else {

          // ... procedure to reset password (which works fine!) and redirect to login...

          return $this->redirect(['action' => 'login']);
        }
    }
}

UsersTable.php

public function validationEmail(Validator $validator)
{
    $validator
        ->email('email')
        ->notEmpty('email', __('An email address is required.'));

    return $validator;
}

它是什么样子的

更新

感谢@ndm 评论,这是显示错误的正确方法。

UsersController.php

public function forgotPassword()
{
    // user context for the form
    $user = $this->Users->newEntity();

    if ($this->request->is('post')) {

        $user = $this->Users->patchEntity(§user, $this->request->getData(), ['validate' => 'email']); <- validation done on patchEntity
        if ($user->errors()) {
            $this->Flash->error(__('An error occurred.'));
        } else {

          // ... procedure to reset password and redirect to login...

          return $this->redirect(['action' => 'login']);
        }
    }

    // pass context to view
    $this->set(compact('user'));
}

在视图中forgotPassword.ctp

<?= $this->Form->create($user) ?>

【问题讨论】:

标签: php validation cakephp-3.x


【解决方案1】:
//modify your function as below 
public function forgotPassword()
{
    if ($this->request->is('post')) {

    $user = $this->Users->newEntity($this->request->getData(), ['validate' => 'email']);
    if ($user->getErrors()) {
        debug($user->getError('email')); // <- shows the validation error
        $this->Flash->error(__($user->getError('email')['_empty']));
    } else {

      // ... procedure to reset password (which works fine!) and redirect to login...

      return $this->redirect(['action' => 'login']);
    }
  }
}

【讨论】:

  • 这不是一个好主意,从每次更改规则时都必须调整控制器,到机器无法读取表单错误。控制器没有理由参与其中,表单助手可以在输入正确数据时很好地处理错误。
  • 这并没有真正回答我关于如何在验证失败的字段下(或下一个)显示消息的问题。从用户体验的角度来看,我认为最好在失败的字段旁边显示验证错误,而不是在闪存消息中。正如@ndm 所提到的,这是一种不好的做法,因为每次规则或表单更改时都需要更新控制器方法。
猜你喜欢
  • 2015-09-14
  • 2016-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-04
  • 1970-01-01
  • 2011-09-08
  • 1970-01-01
相关资源
最近更新 更多