protected function _createValidator()
    {
        $config = $this->config();
        if ($config['validator'] instanceof Validator) {
            return $config['validator'];
        }

        $this->_controller->loadModel('Comment.Comments');
        if ($this->_controller->request->is('userLoggedIn')) {
            // logged user posting
            $validator = $this->_controller->Comments->validationDefault(new Validator());
            $validator
                ->requirePresence('user_id')
                ->notEmpty('user_id', __d('comment', 'Invalid user.'))
                ->add('user_id', 'checkUserId', [
                    'rule' => function ($value, $context) {
                        if (!empty($value)) {
                            $valid = TableRegistry::get('User.Users')->find()
                                ->where(['Users.id' => $value, 'Users.status' => 1])
                                ->count() === 1;

                            return $valid;
                        }

                        return false;
                    },
                    'message' => __d('comment', 'Invalid user, please try again.'),
                    'provider' => 'table',
                ]);
        } elseif ($this->config('settings.allow_anonymous')) {
            // anonymous user posting
            $validator = $this->_controller->Comments->validator('anonymous');
        } else {
            // other case
            $validator = new Validator();
        }

        if ($this->config('settings.use_captcha')) {
            $validator
                ->add('body', 'humanCheck', [
                    'rule' => function ($value, $context) {
                        return CaptchaManager::adapter()->validate($this->_controller->request);
                    },
                    'message' => __d('comment', 'We were not able to verify you as human. Please try again.'),
                    'provider' => 'table',
                ]);
        }

        return $validator;
    }
protected function _createValidator()
    {
        $config = $this->config();
        if ($config['validator'] instanceof Validator) {
            return $config['validator'];
        }

        $this->_controller->loadModel('Comment.Comments');
        if ($this->_controller->request->is('userLoggedIn')) {
            // logged user posting
            $validator = $this->_controller->Comments->validationDefault(new Validator());
            $validator
                ->requirePresence('user_id')
                ->notEmpty('user_id', __d('comment', 'Invalid user.'))
                ->add('user_id', 'checkUserId', [
                    'rule' => function ($value, $context) {
                        if (!empty($value)) {
                            $valid = TableRegistry::get('User.Users')->find()
                                ->where(['Users.id' => $value, 'Users.status' => 1])
                                ->count() === 1;

                            return $valid;
                        }

                        return false;
                    },
                    'message' => __d('comment', 'Invalid user, please try again.'),
                    'provider' => 'table',
                ]);
        } elseif ($this->config('settings.allow_anonymous')) {
            // anonymous user posting
            $validator = $this->_controller->Comments->validator('anonymous');
        } else {
            // other case
            $validator = new Validator();
        }

        if ($this->config('settings.use_captcha')) {
            $validator
                ->add('body', 'humanCheck', [
                    'rule' => function ($value, $context) {
                        return CaptchaManager::adapter()->validate($this->_controller->request);
                    },
                    'message' => __d('comment', 'We were not able to verify you as human. Please try again.'),
                    'provider' => 'table',
                ]);
        }

        return $validator;
    }
protected function _createValidator ( ) { $ config = $ this -> config ( ) ; if ( $ config [ 'validator' ] instanceof Validator ) { return $ config [ 'validator' ] ; } $ this -> _controller -> loadModel ( 'Comment.Comments' ) ; if ( $ this -> _controller -> request -> is ( 'userLoggedIn' ) ) { // logged user posting $ validator = $ this -> _controller -> Comments -> validationDefault ( new Validator ( ) ) ; $ validator -> requirePresence ( 'user_id' ) -> notEmpty ( 'user_id' , __d ( 'comment' , 'Invalid user.' ) ) -> add ( 'user_id' , 'checkUserId' , [ 'rule' => function ( $ value , $ context ) { if ( ! empty ( $ value ) ) { $ valid = TableRegistry :: get ( 'User.Users' ) -> find ( ) -> where ( [ 'Users.id' => $ value , 'Users.status' => 1 ] ) -> count ( ) === 1 ; return $ valid ; } return false ; } , 'message' => __d ( 'comment' , 'Invalid user, please try again.' ) , 'provider' => 'table' , ] ) ; } elseif ( $ this -> config ( 'settings.allow_anonymous' ) ) { // anonymous user posting $ validator = $ this -> _controller -> Comments -> validator ( 'anonymous' ) ; } else { // other case $ validator = new Validator ( ) ; } if ( $ this -> config ( 'settings.use_captcha' ) ) { $ validator -> add ( 'body' , 'humanCheck' , [ 'rule' => function ( $ value , $ context ) { return CaptchaManager :: adapter ( ) -> validate ( $ this -> _controller -> request ) ; } , 'message' => __d ( 'comment' , 'We were not able to verify you as human. Please try again.' ) , 'provider' => 'table' , ] ) ; } return $ validator ; }
Creates a validation object on the fly.

@return \Cake\Validation\Validator
Creates a validation object on the fly .