In my application, custom validation for address field not working, but other fileds are working. No idea regarding the issue.
View
<div class="col-md-4">
<label for="email">Location:</label>
<div class="form-group">
<input name="address" id="autocomplete" placeholder="Enter your address"
onFocus="geolocate()" class="form-control" value="{!!$company->address!!}" type="text"></input>
</div>
</div>
Controller
$this->validate($request, [
'name' => 'required|regex:/^[a-zA-Z .0-9]+$/',
'office_mail' => 'required|email|max:255',
'address' => 'required|regex:/^[a-zA-Z ,0-9]+$/',
'industry_id' => 'required',
'contact_number' => 'numeric|digits_between:10,12',
'company_website_url' => 'required|url',
]);
Validation.php
'custom' => [
'name' => [
'required' => 'Your name is required',
],
'address' => [
'required' => 'The location field is required',
],
'address' => [
'regex' => 'Please enter a valid location',
],
'company_id' => [
'required' => 'The company field is required',
],
]
The custom validation for name is working, I get "Your name is required" validation message.But for address field I'am getting 'The address field is required' instead of 'The location field is required'
'custom' => [
'name' => [
'required' => 'Your name is required',
],
'address' => [
'required' => 'The location field is required',
'regex' => 'Please enter a valid location',
],
'company_id' => [
'required' => 'The company field is required',
]
]
Because your define address field 2 times in array so 1st one override
by 2nd .
In 2nd array no message for required filed that is the reason its displaying default message
'address' => [
'regex' => 'Please enter a valid location', // No message for required
],
Need change it to :
'address' => [
'required' => 'The location field is required',
'regex' => 'Please enter a valid location',
],
Related
I want to make roles to be available only for admin can select roles for users and while user wants to edit the profile information can only see his roles but can't edit his role how to make it roles field to be locked for users
Following code :
<div class="form-group">
{{ Form::label('role', 'Roles', array('class' => 'control-label mb-1')) }} <br/>
{{ Form::select('roles[]',$roles,$selectedRoles,['class'=>'myselect','data-placeholder'=>'Select role(s)', 'multiple'] ) }}
</div>
You may use the following
use Illuminate\Validation\Rule;
//..
if(!$request->filled('roles')) {
$request->merge(['roles' => []]);
}
$this->validate($request,[
'name' => 'required',
'email' => 'required|email', //'required|email|unique:users,email',
'password' => [ 'string', 'min:8'],
'roles' => 'nullable|array',
'roles.*' => [Rule::requiredIf($request->filled('roles')), 'exists:roles,id'],
],[
'name.required' => "Name field is required",
'email.required' => "Email Field is Required",
'email.email' => "Invalid Email Format ",
'password.min' => "The Password Must be at Least 8 Characters or More",
'roles.*' => "The Role is Required",
]);
//...
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 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'
];
I have validated UAE-based phone numbers in Laravel. Is there any way to validate all countries numbers (not only UAE-based)?
$messages = [
'required' => 'The :attribute field is required.',
'integer' => 'The :attribute field value should be an integer.',
'regex' => 'The :attribute is invalid',
'digits' => 'The :attribute is invalid',
];
$validator = Validator::make($request->all(),
[
'number' => 'required|digits:12|regex:/(9715)[0-9]{8}/',
'service' => 'required|integer'
],
$messages
);
I am trying to add a custom validation message using the below code,
$validator = Validator::make(
$user,
[
'first_name' => 'required|min:2',
'email' => [
'required',
'email',
Rule::notIn(array_column(Customer::getEmails(), 'email'))
]
],
['email.required' => 'Email is required (Custom message)']);
I have added a custom message for email required validation. No issues there.
For Rule::notIn validation, currently it is returning The email is invalid. How can I add a custom message in this case?
Unable to find anything related in Laravel docs about this.
Try this:
$validator = Validator::make(
$user,
[
'first_name' => 'required|min:2',
'email' => [
'required',
'email',
Rule::notIn(array_column(Customer::getEmails(), 'email'))
]
],
[
'email.required' => 'Email is required (Custom message)',
'email.email' => 'Your custom message here',
'email.not_in' => 'Your custom message here',
]
);