laravel 4 Input::old() is empty
Solution 1
Finally i found the issue: There was a leading space in the routes.php file, before the <?php
opening tag. (was working on a team and someone else added that space).
Solution 2
You are doing Input::flash()
and then withInput()
, which effectively does Input::flash()
twice, probably invalidating the flashed input. Try only doing one of the two.
Also, MihirEvent::save();
is wrong, you want to do $event->save();
.
Solution 3
Looking at the Laravel 4 source:
/**
* Flash an array of input to the session.
*
* @param array $input
* @return \Illuminate\Http\RedirectResponse
*/
public function withInput(array $input = null)
{
$input = $input ?: $this->request->input();
$this->session->flashInput($input);
return $this;
}
It looks like if you don't pass an array of Input with ->withInput it tries to pull it from the original request. Try modifying the line like so:
if ($event_add === true)
{
return Redirect::to('admin/event/create')
->withErrors($validator)->withInput(Input::all());
}
else
{
return Redirect::to('admin/event/edit/'.$event->event_id)
->withErrors($validator)->withInput(Input::all());
}
This should hopefully force it to pass the array of input values, instead of relying on the
'$this->request->input()'
still existing in the session.
beerwin
Areas: PHP CodeIgniter Laravel MySQL SQL Server Vue, React, Javascript, JQuery CSS Delphi C# Node JS
Updated on June 04, 2022Comments
-
beerwin almost 2 years
I'm using Laravel 4 for a project, but got an issue. I'm not sure, what i'm doing wrong.
Details:
- Form is posted to the controller's save function.
- When validation fails, i'm redirecting to the create function
- After redirect (using
Redirect::to(somewhere)->withErrors($validator)->withInput()
):- Validation errors are being displayed correctly (if any)
- Input::old() is empty (it should contain previously submitted data)
Create function in controller
public function create() { $this->scripts[] = 'various js path here'; return View::make('admin.modules.events.create', array( // Loading various scripts specified in this function 'scripts' => $this->scripts, )); }
In the view:
... {{ Form::bsInput('event_name', 'Event title', 'event title goes here', $error = (($errors->has('event_name')) ? $errors->get('event_name') : false), $type = 'text', Input::old('event_name')) }} ...
Note:
bsInput
is a wrapper around Form::Input() to create bootstrap controls together with labelsController:
public function save() { if (Input::has('submitEventSave')) { $event = Mihirevent::find(Input::get(event_id)); $event_add = false; } else { $event = new Mihirevent(); $event_add = true; } if ($event === false) { // doing something else } else { $event->event_name = Input::get('event_name'); $event->event_slug = Input::get('event_slug'); $event->event_description = Input::get('event_description'); $event->event_location_text = Input::get('event_location_text'); $event->event_location_data = Input::get('event_location_data'); $event->event_status = Input::get('event_status'); $event->featured_image = Input::get('featured_image'); $event->event_date_from = Input::get('event_date_from'); $event->event_date_until = Input::get('event_date_until'); $validation_rules = $event_add === true?$event->rules:$event->update_rules; $inputs = array( 'event_name' => $event->event_name, 'event_slug' => $event->event_slug, 'event_location_text' => $event->event_location_text, ); $validator = Validator::make($inputs, $validation_rules); if ($validator->fails()) { Input::flash(); if ($event_add === true) { return Redirect::to('admin/event/create') ->withErrors($validator)->withInput(); } else { return Redirect::to('admin/event/edit/'.$event->event_id) ->withErrors($validator)->withInput(); } } // save MihirEvent::save(); // redirect to list return Redirect::route('adminEvent'); } }
Update:
bsInput
macro:Form::macro('bsInput', function($name, $text, $placeholder = null, $error = false, $type = 'text', $default = null, $class=null) { $label = Form::label($name, $text, array('class' => 'control-label')); $input = Form::input($type, $name, $default, array('placeholder' => $placeholder, 'class' => 'form-control'.($class?' '.$class:''))); $error_messages = false; if($error) { $error_messages = '<ol>'; foreach ($error as $value) { $error_messages .= '<li>'.$value.'</li>'; } $error_messages .= '</ol>'; } $html = '<div class="form-group'.(($error) ? ' has-error' : '').'">'; $html .= $label; $html .= $input; $html .= (($error_messages) ? '<div class="alert alert-danger">'.$error_messages.'</div>' : ''); $html .= '</div>'; return $html; });