【问题标题】:Dynamically change CakePHP 3 custom validation rule message动态更改 CakePHP 3 自定义验证规则消息
【发布时间】:2017-04-12 01:33:06
【问题描述】:

我有一个自定义验证方法,可以检查字段中的标记值,以确保它们都存在于内容中。该方法工作正常,当所有标记都存在于内容中时它验证为 OK,并在缺少一个或多个标记时引发验证错误。

当我将自定义验证规则附加到validationDefault() 中的表时,我可以轻松地应用验证错误消息来声明缺少某些令牌。但是,我真正想做的是设置一个验证消息,以反映哪些令牌尚未设置。如何在 CakePHP 3 中动态设置验证消息?在 CakePHP 2 中,我曾经使用 $this->invalidate() 来应用适当的消息,但这似乎不再是一个选项。

我的代码看起来像这样(我已经去掉了我的实际令牌检查,因为它与这里的问题无关):-

public function validationDefault(Validator $validator)
{
    $validator
        ->add('content', 'custom', [
            'rule' => [$this, 'validateRequiredTokens'],
            'message' => 'Some of the required tokens are not present'
        ]);

    return $validator;
}

public function validateRequiredTokens($check, array $context)
{
    $missingTokens = [];

    // ... Check which tokens are missing and keep a record of these in $missingTokens ...

    if (!empty($missingTokens)) {
        // Want to update the validation message here to reflect the missing tokens.
        $validationMessage = __('The following tokens are missing {0}', implode(',', $missingTokens));

        return false;
    }

    return true;
}

【问题讨论】:

    标签: php validation cakephp cakephp-3.3


    【解决方案1】:

    不确定这是否是自 burzum 回答以来有所改进的地方。

    但这实际上很简单。自定义验证规则可以返回 true(如果验证成功)、false(如果不成功),但您也可以返回字符串。该字符串被解释为 false,但用于错误消息。

    所以你基本上可以这样做:

    public function validationDefault(Validator $validator)
    {
        $validator
        ->add('content', 'custom', [
            'rule' => [$this, 'validateRequiredTokens'],
            'message' => 'Some of the required tokens are not present'
        ]);
    
        return $validator;
    }
    
    public function validateRequiredTokens($check, array $context)
    {
         $missingTokens = [];
    
         if (!empty($missingTokens)) {
             // Want to update the validation message here to reflect the missing tokens.
             $validationMessage = __('The following tokens are missing {0}', implode(',', $missingTokens));
    
             //Return error message instead of false
             return $validationMessage;
         }
    
         return true;
    }
    

    来自食谱:

    Conditional/Dynamic Error Messages

    【讨论】:

      【解决方案2】:

      Read the API docs.

      复制粘贴:

      EntityTrait::errors()

      为字段或字段列表设置错误消息。在没有第二个参数的情况下调用它会返回指定字段的验证错误。如果不带参数调用,则返回存储在此实体和任何其他嵌套实体中的所有验证错误消息。

      // Sets the error messages for a single field
      $entity->errors('salary', ['must be numeric', 'must be a positive number']);
      
      // Returns the error messages for a single field
      $entity->errors('salary');
      
      // Returns all error messages indexed by field name
      $entity->errors();
      
      // Sets the error messages for multiple fields at once
      $entity->errors(['salary' => ['message'], 'name' => ['another message']);
      

      http://api.cakephp.org/3.3/class-Cake.Datasource.EntityTrait.html#_errors

      【讨论】:

        猜你喜欢
        • 2011-04-29
        • 2021-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多