Laravel Custom Validation: Show field data of failing record - laravel

I need a way to place data of failing record along with count of records that failed because of the same reason. I hope the explanation is enough to get the requirement.
eg:
$rules['inventories.*.activity_id']= [ 'required', 'exists:activities,id ];
$messages['inventories.*.activity_id.required'] = 'Activity id can not be blank.';
$messages['inventories.*.activity_id.exists'] = "Activity id <<< [FAILING RECORD -> ACTIVITY_ID] >>> does not exist in the system. <<< NUMBER OF RECORDS FAILED FOR THIS REASON >>> entries skipped.";
Anyone have an idea about this?
I found I can place :attributes but this does't not what I want.

You could try using the :input attribute to display the value you are checking the existence of in your custom validation message:
'Activity id :input does not exist in the system.'
Though this will not get you to your failure count part.

this shows you number of validation errors:
count($errors)
if you want to show the invalid value that passed among request in the error message, you can do this:
public function messages()
{
return [
'fieldname.numeric' =>
'The :attribute must be numeric. Your value is '.$request->input('fieldname')
];
}

Related

Laravel Request / Validation always return string type values

In laravel 7, the $request->all() or the $validator->valid(), always return an array of values, key assign is correct, but the values are always strings.
I need the validator to transform the field to the rules i made.
Example in rules for validation : ['no' => 'required|integer|min:1',...]
Example of the output of validation->valid() : [ "no" => "1231" ] - string typed, i need this to be integer like : [ "no" => 1231 ]
I dont want to cast every field one by one... what i'm doing wrong ?
NOTE
All the validations works well, it's only the output of the fields that i want to match the validation, if i say the field is integer, the result of the validation must be integer and not string.
I just resolve this problem :
Create a FormRequest file, and put the validations, rules and messages there.
I dont know why... in the last version : my validations , rules and messages stay in the controller file.
Clear cache, everything ok.

How to have a CakePHP model field requirement checked manually?

QUESTION UPDATED: I found out more information and therefor changed my question.
I want to save my user with his license number, but only if he selects that he has a license. I have three possible values for the pilot_type field (ROC, ROC-light and recreative) and only the last option should allow for an empty string to be submitted.
In order to achieve this, I wrote the following validation function:
$validator
->add('license_nr', 'present_if_license', [
'rule' => function ($value, $context) {
return $context['data']['pilot_type'] == 'recreative' || !empty($value);
},
'message' => "If you don't fly recreatively, a license number needs to be supplied"
]);
The problem is that setting any validation rule on a field will trigger an additional check in the CakePHP Model class that will reject the value if it's empty. I tried to fix this by adding ->allowEmpty('license_nr'), but that rule makes for the model to accept an empty string without even running my custom function. Even putting them in order and using 'last' => true on my custom rule doesn't resolve this problem:
$validator
->add('license_nr', 'present_if_license', [
'rule' => function ($value, $context) {
return false;
// return $context['data']['pilot_type'] == 'recreative' || !empty($value);
},
'last' => true,
'message' => "If you don't fly recreatively, a license number needs to be supplied"
])
->allowEmpty('license_nr');
How do I make CakePHP run my custom function in order to see if the field can be empty, rather than just assuming that it can never be empty or must always be empty?
By default fields aren't allowed to be empty, so that's the expected behavior and you'll have to explicitly allow empty values.
In order to achieve what you're trying to do, instead of using an additional rule, you should customize the allowEmpty() rule, use the second argument to supply a callback (note that unlike rule callbacks it will receive a single argument that provides the context).
So you'd do something like this, which you may need to modify a bit, for example depending on whether you need it to work differently for creating ($context['newRecord'] = true) and updating ($context['newRecord'] = false) records:
->allowEmpty(
'license_nr',
function ($context) {
return $context['data']['pilot_type'] === 'recreative';
},
"If you don't fly recreatively, a license number needs to be supplied"
)
As of CakePHP 3.7 you can use allowEmptyString(), it will work the same way, you just need to swap the second and third arguments, as allowEmptyString() takes the message as the second argument, and the callback as the third argument.
See also
Cookbook > Validation > Conditional Validation

Laravel Validation - Individual rule and individual field problem

i have a field in Laravel with the name "company_url". and its stored like this.
$post['company_url'] = "http://example.org";
and then i have a rule string which i have stored for validation which must be applied on this individual field. which is stored like this
$post['rule'] = "required|max:24";
now i am trying this code to get validation errors. which is not working.
$validator = Validator::make([$post['name']], [$post['rules']]);
tell me what is the way to get errors on this validation?
The data you're passing to make() is in incorrect format. It should be in key-value pair format.
Also I don't know where the $post['name'] coming from. I assume it is company_url not name.
$post['rules'] is also undefined. It should be $post['rule']
The following should work:
$validator = Validator::make(['company_url' => $post['company_url']], ['company_url' => $post['rule']]);

laravel validation check field equal something

how to check a field is exactly equal to a string or number
I want to check a field named course_id is equal a field of database id in course database. now I want to check if course_id is equal to id.
You can validate it by using the exists validation rule:
$validationRules = ['course_id' => 'exists:course,id'];
create rule as below and use validator on input
$rules = array(
'id' => 'exists:your_table_name'
);
for more help
https://laravel.com/docs/5.2/validation#rule-exists

Laravel validation rule `confirmed` message show on wrong field

For example you have 2 inputs: password and password_confirmed.
model
$rule = array (
'password' => 'min:4|confirmed',
'password_confirmed' => 'min:4',
);
If the user inputs the wrong password in the password_confirmed input, the validator sends the message to password so that the error message gets displayed with the password errors and not the password_confirmation errors.
How do I make the confirmation error go to the password_confirmation messages?
Well, this may not be a direct answer to your question, but will still give desired result.
Look into custom messages for your validation:
$messages = [
'password.confirmed' => 'Your passwords were mismatched',
'password_confirmation.min' => 'Your password must be at least 4 characters'
];
You should be able to get the desired message for any of your fields by utilizing this feature. Unless I am misunderstanding your issue?

Resources