Cakephp-3 conditional notEmpty validation behaviour - validation

I need to do a conditional validation on a field: if other_field = 1 then this_field = notBlank. I can not find a way to do this.
My validator in table class:
public function validationDefault(Validator $validator) {
$validator->allowEmpty('inst_name');
$validator->add('inst_name', [
'notEmpty' => [
'rule' => 'checkInstName',
'provider' => 'table',
'message' => 'Please entar a name.'
],
'maxLength' => [
'rule' => ['maxLength', 120],
'message' => 'Name must not exceed 120 character length.'
]
]);
return $validator;
}
public function checkInstName($value, array $context) {
if ($context['data']['named_inst'] == 1) {
if ($value !== '' && $value !== null) {
return true;
} else {
return false;
}
} else {
return true;
}
}
Trouble here is, if i note, in the start of the method, that the field is allowed to be empty, when the entered value is empty, Cake doesn't run through any of my validations, because it's empty and allowed to be such. If i do not note that the field can be empty, Cake just runs "notEmpty" validation before my custom validation and outputs "This field cannot be left empty" at all time when it's empty.
How do i make Cake go through my conditional "notEmpty" validation?
I did try validation rule with 'on' condition with the same results.

Successfully tested, this might help you and others. CakePHP 3.*
$validator->notEmpty('event_date', 'Please enter event date', function ($context) {
if (!empty($context['data']['position'])) {
return $context['data']['position'] == 1; // <-- this means event date cannot be empty if position value is 1
}
});
In this example Event Date cannot be empty if position = 1 . You must put this condition if (!empty($context['data']['position'])) because the $context['data']['position'] value only will exist after the user click submit button. Otherwise you will get notice error.

Related

Cakephp Custom Validation Method Returning False but Saves and Displays no Message

I have the following validator under ValidationDefault to check whether the effective_until date is after the effective_on date. If false it should display the message.
$validator
->add('effective_until', 'custom', ['rule' => 'checkEffectiveDateRange', 'provider' => 'table', 'message' => 'The effective until date must come after the effective on date.']);
I have the following custom function in the same table but even when I purposely set the effective_until date to be earlier than the effective_on date it saves the data and doesn't display the validation error message. Am I doing something wrong here?
public function checkEffectiveDateRange($check, $context)
{
if(array_key_exists('newRecord',$context))
{
return strtotime($context['data']['effective_on']) < strtotime($check);
}
else
{
return strtotime($context['effective_on']) < strtotime($check);
}
}
For anyone that may need this I resolved this by changing the validator to below
Validator
public function validationDefault(Validator $validator): Validator
{
$validator
->add('effective_until', 'custom',
['rule' => function($check, $context) {
if($this->checkEffectiveDateRange($check, $context)) {
return true;
}
return false;
},
'message' => 'The effective until date must come after the effective on date.']);
return $validator;
}

how to create Custom Validation Rule for during Update a record in CakePHP

I have added a rule to check the previous password on update user record, but the rule also applies to creating a record, I have added 'update' but still, it's not working.
$validator
->scalar('password')
->maxLength('password', 25)
->notEmpty('password', 'Password is required', 'create')
->allowEmpty('password', 'update')
->add('password', 'validFormat', [
'rule' => ['custom', '/^(?=.*[!#$])(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{6,16}$/i'],
'message' => __('Minimum 6 character & alphanumeric with one symbol(!#$) is Required.'),
'allowEmpty' => true
])
->add('password', 'custom', [
'rule' => function($value, $context){
$user = $this->get($context['data']['id']);
if ($user) {
if (!(new DefaultPasswordHasher)->check($value, $user->password)) {
return true;
}
}
return false;
},
'message' => 'You cannot use your previous password',
'allowEmpty' => true
], 'update');
Adding updated does not do anything as it is not something that CakePHP supports there.
If you read Custom Validation Rules you will see that you can use $context['newRecord'] to determine if this is an update or create.
So, the first line in your validation function could be something like
if ($context['newRecord']){
return true; //new passwords are always valid!
}
Alternative way would be to check the $user variable and add an else branch
if($user){
//you already have code here
}else{
//add this else to return TRUE because for new user, new password is OK
return true;
}

How to exclude a perticular field from unique validation in edit mode in cakephp3.0 validation

