I'm using the validation for the employee records during an update. There are some fields which should be unique. But during an update of the employee records, the validation for the unique field is being done. I have researched and tried out the solution as well.
But I'm getting this error:
Error Code : 904 Error Message : ORA-00904: "ID": invalid identifier
Position : 71 Statement : select count() as aggregate from
"EMPLOYEES" where " EMAIL" = :p0 and "ID" <> :p1 Bindings :
[ad#sdfdsf.com,3336] (SQL: select count() as aggregate from
"EMPLOYEES" where " EMAIL" = ad#sdfdsf.com and "ID" <> 3336)
Here is my attempt for the solution:
public function update(Request $request, int $employee_id) {
$this->validate ( $request, [
'first_name' => "required|max:220|regex:/[a-z]/",
'middle_name' => "max:120",
'last_name' => "required|max:220|regex:/[a-z]/",
'email' => "required|unique:employees, email, $employee_id|regex:/[a-zA-Z0-9._%+-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}/",
'home_phone' => "unique:employees,home_phone, $employee_id|numeric|regex:/^[09][0-9]{8,9}$/",
'mobile' => "unique:employees,mobile, $employee_id|numeric|regex:/(9)[0-9]{9}/",
'job_id' => 'required',
'department_id' => 'required',
'group_id' => 'required',
'node' => 'required',
'branch' => 'required',
'username' => "required|unique:employees,username, $employee_id|regex:/[A-Za-z0-9][.][A-Za-z0-9]/",
'exchange_username' => "required|unique:employees,exchange_username, $employee_id|regex:/[A-Za-z0-9][.][A-Za-z0-9]/",
'extension' => "unique:employees,extension, $employee_id|numeric|regex:/[0-9]{4}/",
] );
Employee::where ('employee_id', $employee_id )->update ( $request->only ( [
'first_name',
'middle_name',
'last_name',
'email',
'address',
'home_phone',
'mobile',
'job_id',
'department_id',
'group_id',
'branch',
'node',
'name',
'username',
'type',
'exchange_username',
'toggle_ivr_access',
'extension',
'attributed_team',
'cable_team_id',
'disable',
] ) );
Session::flash ( 'message', 'The Employee is Successfully Updated.' );
return redirect ()->route ( 'employees.index' );
}
Forcing A Unique Rule To Ignore A Given ID:
Sometimes, you may wish to ignore a given ID during the unique check.
For example, consider an "update profile" screen that includes the
user's name, e-mail address, and location. Of course, you will want to
verify that the e-mail address is unique. However, if the user only
changes the name field and not the e-mail field, you do not want a
validation error to be thrown because the user is already the owner of
the e-mail address.
To instruct the validator to ignore the user's ID, we'll use the Rule
class to fluently define the rule. In this example, we'll also specify
the validation rules as an array instead of using the | character to
delimit the rules:
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);
I figured out the mistake and successfully find out the solution. Here is the working solution in case anyone is facing the similar problem. I had to pass the employee_id of the single user whereas I was passing the employee_id of the multiple users.
public function update(Request $request, int $employee_id) {
$this->validate ( $request, [
'first_name' => "required|max:220|regex:/[a-z]/",
'middle_name' => "max:120",
'last_name' => "required|max:220|regex:/[a-z]/",
'email' => "required|unique:employees,email,".$employee_id.',employee_id|regex:/[a-zA-Z0-9._%+-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}/',
'home_phone' => "unique:employees,home_phone,$employee_id,employee_id|numeric",
'mobile' => "required|unique:employees,mobile,$employee_id,employee_id|numeric",
'job_id' => 'required',
'department_id' => 'required',
'group_id' => 'required',
'node' => 'required',
'branch' => 'required',
'username' => "required|unique:employees,username,".$employee_id.',employee_id|regex:/[A-Za-z0-9][.][A-Za-z0-9]/',
'exchange_username' => "required|unique:employees,exchange_username,".$employee_id.',employee_id|regex:/[A-Za-z0-9][.][A-Za-z0-9]/',
'extension' => "required|unique:employees,mobile,$employee_id,employee_id|numeric|regex:/[0-9]{4}/",
] );
Employee::where ('employee_id', $employee_id )->update ( $request->only ( [
'first_name',
'middle_name',
'last_name',
'email',
'address',
'home_phone',
'mobile',
'job_id',
'department_id',
'group_id',
'branch',
'node',
'name',
'username',
'type',
'exchange_username',
'toggle_ivr_access',
'extension',
'attributed_team',
'cable_team_id',
'disable',
] ) );
Session::flash ( 'message', 'The Employee is Successfully Updated.' );
return redirect ()->route ( 'employees.index' );
}
Related
this is my controller
$rules = [
'Group_Id'=>'required',
'Group_Code'=>'required',
'Group_Name'=>'required| string',
'Currency'=>'required',
'Country'=>'required',
'State'=>'required',
'City'=>'required',
]
I want group id Group code should be unique while create if group id /group code already present in table means I want to print error as group id/group id already exit .while updating don't want to change group id filed that's remains same
$rules = [
'Group_Id'=>'required|unique:group,id',
'Group_Code'=>'required|unique:group,code',
'Group_Name'=>'required| string',
'Currency'=>'required',
'Country'=>'required',
'State'=>'required',
'City'=>'required',
]
For solving this issue you should add unique identifier in your validation method mentioned as below
$data = $request->validate([
'Group_Id' => 'required',
'Group_Code' => 'required|unique:table_name_here',
'Group_Name' => 'required| string',
'Currency' => 'required',
'Country' => 'required',
'State' => 'required',
'City' => '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'm trying to update a user, as an admin.
I'm changing the username, but it says email must be unique.
How do I fix this.
public function update($id, PutUser $request)
{
if (auth()->id() == $id) {
return redirect()->back()->withFlashDanger('Permission Denied, You can not edit own profile here.');
}
$user = User::find($id);
$user->update((array_merge($request->validated(), ['county' => request('county')])));
//Update model_has_roles model with assignees_roles
return redirect()->route('users.index')->withFlashSuccess(trans("alerts.users.updated"));
}
This is the request class
public function authorize()
{
return true;
}
public function rules()
{
$user_id = $this->input('id');
return [
'name' => 'required|string',
'username' => 'required',
'email' => 'required|email|unique:users,email'.$user_id,
'gender' => 'required',
'phone' => 'sometimes|numeric',
'address' => 'sometimes|string',
'country_id' => 'required',
];
}
}
I keep getting a failed email validation. 'Email has already been taken'. Any idea
You are missing a comma after the email label in your validation:
return [
'name' => 'required|string',
'username' => 'required',
'email' => 'required|email|unique:users,email,'.$user_id,
'gender' => 'required',
'phone' => 'sometimes|numeric',
'address' => 'sometimes|string',
'country_id' => 'required',
];
Since Laravel 5.3 (I believe), you can also use rule builders for more descriptive validation rules. Those are better to read and interpret for humans so it would result in a lower error rate:
use Illuminate\Validation\Rule;
return [
'email' => [
'required',
Rule::unique('users', 'email')->except($user_id),
]
];
https://medium.com/#tomgrohl/why-you-should-be-using-rule-objects-in-laravel-5-5-c2505e729b40
I have 2 tables within one function that I'd like to save data to. One is the Users table, and the second one is a Clinic table.
My user's table is currently working correctly, but I'm unsure if it's 'best practice':
$user = User::create([
'name' => Str::title($request->get('name')),
'email' => $request->get('email'),
'password' => bcrypt($request->get('password'))
])
->clinic()->create($request->only([
'name' => Str::title('clinic_name'),
'telephone',
'address_1',
'address_2',
'city',
'postcode'
]));
My problem occurs at the 'name' column of the Clinic table. It just doesn't save it, even though it's in the $fillable array in my Clinic column:
protected $fillable = ['user_id', 'name', 'telephone'', 'address_1',
'address_2', 'city', 'postcode'];
I have attempted to 'Chain' the methods together, as I want to save the 'user_id' within the Clinic table because they're related.
Many thanks for your help.
You're overriding the name key in your $request->only() call:
$user = User::create([
'name' => Str::title($request->get('name')),
'email' => $request->get('email'),
'password' => bcrypt($request->get('password'))
])->clinic()->create($request->only([
'name' => Str::title('clinic_name'), // This is overriding your 'name' field.
'telephone',
'address_1',
'address_2',
'city',
'postcode'
]));
If you want to run Str::title() over the requests clinic_name, you'll need to assign the attributes manually like so:
$user = User::create([
'name' => Str::title($request->get('name')),
'email' => $request->get('email'),
'password' => bcrypt($request->get('password'))
])->clinic()->create([
'name' => Str::title($request->get('clinic_name')),
'telephone' => $request->get('telephone'),
'address_1' => $request->get('address_1'),
'address_2' => $request->get('address_2'),
'city' => $request->get('city'),
'postcode' => $request->get('postcode'),
]);
Note: Just as a tip, you can also just retrieve request input as a property like so:
->create([
'name' => $request->name // Performs $request->get('name').
])
You can't have 'name' => Str::title('clinic_name') when using create(), it must be a single key as 'name'.
You can use the following before creating the user:
$request->replace('name' => Str::title('clinic_name'));
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',
],