1

In reference to this stockoverflow question

I created a new request with php artisan make:request ApplicationFormRequest

public function rules() { 
   return ['first_name' => 'required']
}

I also have a controller as such,

public function store(ApplicationFormRequest $request){
   //empty
}

I have seen an example on stackoverflow with

return Redirect::to('/')->withInput();

they seem to put their validation together with the controller, but I am not sure how to go about it this way.

Where would I put that? How do I retrieve old input even with validation fail?

EDIT you see my controller is empty, it validate automatically with the rule i set at ApplicationFormRequest. when it failed it automatically redirect to the view where the input is submitted with error

@foreach ($errors->all() as $error)
    <li class="alert label">{{$error}}</li>
@endforeach

but I am unable to fill the input with the input user just submitted, I try to do

<input type="text" value="{{ old('first_name') }}" \>

but this give me error Use of undefined constant

Community
  • 1
  • 1
aahhaa
  • 2,240
  • 3
  • 19
  • 30
  • 1
    Your question is very vague. What does the Redirect example mean? What do you mean "put their validation together with the controller?" – d1str0 Feb 29 '16 at 05:15
  • I try to clarified it in my question please take a look, let me know if there is anything that is unclear. – aahhaa Feb 29 '16 at 05:22
  • Check a value exists. I don't know how is it done in Laravel, but it should look something like this `if(old('first_name')!=""){echo old('first_name');}else{}` – Ikhlak S. Feb 29 '16 at 05:29
  • @user3284463 thanks, but I don't know where to pass my first_name variable back in. as I stated in the question I have seen people do `return Redirect::to('/')->withInput();` but I don't know how in my case. – aahhaa Feb 29 '16 at 05:31
  • Is this a multi-step form or an edit form? – Ikhlak S. Feb 29 '16 at 05:57
  • @user3284463 it is a single step form. But as validation fail it will be redirected to the same page. But with all previous input deleted. I want to keep the input for user to fix. – aahhaa Feb 29 '16 at 05:59
  • Okay... Check if the POST or GET has a value by using `input::has("first_name") ` and if does, then echo to input value which would look like `{{ Input::get('name') }}` else do nothing – Ikhlak S. Feb 29 '16 at 06:13
  • @user3284463 I try `{{old('last_name')}}` after I took a shower it just magically work... I have no ideas why, yet I tried that so many times before. thanks guys. – aahhaa Feb 29 '16 at 06:33
  • Great. My answer is just a long cut to the `old` function. lol – Ikhlak S. Feb 29 '16 at 06:47

3 Answers3

1

In Laravel 5.2, if the view forms are not being populated with old data sent back from a custom Request class, do the following:

1) Place all routes related to the request in the route group, which should be included in 5.2

Route::group(['middleware' => ['web']], function () {
    // routes here
});

2) Run php artisan key:generate


EDIT: This fix may not be applicable for versions other than 5.2.

Credits:

Laravel - Session store not set on request

No supported encrypter found. The cipher and / or key length are invalid.

Another relevant post

Community
  • 1
  • 1
brietsparks
  • 4,776
  • 8
  • 35
  • 69
0

as the error say use of undefined constant. Your errors variable is not in the scope of view. check what you are doing wrong.

use Docs for further reading.

Aqeel Haider
  • 603
  • 7
  • 24
  • I think what I did wrong is not passing the old input back, but since the app do it automaticlly I don't know where to pass it. – aahhaa Feb 29 '16 at 05:30
0

You can do this by checking if a value is available in the POST or GET array first. If it is available in the POST or GET array, then echo it in the value attribute of the input.

Your input field would look something like this:

<input type="text" name="first_name" value="if (Input::has('first_name')){ @echo Input::get('first_name');}" \>
Ikhlak S.
  • 8,578
  • 10
  • 57
  • 77