I am wondering if I can create a custom validation message for a specific field.
i have tried using this
$rules = [
'nama' =>'required',
'spesialis' =>'required',
'alamat' => 'required',
'telp' =>'required',
'tanggalMulai' => 'required'
];
$message=[
'required' => '* :attribute Harus Diisi'
];
$this->validate($request,$rules,$message);
Dokter::create([
'nama' => $request->nama,
'spesialis' =>$request->spesialis,
'alamat' => $request->alamat,
'telp' => $request->telp,
'tanggalMulai' => $request->tglMulai
]);
return redirect()->route('dokter');
but after i using this, i can't save my data
This is how i solve the problem
$message=[
'required' => '* :attribute harus diisi',
'min' =>'*:attribute minimal :min karakter'
];
$validatedData = $request->validate([
'nama' =>'required',
'spesialis' =>'required',
'alamat' => 'required',
'telp' =>'required|numeric|min:9',
'tanggalMulai' => 'required|date'
],$message);
Dokter::create($validatedData,$message);
You've to pass custom messages per particular field.
$message=[
'nama.required' => 'name field is required',
'spesialis.required' => 'spesialis field is required'
];
Related
Here my code :
$rules = [
'name' => 'required|string|max:255',
'price' => 'required|numeric|min:0',
'unit' => 'required|in:piece,kg,m',
'price_type' =>'required|string',
'service' => [
'string',
'required',
Rule::in($services_ids->all()),
],
'facility' => [
'string',
'required',
Rule::in($facilities_ids->all()),
],
'conciergeries' => [
'array',
'required',
Rule::in($conciergeries_ids->all()),
],
];
$custom_messages = [
'required' => 'Vous devez sélectionner un(e) :attribute.'
];
$validated = request()->validate($rules, $custom_messages);
The problem is that my custom_messages only works with 'name', 'price', 'unit', 'price_type' but not with 'service', 'facility' and 'conciergeries'.
Questions :
How to apply my custom messages with 'service', 'facility' and 'conciergeries' too ?
How to create a custom message for specifically one field ?
Thank's !
You just need to specify for which field you want to change the message
Try it like:-
$custom_messages = [
'service.required' => 'Your custom message for required service',
'service.string' => 'Your custom message of service should be string',];
And same process for facility and conciergeries.
I am using Laravel 6.13.1.
I have the following validation
$validator = Validator::make($request->all(), [
'name' => 'required|max:100',
'email' => 'required|email',
'mobile_number' => 'required',
'date_of_birth' => 'required',
'address' => 'required',
'category' => 'required',
'other_category' => 'required_if:category,==,Others',
'sub_caste' => 'required',
'photo' => 'required',
'status' => 'required|integer',
'father_name' => 'required',
'father_occupation' => 'required',
]);
if ($validator->fails()) {
return back()->withErrors($validator)->withInput();
}
It has 12 rules and it works. If I add one more rule then the validator stops working.
{{$errors}} gives an empty array in the view file.
Edit 1: The validation with 12 rules shows all error messages, but if I add one more validation like
$validator = Validator::make($request->all(), [
'name' => 'required|max:100',
'email' => 'required|email',
'mobile_number' => 'required',
'date_of_birth' => 'required',
'address' => 'required',
'category' => 'required',
'other_category' => 'required_if:category,==,Others',
'sub_caste' => 'required',
'photo' => 'required',
'status' => 'required|integer',
'father_name' => 'required',
'father_occupation' => 'required',
'mother_name' => 'required',
]);
then no error messages. {{$errors}} is an empty array.
In my view, I am using the following code to list errors
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
Edit 2: I tried the same validation on Laravel 5.5 and it works well as indented.
I was facing the same problem, and found somewhere that maybe this is caused by the size of error messages. I've changed the SESSION_DRIVER env option from "cookie" to "file" and it worked!
I don't think that it has a limit, However you can create a request and put all your validation on it like that:
php artisan make:request UserRequest
You can check out this link Form Request Validation
if i have more than 20 fields,can we validate this with less code???
i do not want to write required for all input .
$request->validate([
'first_name' => 'required',
'last_name' => 'required',
'gender' => 'required',
'date_of_birth' => 'required',
'place_of_birth' => 'required',
'nationality' => 'required',
'mobile_number' => 'required',
'email' => 'required|email|unique:informations',
'home_region' => 'required',
'digital_address' => 'required',
'school_name' => 'required',
'school_region' => 'required',
'school_digital_address' => 'required',
'school_level' => 'required',
'school_program_of_study' => 'required',
'patron_first_name' => 'required',
'patron_last_name' => 'required',
'patron_gender' => 'required',
'patron_mobile_number' => 'required'
]);
Sure! here's how, iterate through all request data (including ones you didn't intend to have sent which makes this a bad idea) and validate them to be required except for ones that requires more rules like 'email' (those can be validated by themselves)
$data = array_except($request->all(), ['_token', 'email']);
foreach ($data as $key => $value) {
$request->validate([$key => 'required']);
}
$request->validate(['email' => 'required|email|unique:informations']);
That's the minimal code laravel needs to works the validation.
If you dont enter each field's name, how is it supposed to know wich missing entry from the request is required ? laravel needs each and every input name marked down.
Actually yes there is a way.
This is your code
$request->validate([
'first_name' => 'required',
'last_name' => 'required',
'gender' => 'required',
'date_of_birth' => 'required',
'place_of_birth' => 'required',
'nationality' => 'required',
'mobile_number' => 'required',
'email' => 'required|email|unique:informations',
'home_region' => 'required',
'digital_address' => 'required',
'school_name' => 'required',
'school_region' => 'required',
'school_digital_address' => 'required',
'school_level' => 'required',
'school_program_of_study' => 'required',
'patron_first_name' => 'required',
'patron_last_name' => 'required',
'patron_gender' => 'required',
'patron_mobile_number' => 'required'
]);
Now in your form html for all these fields you need to make an array for it like this
<form>
<input type="text" name="person[first_name]">
<input type="text" name="person[last_name]">
<input type="email" name="email">
</form>
and in your Controller or Request you can make this array required like this
$request->validate([
'person' => 'required|array',
'person.*' => 'required'
'email' => 'required|email|unique:informations',
]);
This way you can have less index in your array ;), Hope it will help
public function register(Request $request){
$validator = \Validator::make($request->all(), [
'first_name' => 'required|min:3',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'password_confirmation' => 'required',
'address' => 'required',
'phone_number' => 'required',
]);
if($request->user_type == 1 ){
$validator = \Validator::make($request->all(), [
'dob' => 'required'
]);
}
if($request->user_type == 2 ){
$validator = \Validator::make($request->all(), [
'doctor_practice' => 'required'
]);
}
// then, if it fails, return the error messages in JSON format
if ($validator->fails()) {
return response()->json($validator->messages(), 401);
}
How can I set the validator based on user type? I did try does not work fine.
Add rules array by users type and put rules at last
$rules = [
'first_name' => 'required|min:3',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'password_confirmation' => 'required',
'address' => 'required',
'phone_number' => 'required',
];
if($request->user_type == 1 ){
$rules['dob'] = 'required';
}else if($request->user_type == 2 ){
$rules['doctor_practice'] = 'required';
}
$validator = \Validator::make($request->all(), $rules);
if ($validator->fails()) {
return response()->json($validator->messages(), 401);
}
It is better and more correctly to use embedded mechanism of validation.
$rules = [
'first_name' => 'required|min:3',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'password_confirmation' => 'required',
'address' => 'required',
'phone_number' => 'required',
'dob' => 'required_if:user_type,1',
'doctor_practice' => 'required_if:user_type,2',
];
$validator = \Validator::make($request->all(), $rules);
if ($validator->fails()) {
return response()->json($validator->messages(), 401);
}
It is described in Laravel documentation
required_if:anotherfield,value,...
The field under validation must be present and not empty if the anotherfield field is equal to any value.
How i set custom the attributte names in laravel 5.2 I already try this code, but doesn't work:
$attNames = array(
'code' => 'Número',
'contributor' => 'Nº Contribuinte',
'create_date' => 'Data criação',
'address' => 'Morada',
'zip_code' => 'Cod. Postal',
'city' => 'Localidade',
'email' => 'E-mail',
'phone_number' => 'Telefone',
'note' => 'Observações',
);
$validator = Validator::make($client, $this->rules,[],$attNames);
$validator->setAttributeNames($attNames);
if ($validator->fails()) {
// send back to the page with the input data and errors
$errors = $validator->messages();
return Redirect::to('/client/create')->withInput()->withErrors($errors);
}
You have passed wrong arguments to Validator::make.
You can pass only three arguments.
As per Documentation,
If needed, you may use custom error messages for validation instead of
the defaults. There are several ways to specify custom messages.
First, you may pass the custom messages as the third argument to the
Validator::make method.
$messages = [
'required' => 'The :attribute field is required.',
];
$validator = Validator::make($input, $rules, $messages);
I figure out.
controller:
use Validator;
(...)
$attName=array(
'code' => trans('validation.code'),
'contributor' => trans('validation.contributor'),
'create_date' => trans('validation.create_date'),
'address' => trans('validation.address'),
'zip_code' => trans('validation.zip_code'),
'city' => trans('validation.city'),
'email' => trans('validation.email'),
'phone_number' => trans('validation.phone_number'),
'note' => trans('validation.note'),
);
$validator = Validator::make($client, $this->rules, [], $attNames);
validation.php:
'attributes' => [
'code' => 'número',
'contributor' => 'nº contribuinte',
'create_date' => 'data criação',
'address' => 'morada',
'zip_code' => 'cod. postal',
'city' => 'localidade',
'email' => 'e-mail',
'phone_number' => 'telefone',
'note' => 'observações',
],