0
votes

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?

2
You can use $this->validateWith([]), put your validation here you don't need to redirect back to your page, it will redirect of the page from where that request happend. - udit rawat
Illuminate\Foundation\Validation\ValidatesRequests; this is already extended by your app\Http\Controllers\Controller.php file. you just need to use it - udit rawat
I tried it. By using this, i don't get neither errors or old post values... - Vasilis13

2 Answers

0
votes
withInputs()

try this, i hope that help a little , or you can

return back()->with('errors',$validator)->withInputs()
0
votes

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',
]);