Pankaj Sharma
Pankaj Sharma

Reputation: 112

CakePHP 3.x add different validation rules for frontend controller

I would like to create the custom validation function in cakephp 3.x In CakePHP I've frontend and backend panels. Frontend have more fields comparing the backend panel. public function validationDefault(Validator $validator). This validation function is call after the action call.

But I want to call a different validation function which will be specify in the controller and check the validation for other fields that are on frontend.

For Example, I have a field name 'company', but it is not in backend. I want to add require validation rule on server site for frontend.

Following function is created on the model:

public function companyValidation(Validator $validator) {
    $validator
        ->requirePresence('company', 'create')
        ->notEmpty('company');
    return $validator;
}

How can I call to companyValidation() function in the controller?

Upvotes: 0

Views: 1019

Answers (1)

Greg Schmidt
Greg Schmidt

Reputation: 5099

First, rename your function from companyValidation to validationCompany. Then, when you are patching your entity, use $x = $this->Table->patchEntity($x, $this->request->data, ['validate' => 'company']);

This is covered in the "Validating data" section of the manual, particularly Using and Different Validation Set.

Upvotes: 2

Related Questions