CakePHP Can a Behavior share Model public $validate

回眸只為那壹抹淺笑 提交于 2019-12-23 01:04:21

问题


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.

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
}

回答1:


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

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.

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?

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
}

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.

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



来源:https://stackoverflow.com/questions/32570721/cakephp-can-a-behavior-share-model-public-validate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!