• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
redirect back withInput requries session otherwise old() empty

#1
I am not sure if this is the correct behaviout.

In one of my controller methods -

PHP Code:
public function test() {
 
       $data_post $this->request->getPost();
 
       if(!empty($data_post)) {
 
           $validation $this->validate(
 
               ['what' => 'required',],
 [
'what' => ['required'=>'Please enter what is required']]);

 if(
$validation == false) {
 
redirect()->back()->withInput();
 exit;
 
           } else {
 
//valid submission
 

 }
 
 
$html '<form method="post">
 <input type="text" class="form-control" name="what" id="what" value="'
.old('what').'" />
 <button class="btn btn-success" type="submit">Submit</button>
 </form>'
;
 
$html .= service('validation')->listErrors();

 echo 
view('myview', ['html' => $html]);
 


It prints out the validation error message but old doesn't fill in the value in the input box.

However, if I just add
PHP Code:
session() 
in this code above it works fine.

Is this how it it is supposed to work? Or perhaps I need to make a session call in the constructor of the controller so that session is available in all the methods?
Reply

#2
Is the form and validation in same controller method? In which case you could end up in endless loop - validation fails, same POST data is sent back, so it'll try to re-validate, repeat...

I keep form and validation on same method, so if input fails validation, it just continues with putting together HTML form, but knows to now insert errors too.
Reply

#3
(10-11-2018, 02:23 AM)Pertti Wrote: Is the form and validation in same controller method? In which case you could end up in endless loop - validation fails, same POST data is sent back, so it'll try to re-validate, repeat...

I keep form and validation on same method, so if input fails validation, it just continues with putting together HTML form, but knows to now insert errors too.

Yes they are in the same method and it works well without any looping issues. As you can see, I go into validation IF(post data is not empty) i.e. on a POST request and redirect WithInput is a GET request - so no issues at all. The only issue is that I got to have a session() call at the top of this method to be able to read old input data using Old method.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2018 MyBB Group.