CakePHP可以是行为共享模型public $ validate [英] CakePHP Can a Behavior share Model public $validate

查看:275
本文介绍了CakePHP可以是行为共享模型public $ validate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了一个模型和行为,其中行为包含一些自定义验证规则方法,如匹配,以确保两个字段具有相同的值,这是可行的,但有一些非常通用的$ validate规则,我想重用在不同的模型中的东西,如密码。当我把$ validate数组放入我的ValidateBehavior并在我的控制器中调用validate时,它似乎没有任何验证,即使字段不正确,一切都通过。

I've setup a model and behavior where the behavior contains some custom validation rule methods like match for ensuring two fields have identical values, and this works, but there are some very generic $validate rules that I'd like to reuse in different models for things like passwords. When I put the $validate array into my ValidateBehavior and invoke validate in my controller, it doesn't appear to hit any validations and everything passes even though the fields are incorrect.

我可以使用$ validate在我的行为有我的模型使用它和工作,当我调用validate的一个职位?此帖子似乎表明我可以,但它不工作。

Can I use $validate in my behavior have my model use it and work when I invoke validate on a post? This post seems to suggest I can, but it isn't working.

MODEL:
class Admin extends AppModel
{
    public $name = 'Admin';
    public $actsAs = [ 'Validate' ];
}

BEHAVIOR:
class ValidateBehavior extends ModelBehavior
{
    public $validate = [
        'currentpassword' => [
            'notEmpty'  => [
                'rule'    => 'notEmpty',
                'message' => 'Current password is required.'
            ],
            'minLength' => [
                'rule'    => [ 'minLength', '8' ],
                'message' => 'Passwords must be at least 8 characters long.'
            ]
        ],
        'newpassword'     => [
            'notEmpty'  => [
                'rule'    => 'notEmpty',
                'message' => 'New password is required.'
            ],
            'minLength' => [
                'rule'    => [ 'minLength', '8' ],
                'message' => 'Passwords must be at least 8 characters long.'
            ],
            'match'     => [
                'rule'    => [ 'match', 'confirmpassword' ],
                'message' => 'New password must match the confirmation password'
        ]

    ],
    ... etc

    public function match( Model $Model, $check, $compareTo )
    {
        $check = array_values( $check )[ 0 ];
        $compareTo = $this->data[ $this->name ][ $compareTo ];

        return $check == $compareTo;
    }
}

function changepassword() {

    $post = $this->request->data;

    // Was there a request to change the user's password?
    if ($this->request->is( 'post' ) && !empty( $post )) {

        // Set and validate the post request
        $this->Admin->set( $this->request->data );

        // Set of validation rules to be run
        $validateRules = [
            'fieldList' => [
                'currentpassword',
                'newpassword',
                'confirmpassword'
            ]
        ];

        if ($this->Admin->validates( $validateRules )) {

            // makes it here even though all fields are empty when
            // the validation rules are in the behavior otherwise
            // when left in the model and the behavior only has 
            // the methods like match this all works
            ...etc
        }
        ...etc
}


推荐答案


我可以在我的行为中使用$ validate,我的模型使用它和工作,当我调用validate的一个职位?

Can I use $validate in my behavior have my model use it and work when I invoke validate on a post?

不是你这样做的方式。这是基本的OOP:你不能在B类中神奇地调用A类的属性,而不传递A的实例或继承A.但是这是你期望在代码中。这不会对某个行为有效,并且不会在任何php脚本中工作。

Not the way you try to do it. This is basic OOP: You can't magically call a property of class A inside class B without passing an instance of A or inheriting A. But that's what you expect in your code. This won't work for a behavior and won't work in any php script.

我建议您阅读CakePHP框架如何工作以及行为如何工作。此书中的还有示例。也许关于OOP以及。

I recommend you to read about how the CakePHP framework works and how behaviors work. There are examples in the book as well. Maybe about OOP as well. Answer that questions for yourself: How can the behavior even know about the validation rules of the model?

在Cake2中,每个行为方法都将模型作为第一个参数。在行为的setup()方法中改变验证规则。

In Cake2 each behaviors method takes the model as first argument. Alter the validation rules as you like in the setup() method of the behavior .

public function setup(Model $Model, $settings = array()) {
    $Model->validate; // Do something with it here
}

更好地将它们合并在那里。所以现在B类(行为)有一个A(模型)的实例可以改变它的公共属性。

Instead of assigning the rules there it might be better to merge them in there. So now class B (behavior) has an instance of A (model) available and can alter it's public properties.

我建议你阅读关于行为的章节和php手册的OOP部分。

I recommend you to read the chapter about behaviors and the php manuals OOP section.

这篇关于CakePHP可以是行为共享模型public $ validate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