$this->validation custom redirect - validation

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.

Related

How can I use fails method validation in controller

controller validator in laravel :
$validationController = $this->validate(request(), [
'title' => 'min:100',
'text' => 'required',
'image' => 'required',
]);
and we can make validator with :
$validationNormaly = Validator::make(request(), [
'title' => 'min:100',
'text' => 'required',
'image' => 'required',
]);
and i can't use $validationController->fails(). how can i use it?
The first validation that you mentioned will fail automatically as designed by the laravel code base.
The Validator::make() i typically use when doing ajax requests and such to the controller method. You will need to run the validation fails method to invoke the failed response like so:
$validator = Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
if ($validator->fails()) {
return redirect('post/create')
->withErrors($validator)
->withInput();
}
You Can Use :
$validator = Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
])->validate();
take advantage of the automatic redirection with error messages

error getting a user existing in laravel sanctum

I have a error, I'm using sanctum and I want to check that the email does not exist
the if returns an empty array but the if is satisfied because it returns true
$mail = $request->input(['email']);
if ($search = User::where('email', $mail)->get()) {
return response()->json(['msg' => 'account already exist'], 409);
} else {
$validate = $request->validate([
'name' => 'required|string|',
'email' => 'required|string',
'password' => 'required|string'
]);
}
any solution?
Why not use the Laravel validation since this looks more like validation, so something like:
$request->validate([
'name' => 'required|string|',
'email' => 'required|string|email|unique:users,email',
'password' => 'required|string'
]);
with this you don't need to do an if else. You can check the Laravel docs on https://laravel.com/docs/8.x/validation#introduction for more details

Laravel one validation function for Store and Update

I am trying to have one validation function for both store and update. So I don't repeat code. It works well. The problem is testing 'unique'. I worked around with this idea. But feels long-winded. Is there a better way to do it?
I want to check for unique at the store.
At update, unique check ignores own id.
I don't want different validations like I did as the user will be
first notified of the unique error, he will fix it. then something
else might be wrong and he has to fix again.
.
public function validateRequest($request){
if($request->method() == "PUT")
{
$val = $this->validate($request, [
'name' => 'unique:customers,id',
'phone' => 'unique:customers,id',
]);
}
if($request->method() == "POST"){
$val = $this->validate($request, [
'name' => 'unique:customers',
'phone' => 'unique:customers'
]);
}
$validation = $this->validate($request, [
'name' => 'required',
'phone' => 'required|integer|gt:0',
'phone2' => 'nullable|integer|gt:0|',
'email' => 'email|nullable',
'note' => 'nullable',
],
[
'phone.integer' => "Invalid phone format. Use international format. Eg: 971550000000",
'phone2.integer' => "Invalid phone format. Use international format. Eg: 971550000000",
'required' => "Required Field",
]);
return $validation;
}

Laravel validation does not work with many fields

I have a strange Laravel behaviour. If I validate my form with less than about 10 fields, everything works perfectly fine, including showing error messages (e.g. "field1 is required"):
public function myFctName(Request $request)
{
$validator = Validator::make($request->all(), [
'year' => 'required',
'field1' => 'required',
'field2' => 'required'
]);
if ($validator->fails()) {
return back()->withErrors($validator->errors())->withInput();
}
return view('companiesView');
}
My form has 23 fields. As soon as I add around 10 fields, everythings works fine if there are no validation errors. Here is the second examplecode:
public function myFctName(Request $request)
{
$validator = Validator::make($request->all(), [
'year' => 'required',
'field1' => 'required',
'field2' => 'required',
'field3' => 'required',
'field4' => 'required',
'field5' => 'required',
'field6' => 'required',
'field7' => 'required',
'field8' => 'required',
'field9' => 'required',
'field10' => 'required',
'field11' => 'required',
'field12' => 'required',
'field13' => 'required',
'field14' => 'required'
]);
if ($validator->fails()) {
return back()->withErrors($validator->errors())->withInput();
}
return view('companiesView');
}
If there is an validation error, the redirect (back()) still works. However, there is no error message displayed.
If I change the line return back()->withErrors($validator->errors())->withInput(); to return back()->withErrors($validator->errors());, the error messages are displayed. So the problem must be with the withInput() function.
Moreover, with the withInput() part in place, there is a warning in the Chrome console (not happening in Firefox though): Set Cookie header is ignored in response from url: ... Cookie length should be less than or equal to 4096 characters. I am not actively doing anything with Cookies at this point.
Does anyone know what the problem could be?
withInput() is using cookie/session under the hood to store the old values. Maybe those are flooding.
An alternate could be
$request->validate([
'year' => 'required',
]);
<input type="text" name="year" value="{{old('year')}}">
for displaying the errors
#if($errors->any())
#foreach($errors->all() as $error)
{{$error}}
#endforeach
#endif
The problem is with the session driver. It is probably set to cookie which can only store 4096 characters. Set it to file, database, or redis.
In .env
SESSION_DRIVER=cookie
to
SESSION_DRIVER=database

Validation : Laravel

In ContactsRequest.php
public function rules()
{
return [
'org_id' => 'required',
'name' => 'required',
'office' => 'required',
'mobile' => 'required_without:home',
'home' => 'required_without:mobile'
];
}
So basically what i want is , i have a form which will be taking the attributes specified in the code. But i want to modify code so that entering either one of 'home' or 'mobile' will allow me to create the new user.
What should be done.Any help would be appreciated

Resources