0

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.

4
  • Can you show me how exactly do you get this object please? Commented Apr 26, 2016 at 12:12
  • 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 Commented Apr 26, 2016 at 12:23
  • Why don't you just use request data? Commented Apr 26, 2016 at 12:25
  • I am new in laravel please help how to use request data ? Commented Apr 26, 2016 at 12:26

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I already get $error message array but I need Data array which one I show in image I already used this code view code for error array
see my updated answer. does that helps? I guess you want to retain the submitted data on occurrence of error?
can explain little bit more @VipindasKS
see the line in my answer return redirect() ->route('your.route') ->withErrors($validator) ->withInput(); this will redirect the form without losing the form field values. so you can call it in your fields like <input value="{{ Request::old('job_title') }}" />
Again, this is in assumption that you need to show the form fields filled with already submitted data, when some of the fields fails validation. so that, you don't need to fill it again.
|
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.