Forms are often used to interact with a specific model such as a user or a blog post. However, in many circumstances a form may collect data that is related to multiple data models.
It may also have special validation requirements that have little to do with the underlying data, such as captcha and password confirmation. Consequently, it often makes sense to create a form model.
A form model represents the data needs of a form. This may be validation alone, storing values for form select drop-downs, having custom methods to generate data for the form, or managing persistent data in a session to make multi-page forms simple.
In this video I discuss modeling forms and introduce a form base model for Laravel.
More documentation and information can be found on the project's Github page .
1 Comments
codedungeon posted...
My choices are to store as a bitwise field, or as a delimited field and assume I handle parsing accordingly myself. Or, is there a "magic" way of handling this with the Form-Box-Model bundle?
codedungeon posted...
I am trying to figure out the fastest / best way to manage forms which have checkbox arrays. Assuming I have some 10 items, trying to determine how to store the data so that it can be "edited" or use Input::old in case of validation failure.
How are you storing data in the table, and ultimately how are you reloading forms so the appropriate checkbox(es) are checked when editing a form.
So far as I can tell, the native Laravel form helper does not really support checkbox arrays so I am looking at using Form-Box-Model
Tuấn Âu posted...
did you creat this blog with laravel ?
Tuấn Âu posted...
Could you teach me how to custom pagination of laravel like your blog?
Shawn McCool posted...
Yes sir!
Shawn McCool posted...
The way that I usually list errors is on a per field basis. With bootstrap that may look something like this:
{{ Form::label( 'name', 'Title', array( 'class' => 'control-label')) }}
{{ Form::text( 'name', ProgramForm::old( 'name'), array('class' => 'span7')) }}
{{ $errors->first( 'name', ':message ' ) }}
Shawn McCool posted...
The form model models forms. Sometimes its uses are as basic as form validation. But, often forms have more complex needs. For example, select boxes need populated, checkbox arrays need to be generated from values, etc. It's become normal to put this sort of thing into the controller, or into a data model, but this provides better separation.
To load an object from the database into a form model you would just do something like:
$user = User::find( 1 );
UserForm::load( $user );
There are some examples in the actual Form Base Model Laravel bundle linked to above.
You can also get more information from the Laravel IRC channel #Laravel on irc.freenode.net
Shawn McCool posted...
I just do application/models/forms/whatever.php
Shawn McCool posted...
For me one big reason is that having tables that do the same thing is more work in every way and without a specific benefit for them to be in the database I would prefer not to expend my time or the client's money.
However, there's nothing wrong with storing them in the database. For example, If I need to create a list of select box items or whatever I would just throw a new method into my form model and have it do the querying.
Or, maybe there is a Project object which has a relationship of has_many to Worker objects. If I need to have a checkbox array with all of the worker objects I can create a method that will just loop through the loaded eloquent model's children and create the list. By doing this I can 1) cache the computation's results (usually not important) and 2) abstract my Objects out of the form.
By this I mean.. If I want to use the same form for both add and edit actions I could do stuff like.. isset( $user ) ? $user->name : '' to default the value, etc. But, to me that's messy.
So, in the controller I'd do something like:
$user = User::find( 1 );
UserForm::load( $user );
Then the add.blade.php view would look like:
Add User
@include( 'users._user_form' );
Then the edit.blade.php view would look like:
Add User
@include( 'users._user_form' );
and finally the views/users/_user_form.blade.php would look like:
{{ Form::open() }}
{{ Form::text( 'name', UserForm::old( 'name' ) ) }}
{{ Form::submit( 'Save' ) }}
{{ Form::close() }}
These are just some rough examples. But, here you can see that this form would work for add or edit since even if the UserForm class doesn't have a loaded object it won't return an error.
Marcin Mucha posted...
Great work, thank you.
I've got 2 questions:
1. If I understand good, this form-model is only to help build forms, but to save results to database should I use "normal" model, is that right?
2. how to connect retrieving data from database with form-model to update them and validate?
It would be great to see a tutorial with working example.
Thanks
Martin
Haso Keric posted...
Shawn im kinda lost where do i place my forms within the bundle directory? also does that mean my layout needs to be in the bundle directory too? how do i make this work with application/views and application/controllers
Glenn Williams posted...
This is great. I'm using it as the base of my first Laravel project and am really grateful for your contributions. One problem I've had getting into Laravel is knowing who to listen to since some contributors seem to want to just make Laravel work the way they are used to in another framework. As the documentation is understandably a bit lacking in some areas at this early stage it makes it all a bit difficult for newbies like me whose PHP skills maybe aren't what they should be. But your contributions (and Andrew Perk's screen casts) really hit the spot.
I needed to enter loads of data over for pages and this has made it a breeze for me but also I need to make any saved records subsequently editable and will try to integrate this with what you've done since this seems the best way to proceed.
And finally I used the code below to list all errors on each page (I just put an 'x' by each individual field) which some fellow newbies may find useful or you may tell me I'm doing it wrong and there is a better or more elegant way to do it.
@if ($errors->messages)
@foreach($errors->messages as $messagegrp) @foreach($messagegrp as $message) {{ $message }} @endforeach @endforeach
Thanks again
Eizil posted...
This is really nice, i wonder how can I populate ajax value base on the selection user made from the drop down menu?
Andy Rundquist posted...
How would you compare and contrast this way of saving pull-down menu items versus having a table in your db do the same thing?
Alex Leonard posted...
Thanks for this video Shawn. Definitely much appreciated as it helped me quickly get my head around this neat little bundle.
Nearly got a multi-page registration form sorted now :)