I'm trying to apply these validation rules to my controller function, but any of the rules are not applying
Here is my code
if($request->hasFile('propic'))
{
$this->validate($request, [
'name' => 'required', 'alpha','min:2', 'max:255',
'last_name' => 'required', 'alpha','min:5', 'max:255',
'mobile' => 'required', 'string','min:10','max:14', 'regex:/\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|
2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|
4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}$/',
'email' => 'required', 'string', 'email', 'max:255', 'unique:users,email,'.$setting->id.'',
'propic' => 'required','image','mimes:jpeg,png,jpg,gif,svg','max:2048',
]);
$imageName = time().'.'.$request->propic->extension();
$request->propic->move(public_path('propics'), $imageName);
$setting->propic=$imageName;
$setting->name=$request->input('name');
$setting->last_name=$request->input('last_name');
$setting->mobile=$request->input('mobile');
$setting->email=$request->input('email');
$setting->update();
return Redirect::back()->with('success',__('sentence.User updated successfully'));
}
At time of writing, there's two accepted formats for passing in validation rules:
As an array of strings (note the square brackets, which is what you are missing currently):
$this->validate($request, [
'name' => ['required', 'alpha','min:2', 'max:255'],
...
]);
As a single pipe-delimited string:
$this->validate($request, [
'name' => 'required|alpha|min:2|max:255',
...
]);
Related
If i have a Validator like below
[
'id' => ['string', 'required', 'regex:/^[A-Za-z]{1}([A-Za-z]|[0-9]){5,7}$/im', 'unique:users'],
'user_name' => ['string', 'required', 'unique:users'],
'email' => ['string', 'required', 'email', 'unique:users'],
'password' => [\Illuminate\Validation\Rules\Password::min(8)->letters()->mixedCase()->numbers()->symbols(), 'max:32', 'sometimes'],
'role_id' => ['integer', 'required'],
'status' => ['integer', 'required', 'in:1,2']
]
That would requires 4 * 3 * 4 * 3 * 2 * 3 = 846 methods to test all the validation cases, which sounds stupid itself.
How would validation feature testing (and other kinds of testing) actually be implemented?
I have two tables "rooms" and "beds" i want to unique bed name when room_id is same. what should i do.
public function store(Request $request)
{
$roomid = $request->input('room_id');
//
$attributes = request()->validate([
'room_id' => ['required', 'integer', 'min:1'],
'name' => ['required', 'string', 'min:1', 'max:10', 'unique:App\Models\bed,name,room_id' . $roomid],
'type' => ['nullable', 'string', 'min:1', 'max:10'],
'description' => ['nullable', 'string', 'min:1', 'max:20']
]);
bed::create($attributes);
}
~~~
You should use closures instead of a rule object because this validation doesn't seem to be necessary throughout your application and you need it only here.
Change your validation to this:
$attributes = request()->validate([
'room_id' => ['required', 'integer', 'min:1'],
'name' => [
'required', 'string', 'min:1','max:10',
function ($attribute, $value, $fail) {
// Checke if name is unique in that room
$name_exists = \App\Models\Bed::where('name', $value)->where('room_id', request()->input('room_id'))->count() > 0;
if ($name_exists) {
$fail('The '.$attribute.' must be unique in this room.');
}
}
],
'type' => ['nullable', 'string', 'min:1', 'max:10'],
'description' => ['nullable', 'string', 'min:1', 'max:20']
]);
Source
solved
use Illuminate\Validation\Rule;
public function store(Request $request)
{
$roomid = $request->input('room_id');
$attributes = request()->validate([
'room_id' => ['required', 'integer', 'min:1'],
'name' => [
'required',
'string',
'min:1',
'max:10',
Rule::unique('beds')
->where(function ($query) {
return $query->where('room_id', request('room_id'));
})
],
'type' => ['nullable', 'string', 'min:1', 'max:10'],
'description' => ['nullable', 'string', 'min:1', 'max:20'],
]);
bed::create($attributes);
}
Hello all this is my update controller for an user. I want to know how can I apply these laravel validation rules ONLY to updated fields. Currently when I update only the first name, mobile number also get validated. My name fields are alpha validated and phone number is validated via regex.
public function update(Request $request, User $setting)
{
request()->validate([
'name' => ['required', 'alpha','min:2', 'max:255'],
'last_name' => ['required', 'alpha','min:2', 'max:255'],
'mobile'=>['required', 'string','regex:/\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|
2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|
4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}$/'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email,'.$setting->id.''],
]);
$setting->update($request->all());
return Redirect::back()->with('success','User updated successfully');
}
I able to handle the email(unique) field, but not the others.
Considering the small chat we had in the comments, what you must do is to first get the model from the database and to diff the request with the model's attributes. Then you can keep the validation rules for the changed attributes only.
public function update(int $id, Request $request, User $userRepository)
{
$user = $userRepository->find($id);
$changedAttributes = array_diff($request->all(), $user->getAttributes());
$validationRules = array_intersect_key([
'name' => ['required', 'alpha','min:2', 'max:255'],
'last_name' => ['required', 'alpha','min:2', 'max:255'],
'mobile' => ['required', 'string', 'regex:/\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|
2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|
4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}$/'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email,'.$setting->id.''],
], $changedAttributes);
$this->validate($request, $validationRules);
$user->update($changedAttributes);
return Redirect::back()->with('success','User updated successfully');
}
I'm trying to validate an array within the update method which means I need to ignore the id's of the rows so that it doesn'r return the error:
contactName has already been taken
Here is my validation from my update method of my controller:
public function update($id, Request $request)
{
$brand = Brand::findOrFail($id);
$request->validate([
'name' => ['required', 'string', 'max:255', Rule::unique('brands')->ignore($id)],
'contactNames' => ['required', 'array'],
'contactNames.*' => ['required', 'max:255', 'string', 'distinct', Rule::unique('brand_managers', 'name')->ignore( //what goes here? )],
'emails' => ['required', 'array'],
'emails.*' => ['required', 'max:255', 'email', 'distinct', Rule::unique('brand_managers', 'email')->ignore( //what goes here? )],
'contactNumbers' => ['array'],
'contactNumbers.*' => ['numeric'],
'groupCheckbox' => ['required', 'min:1'],
]);
}
With the unique validation rule on 'name' I can ignore the id that is coming with the $id using route model binding, but with the contactNames and contactEmails this needs to check a table that has a manyToMany relationship -
how do I ignore the id's of the brand_manager rows for multiple validation checks?
I will try and edit my question for more clarity
Thanks!
You can easily create validation rules dynamically as you wish.
Here is example from my current project which requires unique field except self.
use Illuminate\Validation\Rule;
$unique_check = Rule::unique('brand_managers', 'email'); // table with some unique rows
//if you need ignore multiple rows/multiple conditions
$unique_check->where(function ($query) use ($brand) {
return $query->whereNotIn('id', $brand->brand_managers->pluck('id')->toArray());
});
$request->validate([
'emails.*' => [ // promocode unique string column
'required',
$unique_check
],
]);
$request->validate([
'name' => ['required', 'string', 'max:255', Rule::unique('brands')->ignore($id)],
'contactNames' => ['required', 'array'],
'contactNames.*' => ['required', 'max:255', 'string', 'distinct',.$brand->id, // ID of record which want to ignore
'emails' => ['required', 'array'],
'emails.*' => ['required', 'max:255', 'email', 'distinct', .$brand->id, // ID of record which want to ignore
'contactNumbers' => ['array'],
'contactNumbers.*' => ['numeric'],
'groupCheckbox' => ['required', 'min:1'],
]);`
pass id want to ignore at time of update records.
more information check https://laravel.com/docs/5.7/validation
My validation fails with this:
$this->validate($request, [
'name' => ['required'],
'email' => ['required', 'email', 'unique:organisers',$organiser->id,'organisers_id'],
'organiser_logo' => ['mimes:jpeg,jpg,png', 'max:10000'],
]);
but it works with this:
$this->validate($request, [
'name' => ['required'],
'email' => ['required', 'email', 'unique:organisers'],
'organiser_logo' => ['mimes:jpeg,jpg,png', 'max:10000'],
]);
This:
'unique:organisers',$organiser->id,'organisers_id'
needs to be:
'unique:organisers,'.$organiser->id.',organisers_id'
or (note the double-quotes):
"unique:organisers,{$organiser->id},organisers_id"
The , means "new array element", the . means "add to this string".