Support
CakePHP Version: 3.0
Platform and Target: My Sql
What you did
I need a help in writing unit test cases for validation rules, I have writte the validation rules in controller
And this is how my controller looks like
public function add() {
$this->set([‘mode’ => ADD_MODE, ‘_serialize’ => [‘mode’]]);
if ($this->request->is(‘post’)) {
$formData = $this->request->data;
// Form validation helper - Surveyid exists
$validateSurveyId = function($value) {
$data = $this->Project->getSurveyId($value);
return (!empty($data)) ? false : true;
};
// Validate form
$validator = new Validator();
$validator
->requirePresence(‘survey_id’, __(‘msg_required’))
->notEmpty(‘survey_id’, __(‘msg_required’))
->maxlength(‘survey_id’, 32, __(‘msg_maxlength’))
->add(‘survey_id’, [
‘value’ => [‘rule’ => [$validateSurveyId], ‘message’ => __(‘msg_surveyid_exsist’)],
‘format’ => [‘rule’ => [‘alphaNumeric’], ‘message’ => __(‘msg_surveyid_format’)]]);
$validator
->requirePresence(‘operator’, __(‘msg_required’))
->notEmpty(‘operator’, __(‘msg_required’))
->maxlength(‘operator’, 100, __(‘msg_maxlength’));
$validator
->requirePresence(‘title’, __(‘msg_required’))
->notEmpty(‘title’, __(‘msg_required’))
->maxlength(‘title’, 255, __(‘msg_maxlength’));
$validator
->requirePresence(‘company_name’, __(‘msg_required’))
->notEmpty(‘company_name’, __(‘msg_required’))
->maxlength(‘company_name’, 100, __(‘msg_maxlength’));
$validator
->requirePresence(‘department_name’, __(‘msg_required’))
->notEmpty(‘department_name’, __(‘msg_required’))
->maxlength(‘department_name’, 100, __(‘msg_maxlength’));
$validator
->requirePresence(‘name’, __(‘msg_required’))
->notEmpty(‘name’, __(‘msg_required’))
->maxlength(‘name’, 100, __(‘msg_maxlength’));
$validator
->requirePresence(‘air_id’, __(‘msg_required’))
->notEmpty(‘air_id’, __(‘msg_required’))
->maxlength(‘air_id’, 32, __(‘msg_maxlength’));
$validator
->requirePresence(‘email’)
->notEmpty(‘email’, __(‘msg_required’))
->maxlength(‘email’, 255, __(‘msg_maxlength’))
->add(‘email’, ‘validFormat’, [
‘rule’ => [‘email’],
‘message’ => __(‘msg_email_format’)
]);
$errors = $validator->errors($formData);
if (!empty($errors)) {
$this->set([‘errors’ => $errors, ‘_serialize’ => [‘errors’]]);
return;
}
$project_id = $this->Project->putProject($formData[‘survey_id’], $formData[‘title’], $formData[‘operator’], PUBLISH_STATUS_DELIVERY, SURVEY_STATUS_NEW, PROJECT_STATUS_ACTIVE, $this->Auth->user(‘id’), $this->Auth->user(‘id’));
$date = date(‘Y-m-d H:i:s’);
$this->ProjectCustomers->putProjectCustomer($project_id, $formData[‘air_id’], $formData[‘email’], $formData[‘name’], $formData[‘company_name’], $formData[‘department_name’], SYS_ADMIN, PROJECT_CUSTOMER_STATUS_ACTIVE, $date, $date);
return $this->redirect([‘action’ => ‘view’, $project_id]);
}