Laravel old POST data empty after validation - php

I am printing the contents of old() in my view:
{{ print_r(old('steps'), true) }}
When I submit the form with the following validation rules, the old data prints fine:
$this->validate($request, [
'steps.*.name' => 'required',
]);
When I add more rules, the old data dissapears completely:
$this->validate($request, [
'steps.*.name' => 'required',
'steps.*.title' => 'required',
'steps.*.type' => 'required',
'steps.*.answer_options' => 'nullable|required_if:steps.*.type,Question',
'steps.*.input_type' => 'nullable|required_if:steps.*.type,Input',
]);
I've confirmed this only happens AFTER validation. How do I fix this?

Try to set SESSION_DRIVER=file to get it work
See related

Related

Laravel validate array and exclude on specific array value

I have an array of values that I send together with other fields in a form to laravel.
The array contains a role_id field and a status field, the status can be I (Insert) U (update) D (Delete). When I validate the values in the array I want it to skip the ones where the status equals D. Otherwise I want it to validate.
private function checkValuesUpdate($userid = null)
{
return Request::validate(
[
'displayname' => 'required',
'username' => 'required',
'email' => ['nullable', 'unique:contacts,email' . (is_null($userid ) ? '' : (',' . $userid ))],
'roles.*.role_id' => ['exclude_if:roles.*.status,D', 'required']
]
);
}
I can't seem to get it to work. I've been searching all over the place for functional code as well as the Laravel documentation. But no luck so far. Has anybody ever done this?
Your problem comes from a misunderstanding of the exclude_if rule. It doesn't exclude the value from validation, it only excludes the value from the returned data. So it would not be included if you ran request()->validated() to get the validated input values.
According to the documentation, you can use validation rules with array/star notation so using the required_unless rule might be a better approach. (Note there's also more concise code to replace the old unique rule, and I've added a rule to check contents of the role status.)
$rules = [
'displayname' => 'required',
'username' => 'required',
'email' => [
'nullable',
Rule::unique("contacts")->ignore($userid ?? 0)
],
'roles' => 'array',
'roles.*.status' => 'in:I,U,D',
'roles.*.role_id' => ['required_unless:roles.*.status,D']
];

Laravel Validator. Unable to retrieve both errors and Input

I am using Laravel for a project and i am trying to validate some input fields from a form. I am using the Validator class.
Here is the code in my controller.
$validator = Validator::make($request->all(), [
'arithmos_kinhths' => 'required',
'kathgoria_kinhths' => ['required',Rule::notIn(['-'])],
'prohgoumenos_paroxos_kinhths' => ['required',Rule::notIn(['-'])],
'programma_kinhths' => ['required',Rule::notIn(['-'])],
'project_kinhths' => ['required',Rule::notIn(['-'])],
'kathogoria_epidothshs_kinhths' =>['required',Rule::notIn(['-'])],
'talk_to_eu_kinhths' => ['required',Rule::notIn(['-'])],
'pagio_kinhths' => 'required',
'sms_kinhths' => ['required',Rule::notIn(['-'])],
'internet_kinhths' => ['required',Rule::notIn(['-'])],
'international_kinhths' => ['required',Rule::notIn(['-'])],
'twin_sim_kinhths' => ['required',Rule::notIn(['-'])],
'wind_unlimited_kinhths' => ['required',Rule::notIn(['-'])],
]);
if ($validator->fails()) {
return back()->withErrors($validator)->withInput();
}
In the blade file i am trying to catch the errors using the code bellow.
#if($errors->any())
#foreach($errors->all() as $error)
<script>
$.notify(
{
title: '<strong>ERROR!</strong>',
message: '{{$error}}',
},
{
type: 'danger',
},
)
</script>
#endforeach
#endif
Also i want to put the old values into the input fields using {{old('value'}}
The problem i have is that i can't combine both errors and inputs. If i return only the errors using withErrors($validator) the errors are printed out. And if i return only withInput i have the post values.
Any ideas?
withInputs()
try this, i hope that help a little , or
you can
return back()->with('errors',$validator)->withInputs()
you can use $this->validatorWith([]) then follow your code, you don't need to manually redirect back to that page. your request will auto redirect to that page from where request has happened. this function belongs to trait Illuminate/Foundation/Validation/ValidatesRequests which is use by app\Http\Controllers\Controller.php. you just need to use. for more about this trait see here ValidateRequest
$this->validatorWith([
'request_param' => 'required',
]);

Handle validator to return json

Laravel throw an exception at $validator->fails() call.
Ok, I just want to create a stateless register method in ApiController.php with Laravel 5.7.
I used the Validator facade to check the sent data.
$validator = Validator::make($request->all(), [
'name' => 'required',
'email' => 'required|email|unique',
'password' => 'required',
'c_password' => 'required|same:password',
]);
if ($validator->fails()) {
return response()->json(['error'=>$validator->errors()],
Response::HTTP_UNAUTHORIZED);
}
But, when I use xdebug, I see something strange. The fails methods seems throw an exception.
Laravel send an error HTML page with title:
Validation rule unique requires at least 1 parameters.
The route is used in api.php
Route::post('register', 'Api\UserController#register');
Do you have an explanation for this?
Thx for reading.
The syntax for unique rule is unique:table,column,except,idColumn.
So i changed it for you to use the users table.
If you don't want to use the users table change the users part behind unique:
$validator = Validator::make($request->all(), [
'name' => 'required',
'email' => 'required|email|unique:users',
'password' => 'required',
'c_password' => 'required|same:password',
]);
if ($validator->fails()) {
return response()->json(['error'=>$validator->errors()],
Response::HTTP_UNAUTHORIZED);
}
For more information on the unique rule see this: https://laravel.com/docs/5.7/validation#rule-unique
In your API request add the following header:
...
Accept: application/json // <-----
This will tell Laravel that you want a response in a json format.
Note: this is different to the Content-type: application/json. The later indicates the format of the data tha is being sent in the body.

Add a validation which is not related to the form

I want to add a validation on a form. My actual form works, here it is:
public function store(Request $request, $id)
{
$this->validate($request, [
'subject' => 'required',
'body' => 'required',
]);
// Do something if everything is OK.
}
Now, I want to check if the user is "active" too. So something like:
\Auth::user()->isActive();
And return an error with the other validation errors if the user is not active.
Can I append something to the validator that has no relation with the form itself? I mean I want to add an error to the other errors if the user is not active.
That code is only validating the request variable (first argument of validate() function). So you will have to put someting in the request to validate it. It applies the rules to the object/array given.
$request->is_active = Auth::user()->isActive();
$this->validate($request, [
'subject' => 'required',
'body' => 'required',
'is_active' => true //or whatever rule you want
]);
Anyways, I never tried that so not sure it will work. The usual way is to do an if
if ( !Auth::user()->isActive() ) {
return redirect->back()->withErrors(['account' => 'Your account is not active, please activate it']);
}
//continue here

laravel validate array fields

I am trying to validate array field using laravel validate functionality as follow
$this->validate($request,['prodActualQty' => 'required|numeric','actQty[]' => 'required'
],$messages);
my input file is: <input class='form-control' type='text' name='actQty[]'>
It gives error if fields are blank but it still gives error even we fill the fields.
In Laravel 5.2 you can validate Form array elements using wildcards keyword.
So as per your situation you can either remove [] like below
$this->validate($request->all(), [
'prodActualQty' => 'required',
'actQty' => 'required'
]);
Or use wildcard operator
$this->validate($request->all(), [
'prodActualQty' => 'required',
'actQty.*' => 'required'
]);

Categories