Gonçalo Marrafa
Gonçalo Marrafa

Reputation: 2113

Form input values with Laravel

I want to build a form that is going to be used to enter data for new records as well as for editing existing records.

How do i define the form input's value attribute knowing that it can be either blank or the existing record's value, if i'm creating a new one or editing, or the input entered by the user, if validation fails?

My current solution is to combine old() with an instance of the model i'm editing. As an example, in a form to edit a user's data, the value for the input element for the user's name would be something like old('name', !empty($user) ? $user->name : '').

Is this the right way to do this sort of thing or is there an easier (more artisan) way?

I'm using Laravel 5.2.

Thanks in advance.

Upvotes: 0

Views: 3917

Answers (4)

rcartwright
rcartwright

Reputation: 1

Or... instead of having a ternary every time in the view, you could just allow the same variable that you are using in the edit view to be available for the create view and then just assign that variable to an empty instance of the model which will allow each property of the object to have empty values. This would be done in the controller instead of the view.

So instead you could just do:

public function create()
{
    $user = New User;
    return view('add_user')->with('user', $user);
}

as edit might be something like

public function edit($id)
{
    $user = User::find($id); (or auth session id or whatever)
    return view('edit_user')->with('user', $user);
}

That way you could just have:

old('name', $user->name) 

in the form partial with $user->name as the default, which would be showing up as an empty string in the create view and showing the actual data in the edit view.

Upvotes: 0

trinvh
trinvh

Reputation: 1580

With form and localization I use 3 packages in every projects I have done:

Clean, easy for reading and contributing.

Upvotes: 1

Giedrius Kiršys
Giedrius Kiršys

Reputation: 5324

You should use Form Model binding and partials.
First create 3 files - create.blade.php, edit.blade.php, _form.blade.php. In _form.blade.php write something like that:

{!! Form::label('title', 'Title:') !!}
{!! Form::text('title', old('title')) !!}

create.blade.php

{!! Form::open(['route' => 'someroute']) !!}
@include('_form.blade.php')
{!! Form::submit('Create') !!}
{!! Form::close() !!}

edit.blade.php

{!! Form::model($model, ['route' => 'someroute']) !!}
@include('_form.blade.php')
{!! Form::submit('Save Changes') !!}
{!! Form::close() !!}

Read more about it here: https://laravelcollective.com/docs/5.2/html#form-model-binding

Explanatory update:
With Form Model binding all attributes that model have will be putted to form fields. So for example, our model have title field, so if we open form like so: Form::model($model) and put Form::text('title') field, it will be auto filled by plugin.

Upvotes: 2

Richard
Richard

Reputation: 119

There are a few options for working with update forms in Laravel 5. To keep my views cleaner, I prefer using the HTML/Form helper library from Laravel Collective. It's well documented and provides form-model binding out of the box, which handles default values without having to add the logic to your view. Just add the library via Composer ("laravelcollective/html": "5.2.*") and add the service provider and alias to your config/app.php. The HTML helper previously shipped with Laravel 4, but was moved to an external library when Laravel 5 was released.

If you prefer to avoid extra libraries, this post in the Laracast forum has a complete example of an update form with prepopulated fields.

Upvotes: 3

Related Questions