Setting default values and using error input with Laravel 4 forms? -
i'm using laravel 4 , built-in form component. i'm trying set can give form set of default values, if user submits , validator fails, use values previous input. code below, however, fails. session value 'threw_error' true reason, when form throws errors.
it seems done sessions, why i'm setting __old_input, internal key form uses. doing wrong? should using __old_input this, or there better way achieve goal?
i'm using resourceful controllers, index() below /, , store() post /.
class admin_detailscontroller extends admin_basecontroller { public function index() { $details = detail::all(); // true reason? if(!session::get('threw_error', false)) { session::put('__old_input', $details); } $data['message'] = session::get('message'); $this->layout->nest('content', 'admin.details.index', $data); } public function store() { $validator = $this->makevalidator(); if($validator->fails()) { return redirect::to('/admin/details')->witherrors($validator)->withinput()->with('threw_error', 1); } // process } } // in view {{ form::text('some_field') }}
edit: code works expected, more or less, though session::flashinput($details) better manually calling __old_input. tried removing fields in form see if there, , worked. in other words, think it's issue either local version of php, or config or - , not laravel issue.
as of laravel 4 can form::model($model, array) instead of form::open(array)
then don't have pass value form helper methods. value model, first, it's gonna check if there oldinput, value old one.
check out method. it's small change, smart 1 :)
but work, have redirect input
redirect::back()->withinput();
like so:
public function createnew() { $data = array( 'model' => new model(); ); $this->layout->nest('content', 'admin.details.form', $data); } public function store() { $validator = $this->makevalidator(); if($validator->fails()) { return redirect::back() ->witherrors($validator) ->withinput(); } } // in view {{ form::model($model, array(...)) }} // first time null // if validation fails, old value {{ form::text('some_field', 'some fields title') }} {{ form::close()}}
ps instead of using session::flashinput()
, can use input::old()
Comments
Post a Comment