When I create a field I use the following validation.
$this->validate($request, [
'client_name' => 'required',
'contact_name' => 'required',
'email' => 'required|unique:clients|email',
]);
Now when I try to edit this with PUT. I try the following
$this->validate($request, [
'client_name' => 'required',
'contact_name' => 'required',
'email' => 'required|unique:clients|email',
]);
$client = \App\Client::find($id);
$client->client_name = $request->client_name;
$client->contact_name = $request->contact_name;
$client->email = $request->email;
$client->slug = str_slug($request->client_name, '_');
$client->save();
The problem - if I remove the Unique attribute it can be set as the same email address as another record. If I keep it in the record will not save.
Is there a work around?
Syntax for unique validator in Laravel (here)
unique:table,column,except,idColumn
So, in your code you need to mention the id which it need to except while applying unique.
$this->validate($request, [
'client_name' => 'required',
'contact_name' => 'required',
'email' => 'required|unique:clients,client_email,clientid,'.$id.'|email',
]);
$client = \App\Client::find($id);
$client->client_name = $request->client_name;
$client->contact_name = $request->contact_name;
$client->email = $request->email;
$client->slug = str_slug($request->client_name, '_');
$client->save();
Can also refer some discussion in laracast (here)
I hope it will help you.
Related
i have three columns in my database ,
i need to make a validation for these columns ,
and to make the tree of them unique.
Validator::make($request->all(),
[
'name' => 'required|min:4|string',
'color'=>'required',
'size_id'=>'required',
]);
i already tried
Validator::make($request->all(),
[
'name' => 'required|min:4|string|unique:products,name,color,size_id',
'color'=>'required|unique:products,color,name,size_id',
'size_id'=>'required|unique:products,size_id,name,color',
]);
and i tried this
$color = $request->input('color');
$size_id = $request->input('size_id');
$name = $request->input('name');
// dd($name);
return
Validator::make($request->all(),
[
'name' => 'required|min:4|string|unique:products,name,'.'NULL'.',color,'.$color.',size_id,'.$size_id,
'description'=>'required|min:4',
'quantity'=>'required|numeric',
'subcategory_id'=> 'required',
'category_id'=> 'required',
'price'=> 'required|numeric',
'color'=>'required|unique:products,color,'.'NULL'.',name,'.$name.',size_id,'.$size_id,
'size_id'=>'required|unique:products,size_id,'.'NULL'.',name,'.$name.',color,'.$color,
'cover'=>'required|image:png,jpg,jpeg',
// 'images'=>'image:png,jpg,jpeg',
'images.
*'=>'image:png,jpg,jpeg',
]);
but it return undefined offset 3 when i check if it fails or not!
thanks
$color = $request->get('color');
$size_id = $request->get('size_id');
$name = $request->get('name');
// dd($name);
return
Validator::make($request->all(),
[
'name' => 'required|min:4|string|unique:products,name,NULL,id,color,'.$color.',size_id,'.$size_id,
'description'=>'required|min:4',
'quantity'=>'required|numeric',
'subcategory_id'=> 'required',
'category_id'=> 'required',
'price'=> 'required|numeric',
'color'=>'required|unique:products,color,NULL,id,name,'.$name.',size_id,'.$size_id,
'size_id'=>'required|unique:products,size_id,NULL,id,name,'.$name.',color,'.$color,
'cover'=>'required|image:png,jpg,jpeg',
// 'images'=>'image:png,jpg,jpeg',
'images.*'=>'image:png,jpg,jpeg',
]);
the code above solved my problem
The function below is to update user information, I need to validate if email is not duplicated, also to ignore password if its field left empty, I don't why this function not working!
public function update(Request $request, $id)
{
$this->validate($request->all(), [
'fname' => 'required',
'email' => 'required|email|unique:users,email,'.$id,
'password' => 'same:confirm-password',
'roles' => 'required'
]);
$input = $request->all()->except(['country_id', 'region_id']);
if(!empty($input['password'])){
$input['password'] = Hash::make($input['password']);
}else{
$input = array_except($input,array('password'));
}
$user = User::find($id);
$user->update($input);
DB::table('model_has_roles')->where('model_id',$id)->delete();
$user->assignRole($request->input('roles'));
return response()->json(array('data' => trans('message.success')));
}
Firstly, you need to correct your validation.
You need to change it to
$request->validate([
'fname' => 'required',
'email' => [
'required',
'email',
Rule::unique('users')->ignore($id),
],
'password' => 'same:confirm-password',
'roles' => 'required'
]);
$request->all() returns an array, you need to change it to
$input = $request->except(['country_id', 'region_id']);
Also I would change the condition to use Laravel's build-in method:
if($request->filled('password'))
Pass the $request object not $request->all(), that returns an array of the input.
$this->validate($request, [
'fname' => 'required',
'email' => [
'required',
Rule::unique('users')->ignore($id),
],
'password' => 'nullable,confirmed',
'roles' => 'required'
]);
I changed the password rule to check if it exists and if it does, it confirms it matches.
Again, I removed the chained all() method
$input = $request->except(['country_id', 'region_id']);
if(!empty($input['password'])){
$input['password'] = Hash::make($input['password']);
}else{
$input = array_except($input,array('password'));
}
Remember to use
use Illuminate\Validation\Rule;
I am using Laravel 6.
I created a form to insert a meeting and I created a validate method in the controller to check if the data inserted in the db are correct.
Unfortunately when the validate method fails the default redirect back deletes every field that has been compiled by the user.
I tried many times but I am unable to understand how to do in case of failure a redirect back with the previous values filled in by the user.
Controller:
{
$this->validate($request, [
'participants' => [ 'required', new CheckParticipant() ],
'description' => 'required',
'room' => [ 'required', new CheckRoom() ],
'date_meeting' => [ 'required', new CheckDateTime() ],
'start' => [ 'required', new CheckTime() ],
'end' => 'required',
]);
$meeting = new Meeting();
$participants = request('participants');
$meeting->id_participants = implode(';', $participants);
$meeting->description = request('description');
$meeting->id_room = request('room');
$meeting->date = request('date_meeting');
$meeting->start_hour = request('start');
$meeting->end_hour = request('end');
$meeting->save();
$message_correct = "The meeting has been correctly inserted!";
return redirect()->route('home')->with('success', $message_correct);
}
Finally if the user filled in for example the name of a participant to the meeting but the validate method fails I would like that the participant appears already selected in the dropdown menu after the default redirect back.
Is someone able to help me?
In your view, you can use the old function to retrieve data flashed from the previous request.
Instead of calling $this->validate try this:
$validator = Validator::make($request->all(), [
'participants' => [ 'required', new CheckParticipant() ],
'description' => 'required',
'room' => [ 'required', new CheckRoom() ],
'date_meeting' => [ 'required', new CheckDateTime() ],
'start' => [ 'required', new CheckTime() ],
'end' => 'required',
]);
if($validator->fails()) {
return redirect('your/url')
->withErrors($validator)
->withInput();
}
Then, in your view you can access the old inputs with old('yourValue').
I don't know how to get the query string array to validate in Laravel Validation.
here is my code
public function addUserByAdmin(Request $request){
$this->validate($request,[
'name' => 'required',
'fullname' => 'required',
'password' => 'required',
'position' => 'required',
'phone' => 'required'
]);
$uesrAction = new UserAction($this->repository);
$user = $uesrAction->addUser($request->input('phone'),$request->input('password'),$request->input('name'),$request->input('fullname'),$request->input('position'));
if($user){
return response()->json(['success' => 'success','user'=>$user]);
}
return response()->json(['success' => 'fail']);
}
but validation doesn't work.Then I tried Validation::make() method and also didn't work.
My URL be like
http://localhost:8000/admin/users/add-user?phone=abcdefg&password=erer&name=ere&fullname=rere&position=343
If you are using laravel 5.5, change your code as follows:
public function addUserByAdmin(Request $request){
$validatedData = $request->validate([
'name' => 'required',
'fullname' => 'required',
'password' => 'required',
'position' => 'required',
'phone' => 'required'
]);
// ...
}
One of the features that was added in this update is that other way to use the validate() method:
More info:
https://laravel.com/docs/5.5/validation#quick-writing-the-validation-logic
https://laravel.com/docs/5.5/releases
I am pretty new in Laravel and I have the following problem.
I have this method that handle the submit of a form:
public function store(Request $request) {
Log::info('store() START');
$data = Input::all();
Log::info('INSERTED DATA: '.implode("|", $data));
$rules = array(
'name' => 'required',
'surname' => 'required',
'login' => 'required',
'email' => 'required|email',
'emailConfirm' => 'required|email',
'pass' => 'required',
'passlConfirm' => 'required',
'g-recaptcha-response' => 'required|captcha',
);
$validator = Validator::make($data, $rules);
if ($validator->fails()){
return Redirect::to('/registration')->withInput()->withErrors($validator);
}
else{
// Do your stuff.
}
}
As you can see it contains a $rules array containing the validation rules.
It works pretty fine but I want also perform the following 2 checks:
The email field have to contains the same text than the emailConfirm field.
The pass field have to contains the same text than the passConfirm field.
Can I implement this kind of check into the $rules array?
Actually Laravel comes with the validation rule called confirmed. You will need to change your fields to email_confirmation and pass_confirmation and add confirmed rule to email and pass fields.
As answered by #Mariusz, Laravel come up with value comparison rule as confirmed rule. To acheive your output you should write,
$rules = array(
'name' => 'required',
'surname' => 'required',
'login' => 'required',
'email' => 'required|confirmed|email',
'pass' => 'required|confirmed',
'g-recaptcha-response' => 'required|captcha',
);
As you have mentioned, you are using laravelcollective/html, then you have to also add following code to generate confirmation fields
echo Form::email("email_confirmation",null, $attributes = []);
echo Form::password("pass_confirmation",null, $attributes = []);