0
votes

How to get form validation return data array on signup form

I submit the form and have some validation mean email,require,unique email,

when validation have error message then laravel 5.2 return validation return array.

3
Can you show me how exactly do you get this object please? - Alexey Mezenin
when any one submit form and empty the userName or email then I will create validation in controller. if user have no userName and email validation return error array and also form data i need form data - Muhammad Bilal
Why don't you just use request data? - Alexey Mezenin
I am new in laravel please help how to use request data ? - Muhammad Bilal

3 Answers

1
votes

I guess you want to retain the submitted data on error.

Considering a sample

public function postJobs(Request $request) {
    $input     = $request->all(); 

    $messages  = [
        'job_title.required'      => trans('job.title_required'),
    ];        

    $validator = Validator::make($request->all(), [
        'job_title'    => 'required'
    ], $messages);       

    if ($validator->fails()) {  // redirect if validation fails, note the ->withErrors($validator)

        return redirect()
            ->route('your.route')
            ->withErrors($validator)
            ->withInput();
    }

 // Do other stuff if no error

}

And, in the view you can handle errors like this:

 <div class="<?php if (count($errors) > 0) { echo 'alert alert-danger'; } ?>" >

    <ul>
        @if (count($errors) > 0)
            @foreach ($errors->all() as $error)
                <li>{!! $error !!}</li>
            @endforeach
        @endif
    </ul>
</div>

And if you want the input data, you need to redirect with ->withInput(); which can be fetch in view like:

Update

 <input name= "job_title" value="{{ Request::old('job_title') }}" />

But, the best thing is to use laravel Form package so they all are handled automatically.

0
votes

If you just need form data, you can use Request object:

 public function store(Request $request)
 {
     $name = $request->input('firstName');
 }

https://laravel.com/docs/5.1/requests#accessing-the-request

Alternatively, you could use $request->get('firstName');

0
votes

Use old() to get previous value from input. Example:

<input name="firstName" value="{{old('firstName')}}">

See documentation here https://laravel.com/docs/5.1/requests#old-input