php - Define default values for Laravel form fields -
to pre-populate form field, can add 'value' form field in create.blade.php:
{{ form::text('title', 'some default title') }}
is there way task elsewhere (maybe in model or controller?). i'd have code form fields identical in create & edit view. thanks!
okay, here are... used laravel's form model binding in example. (i work user model/db table). if topic not clear you, take @ http://laravel.com/docs/html#form-model-binding
// controller class userscontroller extends basecontroller { ... // method show 'create' form & initialize 'blank' user's object public function create() { $user = new user; return view::make('users.form', compact('user')); } // method should store data sent form form (for new user) public function store() { print_r(input::all()); } // retrieve user's data db given id & show 'edit' form public function edit($id) { $user = user::find($id); return view::make('users.form', compact('user')); } // here should process form data user exists already. // modify/convert input data maybe, save then... public function update($id) { $user = user::find($id); print_r($user->toarray()); } ... }
and here come view file served controller.
// view file - self describing think <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>document</title> </head> <body> @if(!$user->id) {{ form::model($user, ['route' => 'admin.users.store']) }} @else {{ form::model($user, ['route' => ['admin.users.update', $user->id], 'method' => 'put']) }} @endif {{ form::text('firstname') }} {{ form::text('lastname') }} {{ form::submit() }} {{ form::close() }} </body> </html>
Comments
Post a Comment