CakePHP 自定义验证与错误消息中的变量

CakePHP custom validation with a variable in the error message(CakePHP 自定义验证与错误消息中的变量)
本文介绍了CakePHP 自定义验证与错误消息中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

下午好.

我有一个模型,它有一个名为百分比"的字段.所有类似模型的百分比总和不能超过 100%.验证过程中会对此进行检查.

I have a Model that has a field called "percentage". All similar Models cannot have their percentages add up to more than 100%. Checking for this is handled during validation.

我需要验证错误消息来说明用户剩余的空间"百分比.例如,如果所有 MyModel 已经有 80% 的总数,而用户尝试创建一个百分比为 40% 的新 MyModel,则错误消息会显示您的百分比太高.您只剩下 20% 了".

I need the validation error message to say how much percentage "room" the user has left. For example, if all MyModels already have a total of 80% and the user tries to create a new MyModel with a percentage of 40%, the error message would say, "Your percentage is too high. You only have 20% remaining."

问题是我不知道如何将变量放入验证错误消息中.

The problem is that I don't know how to put a variable in a validation error message.

在 MyModel.php 中:

In MyModel.php:

public $validate = array(
    'percentage' => array(
        'rule' => array('confirmValidPercentage', 'percentage'),
        'message' => 'foo',
        'required' => true,
    ),
);

public function confirmValidPercentage($data) {
    $percentage = floatval($data['percentage']);

    $total = 0.00;
    $weights = $this->find('all', array('recursive'=>-1));
    foreach ($weights as $weight) {
        $total += floatval($weight[$this->name]['percentage']);
    }

    if ($total + $percentage > 100) {
        // handle the error variable here
        return false;
    }
    else {
        return true;
    }
}

我试过了:

$this->validate['percentage']['message'] = 'You have '.(100-$total).'% remaining';

但是这里设置的消息元素不会覆盖原始消息——错误消息仍然是foo".我尝试从 $validation 数组中完全删除消息元素,但它默认为父名称,即百分比".我试过了:

but the message element set here does not override the original message - the error message remains 'foo'. I tried removing the message element form the $validation array altogether, but it defaults to the parent name, i.e. 'percentage'. I tried:

unset($this->validate['percentage']['message']);

之前设置验证消息但结果是一样的.

before setting the validate message but the result is the same.

有谁知道如何在验证错误消息中返回一个变量?非常感谢.

Does anyone know how to return a variable in a validation error message? Thanks a lot.

推荐答案

那其实很简单.只返回错误字符串!

thats actually pretty easy. just return the error string!

return 'You have ' . ...;

这篇关于CakePHP 自定义验证与错误消息中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)