dongwen2794 2016-11-28 10:48
浏览 44
已采纳

动态更改CakePHP 3自定义验证规则消息

I've got a custom validation method that checks a field for token values to ensure that they are all present in the content. The method works fine, it validates as OK when all the tokens exist in the content and throws a validation error when one or more tokens are missing.

I can easily apply a validation error message to state that some of the tokens are missing when I attach my custom validation rule to my table in validationDefault(). However, what I really want to do is set a validation message that reflects which of the tokens haven't been set. How can I dynamically set the validation message in CakePHP 3? In CakePHP 2 I used to use $this->invalidate() to apply an appropriate message, but this no longer appears to be an option.

My code looks like this (I've stripped out my actual token check as it isn't relevant to the issue here):-

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;
}
  • 写回答

1条回答 默认 最新

  • duanjia6959 2016-11-28 11:04
    关注

    Read the API docs.

    Copy and paste:

    EntityTrait::errors()

    Sets the error messages for a field or a list of fields. When called without the second argument it returns the validation errors for the specified fields. If called with no arguments it returns all the validation error messages stored in this entity and any other nested entity.

    // 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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值