Laravel 5.5 Validation with OR/ELSE conditional - laravel-5

i'm new to laravel and i would like to know if there are a way to use or/else conditional in $rules, because I am struggling trying to do the following validation:
If the field is Z the size must be y, else, x.
I tried this, but it always validade data with size:11
'type => 'required',
'field' => ['required', ('type == 1' ? 'size:11' : 'size:14')],
Anyone has a clue? thanks in advance

I assume you use Form request validation so you can do something like this:
$rules = [
'type' => 'required',
'field' => ['required'];
];
$rules['field'][] = ($this->input('type') == 1) ? 'size:11' : 'size:14';
return $rules;

Use Laravel Validator ,
Refer Link: https://laravel.com/docs/5.5/validation
$validatedData = $request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);

Related

Using Laravel firstOrCreate function to search in two columns

how can I check two columns with firstOrCreate function? I need to work this like where('email', $request->email)->orWhere('personal_code', $request->personal_code), not like where('email', $request->email)->where('personal_code', $request->personal_code)
$user = User::firstOrCreate(
[
'email' => $request->email,
'personal_code' => $request->personal_code,
],
[
'name' => $request->tenant,
'phone' => $request->phone,
'address' => $request->address
]
);
Thanks for help in advance!
You won't be able to run an or within the firstOrCreate() method.
To achieve this, you would need to create using custom functionality:
$user = User::where('email', $request->email)
->orWhere('personal_code', $request->personal_code);
$user = $user->exists()
? $user->first()
: User::create($request->only(['email', 'personal_code', 'name', 'phone', 'address']));
// or $request->expect('_token') if all data passed via form.

How do i add more validation rules to a laravel validation array?

I have a laravel validation rules array that I have defined like this.
$rules = array(
'name' => 'required|regex:/(^[A-Za-z0-9 ]+$)+/',
'email' => 'required|email',
'mobile' => 'regex:/^\+?\d+$/',
);
$validation = Validator::make($input, $rules);
now depending on another if condition I want to add another validation rule into this array.
address => 'required'
how do I do it? I have tried the using array_push() function with the $rules array, but it doesn't work.
$rules['address'] = 'required';

Laravel - Validate value against string

I am running some basic validation inside a Laravel 5.5 controller like this...
$this->validate($request, [
'name' => 'required|max:30',
'email' => 'required|unique:users|email',
'password' => 'required|max:20',
'mykey' => 'required',
]);
Is there a way to check if 'mykey' matches a php string I have saved? I know I can do an if statement and compare them but wondered if there was a way I could do this inside the validation itself?
You can use in rule, This works for n number of values
$request->validate([
'name' => 'required|max:30',
'email' => 'required|unique:users|email',
'password' => 'required|max:20',
'mykey' => [
Rule::in([env('MY_KEY'),config('app.another_key')]),
]
]);
Laravel provides a regex option for validation. Depending on the complexity of the string comparison it may be useful:
https://laravel.com/docs/5.5/validation#rule-regex
You can this rule:
$key = "my_saved_key"
$request->validate([
'name' => 'required|max:30',
'email' => 'required|unique:users|email',
'password' => 'required|max:20',
'mykey' => 'in:' . $key,
]
]);

$this->validation custom redirect

I have a form with four tab so after validation it redirect me on first tab always, but i want it redirect me on that which have error message
Here is my code
$this->validate($request,
[
'name' => 'required|min:3|alpha_spaces',
'date_of_birth' => 'required|date|date_format:"Y-m-d"',
'place_of_birth' => 'required',
'nationality' => 'required',
'address' => 'required',
'port_name' => 'required',
'contact_number' => 'required|digits_between:8,15|numeric',
'religion' => 'required',
'education_level' => 'required',
'marital_status' => 'required',
'interview_method' => 'required',
'can_be_interviewed_via' => 'required',
'date_to' => 'required',
'date_from' => 'required',
'country' => 'required',
]);
and for redirect i m using on every tab i m using submit button with hidden filed selecttab
if ($data['selecttab'] == 'tab0') {
return redirect("fdws/".$id."/edit?tab=tab0");
}elseif($data['selecttab'] == 'tab1'){
return redirect("fdws/".$id."/edit?tab=tab1");
}elseif($data['selecttab'] == 'tab2'){
return redirect("fdws/".$id."/edit?tab=tab2");
}else{
return redirect("fdws/".$id."/edit?tab=tab3");
}
When no validation apply it work fine
Before you call your `$this->validate($request, ...` set `$redirect` to the result of your `if ($data['selecttab'] == 'tab0') { ...` statement. Therefore, I suggest first do your if statement and have a variable named `$redirect` that catches the result of the if statment
if ($data['selecttab'] == 'tab0') {
$redirect = "fdws/".$id."/edit?tab=tab0";
}elseif($data['selecttab'] == 'tab1'){
$redirect = "fdws/".$id."/edit?tab=tab1";
}elseif($data['selecttab'] == 'tab2'){
$redirect = "fdws/".$id."/edit?tab=tab2";
}else{
$redirect = "fdws/".$id."/edit?tab=tab3";
}
And then `$this->validate(...)`.
Scratch that! I made a big mistake. According to official Laravel 5.0 documentation, and also looking at the Illuminate\Foundation\ValidatesRequests trait, when using Controller Validation, it is NOT possible to just chose the redirect route without modification to the traits or other codes. I think using Form Request will give you the power you want with a lot less hassle.
Solution found,
I done like that and its working fine :)
$validator = Validator::make($request->all(), [
'can_be_interviewed_via' => 'required',
]);
if ($validator->fails()) {
return redirect("fdws/".$id."/edit?tab=tab3")
->withErrors($validator)
->withInput();
}
In Laravel 5 use Middleware as helpers for controllers and routes. This will help you a lot.

Laravel Form validation issue when form edit?

I used Laravel Form validation for validate form:
$rules = array(
'user_name' => 'required|min:3|max:20|unique:users',
'email' => 'required|email|max:50|unique:users',
'mobile' => 'required'
);
this is working well on some thing add. but when i going to edit unique:users part given error "already been taken", so how to write validation to check exclude edit row.
If I wrote this way, does it work?
$rules = array(
'user_name' => 'required|min:3|max:20|unique:users,'.$id,
'email' => 'required|email|max:50|unique:users,'.$id,
'mobile' => 'required'
);
#user3099298 Your second set of code looks alright.
$rules = array(
'user_name' => 'required|min:3|max:20|unique:users,user_name,' . $user_id, // $user_id = Currently being edited user id.
'email' => 'unique:users,email_address,' . $user_id, // $user_id = Currently being edited user id.
'mobile' => 'required',
);
I have added column names in rules - may be you can try that one.
Please check here the documentation
Where they explain how to ignore some rules by providing ID.

Resources