【问题标题】:In cakephp, how do I access model fields inside custom validations of other model fields?在 cakephp 中,如何访问其他模型字段的自定义验证中的模型字段?
【发布时间】:2019-03-22 10:05:53
【问题描述】:

我在 cakephp 1.3 中有一个模型。我想为我的 example_id 字段设置自定义验证,它使用 example_type 字段的值。如果 example_type 是特定类型(1,而不是 2 或 3),我只想让验证失败。

这是验证变量的示例:

var $validate = array(
    'example_type' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            'message' => 'Example Type: This is a required field'
        ),
        'numeric' => array(
            'rule' => array('numeric'),
            'message' => 'Example Type: Please choose from the drop down.'
        ),
        'isSpecificType' => array(
            'rule' => array('isSpecificType')
        )
    ),
    'example_id' => array(
        'range' => array(
            'rule' => array('range', -1, 2147483648),
            'message' => 'Example ID: Enter number from 0 to 2147483647',
            'allowEmpty' => true
        ),
        'numeric' => array(
            'rule' => array('numeric'),
            'message' => 'Example ID: Numeric only.',
            'allowEmpty' => true
        ),
        'is_type' => array(
            'rule' => array('exampleNotEmpty', $specific_type_var),
            'message' => 'Example ID: You must have an example id if the example type is 1'
        )
    )
);

然后我将有两个自定义验证函数,isSpecificType 和 exampleNotEmpty。请参阅以下内容:

function isSpecificType($check) {
    $specific_type_var = $check['example_type'];
    return true;
};

function exampleNotEmpty($check, $type) {
    if ($type === 1) {
        if (is_null($check['example_id'])) {
            return false; //by the way, i realize is_null may not be correct here, but I have the capability to correct this after I am able to run it a few times
        }
    }
    return true;
};

最后,在我的 AppModel 顶部,就在 $validate 声明之前,我创建了变量 $specific_type_var。请参阅以下内容:

var $specific_type_var = 1;

所以,现在,我的问题。我在这一行收到错误:

'rule' => array('exampleNotEmpty', $specific_type_var),

错误是:Parse error: syntax error, unexpected '$specific_type_var' (T_VARIABLE), expecting ')' in /cakephpproject/app/models/example.php on line ##

我在这篇文章中的最后一个问题是:我这样做的方式是否正确?我只是有语法错误吗?或者,我可以直接从另一个字段调用的自定义验证函数内部访问 example_type 字段吗?我在模型中做的事情是否可行,还是必须在控制器层做?

【问题讨论】:

    标签: php validation cakephp model-view-controller cakephp-1.3


    【解决方案1】:

    我遇到了一些问题。一个是我如何在 cakephp 模型中使用变量。其次是如何使用这些变量将字段传递给其他字段的自定义验证器。第三是如何将空字段传递给自定义验证器。

    回答第一个问题:重写构造函数允许您向模型添加变量。

    public function __construct($id = false, $table = null, $ds = null){
        parent::__construct($id, $table, $ds);
        $this->example_type_var = 1;
    }
    

    回答第二个问题:在您需要传递的字段上创建一个自定义验证器,并使用该函数进行设置。请注意,(我认为)规则何时发生是有顺序的。确保将您的自定义规则作为第一条规则,并将此字段放在您需要访问此字段值的第二个字段之前。

    var $validate = array(
        'example_type' => array(
            'updateActionne' => array(
                'rule' => array('_updateTheOleType')
            )
        )
    )
    

    然后在自定义验证器中:

    function _updateTheOleType($check) {
        $this->example_type_var = $check['example_type'];
        return true;
    }
    

    在第二个自定义验证器中,再次使用 $this->example_type_var。请参阅以下内容:

    function _exampleNotEmpty($check) {
        if ($this->example_type_var === '3') {
            if (empty($check['example_id']) && $check['example_id'] !== '0') {
                return false;
            }
        }
        return true;
    }
    

    回答第三个问题:我错误地认为 allowEmpty 是我需要将一个空字段传递给自定义验证器。这不是它的工作原理。 AllowEmpty 设置为 true 意味着 null 字段在规则上返回 true。您的自定义验证器甚至不会运行。 AllowEmpty 设置为 false 意味着 null 字段将无法通过验证,同样不会进入规则的自定义验证器函数。需要正确的属性。将 required 设置为 true 会强制规则始终运行。

    错误:

    var $validate = array(
        'example_id' => array(
            'exampleNotEmpty' => array(
                'rule' => array('_exampleNotEmpty'),
                'message' => 'Example ID: You must have an example ID if the event is example type.',
                'allowEmpty' => true
            )
        )
    )
    

    错误:

    var $validate = array(
        'example_id' => array(
            'exampleNotEmpty' => array(
                'rule' => array('_exampleNotEmpty'),
                'message' => 'Example ID: You must have an example ID if the event is example type.',
                'allowEmpty' => false
            )
        )
    )
    

    正确:

    var $validate = array(
        'example_id' => array(
            'exampleNotEmpty' => array(
                'rule' => array('_exampleNotEmpty'),
                'message' => 'Example ID: You must have an example ID if the event is example type.',
                'required' => true
            )
        )
    )
    

    【讨论】:

    • 所以我花了大约 8 个小时才弄明白。 Stackoverflow 和 IRC@cakephp 让我失望了。这应该很容易,但我没有圣人的耐心。
    猜你喜欢
    • 2012-06-09
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    • 2016-01-20
    • 2019-09-06
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    相关资源
    最近更新 更多