I want to validate a field called survey_id which is an input from user for uniqueness. It is working properly and giving the correct response when adding the new record, but when I tried to edit this record it is giving an error [unique] => Provided value already exist. So what I want is to exclude the survey_id of the current record from uniqueness check and if user input some other value for survey_id it should check for uniqueness search.
Currently I am using the CakePHP 3.0 validation with on create validation. Here is the validation rule that I am using:
validator
->requirePresence('survey_id', __('msg_required'))
->notEmpty('survey_id', __('msg_required'))
->maxlength('survey_id', 32, __('msg_maxlength'))
->add('survey_id', 'unique', ['rule' => ['validateUnique',['id']], 'provider' => 'table', 'message' => 'Provided value already exist', 'on'=>'create']);
return $validator;
Is there anything wrong with this code?
Thanks in advance.
`
It will work with this validation rule
$validator
->requirePresence('survey_id', __('msg_required'))
->notEmpty('survey_id', __('msg_required'))
->maxlength('survey_id', 32, __('msg_maxlength'))
->alphaNumeric('survey_id', __('msg_surveyid_format'))
->add('survey_id', 'custom', [
'rule' => function ($value, $context) {
if (!empty($context['data']['projectId'])) { $values = array($context['data']['projectId']); } else { $values = array(); }
$data = $this->getSurveyId($value, $values);
return (!empty($data)) ? false : true;
},
'message' => __('msg_surveyid_exsist')]);
return $validator;
}
public function getSurveyId($surveyId = null, $exclude = null) {
$where = array('p.survey_id' => $surveyId);
if (!empty($exclude) && is_array($exclude)) {
$where[] = array('p.id NOT IN' => $exclude);
}
return $this->db->newQuery()
->select('*')
->from(['p' => 'projects'])
->where($where)
->execute()
->fetch('assoc');
}

In CakePHP3 how do I create a custom model rule which validates that a time is after another time in the same table?

Columns (both datatype time):
Start
End
End cannot be before start.
$validator
->requirePresence('end', 'create')
->notEmpty('end')
->add('end', [
'time' => [
'rule' => 'time',
'message' => 'end can only accept times.'
],
'dependency' => [
'rule' => [$this, 'endBeforeStart'],
'message' => 'end can not be before start.'
],
]);
If it is a PUT request which only contains end, the model will need to query the existing record to compare against start. If it is a PUT which contains both then it need to validate against the intended new parameter.
How does cakePHP3 do this?
private function endBeforeStart($fieldValueToBeValidated, $dataRelatedToTheValidationProcess)
{
//What goes here?
}
I can't seem to find any examples of doing this online.
I'm not quite sure and haven't tested it, but maybe this gives you some hints:
$validator
->add('end', [
'endBeforeStart' => [
'rule' => function ($value, $context) {
// If it's a POST (new entry):
if ( $context['newRecord'] == '1' ) {
// Do your comparison here
// Input values are e.g. in $context['data']['starttime']
// If end is before start:
return false;
}
// If it's a PUT (update):
else {
// If starttime is not in $context['data']['starttime']
// check for the old value in $getOldEntry
$getOldEntry = $this->getOldEntry( $context['data']['id'] );
// And do your comparison here...
// If end is before start:
return false;
}
return true;
},
'message' => 'end can not be before start.' ],
])
public function getOldEntry($id = null) {
return $this->get($id);
}
I'm also not sure if the last function has to be private or public...

Cakephp: Validating an input field depending on an option selected from a dropdown list

Hie guys i need help with my code I don't know how to do it. I have a form where a student selects an exam body, if the exam body selected is zimsec marks should be empty and if the exam body is cambridge marks should not be empty and should take a range depending on grade. validMarks is the function I am using to validate marks and it stopped working when i allowed marks to be empty so as to accomodate Zimsec.
My add.ctp
echo "<td>";
echo $this->Form->label('Mark(%): ');
echo "</td><td>";
echo $this->Form->input("ApplicantOlevelQualification.mark.$s",array('label'=>''));
echo "</td></tr>";
echo $this->Form->label('Exam Body<font color=red>*</font>');
$exambody=array(
'ZIMSEC'=>'ZIMSEC',
'CAMBRIDGE'=>'CAMBRIDGE'
);
echo $this->Form->select('exam_body_code',$exambody,array('empty'=>'Please Select','selected'=>false,'label'=>'Exam Body<font color="red">*</font>'));
My Controller
$exam_body_code = $this->data['ApplicantOlevelQualification']['exam_body_code'];
'mark' => $this->data['ApplicantOlevelQualification']['mark'][$i],
My model
'exam_body_code' => array(
'notempty' => array(
'rule' => array('notempty'),
),
),
'mark' => array(
//'numeric' => array(
//'rule' => array('numeric'),
'rule' => array('validMarks'),
'message' => 'Wrong mark for this grade, please try again.',
'allowEmpty' => true,
// ),
),
public function validMarks($check) {
$grade=($this->data['ApplicantOlevelQualification']['grade']);
$mark=($this->data['ApplicantOlevelQualification']['mark']);
//var_dump($mark);
if($grade== 'A' && $mark>74) {
// $this->validationError( 'grade', 'Grade A must be greater than or equal to 75%' );
//Access $this->data and $check to compare your marks and grade;
return true;
} elseif( ($grade)== 'B' && ($mark>64)) {
return true;
} elseif( ($grade)== 'C' && ($mark)>50) {
return true;
} elseif( ($grade)== 'D' && ($mark)>40) {
return true;
} elseif( ($grade)== 'E' && ($mark)>30) {
return true;
} elseif( ($grade)== 'U' && ($mark)>0) {
return true;
} else {
return false;
}
//Access $this->data and $check to compare your marks and grade..
}
if the exam body selected is zimsec marks should be empty and if the exam body is cambridge marks should not be empty and should take a range...
In that case you should split validation into 2 functions:
function emptyIfZimsec($data) {
return $this->data['ApplicantOlevelQualification']['exam_body_code'] != 'ZIMSEC'
|| empty($this->data['ApplicantOlevelQualification']['mark']);
}
function validMarks($data) {
if ($this->data['ApplicantOlevelQualification']['exam_body_code'] != 'CAMBRIDGE')
return true;
...
emptyIfZimsec will result in a validation error if the code is ZIMSEC and mark is not empty. and validMarks will check CAMBRIDGE marks (and skip if ZIMSEC)
This way you can also output separate validation error messages for each case.
Hope this helps.

Resources