I'm using laravel 5.4 to make a custom form validation. But why is the custom error message isn't displayed?
Validator::extend('myCustomeValidator', function ($attribute, $value, $parameters, $validator) {
//some code here
return false;
});
return Validator::make($data, [
'myField' => 'myCustomeValidator',
]);
and added the following to the file : ressources\lang\en\validation.php as the documentation advises:
'custom' => [
'myField' => [
'myCustomeValidator' => 'You made an error.',
],
],
The error is correctly triggered but instead of my custom error message, I get this:
validation.my_custome_validator
What am I missing?
You need to use the snake-cased name of your custom validation rule. The following should the trick:
'custom' => [
'myField' => [
'my_custome_validator' => 'You made an error.',
],
],
Related
I'm working with Laravel 5.8 and here is my validation request form:
public function rules()
{
return [
'art_audio_file' => ['nullable', 'file', 'mimes:audio/mpeg,mpga,mp3,wav,aac'],
'art_audio_file_title' => ['required_if: IF USER HAS UPLOADED FILE']
];
}
I wonder, how can I make art_audio_file_title required if art_audio_file is not empty.
So how can I do that?
You can add your logic like below:
return [
'art_audio_file' => ['nullable', 'file', 'mimes:audio/mpeg,mpga,mp3,wav,aac'],
'art_audio_file_title' => [
Rule::requiredIf(function() {
return !empty($this->request->get('art_audio_file'));
})
]
];
or in your case simply:
'art_audio_file_title'=>'required_with:art_audio_file'
I have the following input
{
"password":"password",
"environment_roles":[
{
"environment_id":"",
"role_id":""
}
],
"admin":true
}
and have a Request class with following rules :
public function rules()
{
return [
'password' => 'required|min:6|regex:/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$/',
'environment_roles' => 'array',
'environment_roles.*.role_id' => 'required_if:admin,false|exists:roles,role_id',
'environment_roles.*.environment_id' => 'required_if:admin,false|exists:environment,environment_id',
'admin' => 'sometimes'
];
}
But it is showing the following validation error if I give the above input, which has admin as true.
"validation": {
"environment_roles.0.role_id": [
"The selected environment_roles.0.role_id is invalid."
],
"environment_roles.0.environment_id": [
"The selected environment_roles.0.environment_id is invalid."
]
},
How can I fix this. I need to validate the environment_roles.*.role_id and environment_roles.*.environment_id when the value for admin is true.
If you are always sending the admin prop it would be more suitable to make it nullable not required. You could try with that:
public function rules()
{
return [
'password' => 'required|min:6|regex:/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$/',
'environment_roles' => 'array',
'environment_roles.*.role_id' => 'nullable|required_if:admin,false|exists:roles,role_id',
'environment_roles.*.environment_id' => 'nullable|required_if:admin,false|exists:environment,environment_id',
'admin' => 'bool|sometimes'
];
}
But your error shows that the role and the environment id's does not exist in the database ( The exists rule ). Setting those two fields to nullable means it will not trigger the exists rule.
I make form by React. After submitted form, I need to validate data from Laravel. Problem is that sending data is diffrent than normal form. So any values from dorm is in array data.
//normal form
$request->title
//sending from React
$request->data['title']
So, look at this code
class articleRequest extends Request
{
public function rulse(){
return [
'title' => 'required',
//other rules
];
}
}
class ArticleController extends Controller
{
public function atoreArticle(articleRequest $request){
Textads::create([
'title'=> $request->data['title'],
//other
]);
}
}
But I have an error that title field is required. Without valdiation everything is ok. How I can solve my problem?
You can try this -
$rules = [
'title' => 'required',
//other rules
];
Validator::make($request->all(), $rules)->validate();
will this work? or $request->all()->data ?
$validator = Validator::make($request->data, [
title'' => 'required'
],[
//custom error message if needed
]);
if ($validator->fails()) {
return response()->json([
'success' => false,
'data' => $validator->messages(),
'message' => "error"
], 422);
}
I try to validate a POST request.
The format is: d.m.Y (12.1.2017)
My rule is required|date_format:d.m.Y for this field.
I get this error message:
InvalidArgumentException in Carbon.php line 425:
Unexpected data found.
Unexpected data found.
Data missing
If I change the . to - or even / it is working -> POST data changed before to match the rule.
I need the German format for this.
edit:
My validation rules:
public function rules()
{
return [
'title' => 'required|max:255',
'expiration_date' => 'required|date_format:d.m.Y',
//'description' => 'required',
'provision_agent' => 'required|integer|between:0,100',
'discount_consumer' => 'required|integer|between:0,100',
'quota' => 'required|integer',
];
}
Wrap your Format should work i just tried with 5.2 it's working fine.
public function rules()
{
return [
'title' => 'required|max:255',
'expiration_date' => 'required|date_format:"d.m.Y"',
//'description' => 'required',
'provision_agent' => 'required|integer|between:0,100',
'discount_consumer' => 'required|integer|between:0,100',
'quota' => 'required|integer',
];
}
But the error what you added in question InvalidArgumentException in Carbon.php line 425: it seems something else my guess you are using expiration_date some where in controller or model like this with Carbon
echo Carbon::createFromFormat('Y-m-d', '12.1.2017');
You should try something like this
echo Carbon::parse('12.1.2017')->format('Y-m-d')
Wrap your format into quotes:
'date_format:"d.m.Y"'
Try like this,
public function rules()
{
return [
'title' => 'required|max:255',
'expiration_date' => 'date_format:"d.m.Y"|required', // I have changed order of validation
//'description' => 'required',
'provision_agent' => 'required|integer|between:0,100',
'discount_consumer' => 'required|integer|between:0,100',
'quota' => 'required|integer',
];
}
Hope this will solve your problem.
If you don't succeed solving the issue otherwise, you can still use a custom validation rule:
Validator::extend('date_dmY', function ($attribute, $value) {
$format = 'd.m.Y';
$date = DateTime::createFromFormat($format, $value);
return $date && $date->format($format) === $value;
}, 'optional error message');
The extra check $date->format($format) === $value is to avoid erroneously accepting out-of-range dates, e.g. "32.01.2017". See this comment on php.net.
Once the custom validation rule has been defined, you can use it like so:
public function rules() {
return [
'expiration_date' => 'required|date_dmY',
];
}
I have some validation that requires a url or a route to be there but not both.
$this->validate($request, [
'name' => 'required|max:255',
'url' => 'required_without_all:route|url',
'route' => 'required_without_all:url|route',
'parent_items'=> 'sometimes|required|integer'
]);
I have tried using required_without and required_without_all however they both get past the validation and I am not sure why.
route is a rule in the route field
I think you are looking for required_if:
The field under validation must be present if the anotherfield field is equal to any value.
So, the validation rule would be:
$this->validate($request, [
'name' => 'required|max:255',
'url' => 'required_if:route,""',
'route' => 'required_if:url,""',
'parent_items'=> 'sometimes|required|integer'
]);
I think the easiest way would be creation your own validation rule. It could looks like.
Validator::extend('empty_if', function($attribute, $value, $parameters, Illuminate\Validation\Validator $validator) {
$fields = $validator->getData(); //data passed to your validator
foreach($parameters as $param) {
$excludeValue = array_get($fields, $param, false);
if($excludeValue) { //if exclude value is present validation not passed
return false;
}
}
return true;
});
And use it
$this->validate($request, [
'name' => 'required|max:255',
'url' => 'empty_if:route|url',
'route' => 'empty_if:url|route',
'parent_items'=> 'sometimes|required|integer'
]);
P.S. Don't forget to register this in your provider.
Edit
Add custom message
1) Add message
2) Add replacer
Validator::replacer('empty_if', function($message, $attribute, $rule, $parameters){
$replace = [$attribute, $parameters[0]];
//message is: The field :attribute cannot be filled if :other is also filled
return str_replace([':attribute', ':other'], $replace, $message);
});