pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API Laravel Form Validation Chris Sevilleja July 8, 2014 ·forms, laravel, validation 15 Comments PHP Advertisements Follow Follow Like BAR TALK TUTORIALS SERIES QUICK TIPS PROJECTS ABOUT
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API
Laravel FormValidation
Chris Sevilleja July 8, 2014 ·forms, laravel, validation 15 Comments
PHPAdvertisements
FollowFollowLike SCO TCH
BAR TALK TUTO RIALS SERIES Q UICK TIPS PRO JECTS ABO UT
pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API
·forms, laravel, validation 15 Comments
7
Share
21
Tweet
11
Plus
View Code
Today we’ll be handling form validation inLaravel. There are many things that need tohappen to make sure that everything works theway a user would expect it to so let’s get started.
pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API
Creating Rules
Let’s create those rules:
<?php // app/routes.php
...// route to process the ducks formRoute::post('ducks', function(){
// process the form here
// create the validation rules ------------------------ $rules = array( 'name' => 'required', // just a normal required validation 'email' => 'required|email|unique:ducks', // required and must be unique in the ducks table 'password' => 'required', 'password_confirm' => 'required|same:password' // required and has to match the password field );
// do the validation ---------------------------------- // validate against the inputs from our form $validator = Validator::make(Input::all(), $rules);
// our duck has passed all tests! // let him enter the database
// create the data for our duck $duck = new Duck; $duck->name = Input::get('name'); $duck->email = Input::get('email'); $duck->password = Hash::make(Input::get('password'));
// save our duck $duck->save();
// redirect ---------------------------------------- // redirect our user back to the form so they can do it all over again return Redirect::to('ducks');
pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API
// app/routes.php...// check if the validator failed ----------------------- if ($validator->fails()) { // redirect our user back with error messages $messages = $validator->messages();
// also redirect them back with old inputs so they dont have to fill out the form again // but we wont redirect them with the password they entered
public static $rules = array( 'name' => 'required', // just a normal required validation 'email' => 'required|email|unique:ducks', // required and must be unique in the ducks table 'password' => 'required', 'password_confirm' => 'required|same:password' // required and has to match the password field );
pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API
Comments Community
Sort by Best
Join the discussion…
• Reply •
Guest • 5 days ago
What i don't understand: Why do you put the validator rules inside the Duck model?
I thought this model is only responsible for the db table "ducks"? 1
• Reply •
Merlin • a day ago
What i don't understand: Why do you put the validator rules inside the Duck model?
I thought this model is only responsible for the db table "ducks"?
• Reply •
Merlin • 6 days ago
$duck = new Duck; .... $duck->save(); ............ is $duck automatically used a table name there? Because i can't findany $table="duck"; or something in your code.
pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API
• Reply •
Chris Sevilleja • 6 days agoBartender Merlin
When you create a new Duck, you are creating a new Eloquent model for Duck. When defining your model(Duck in this case), Eloquent knows to look at the plural version in the database. So it will look for a table.
You can specifically name your table using convention.
1
• Reply •
Guest • 5 days ago Chris Sevilleja
But if you need to change the db table name then you must change the Model class name annd all$duck-> in your scripts, right? I didn't like this a bit :)
• Reply •
Chris Sevilleja Bartender Guest
You don't have to do it that way. You could just specify the table name by using 'ducks';.
That would then cascade down to all the instances you pull in the Duck model.
• Reply •
Pete Houston • 12 days ago
Just a comment, in [Model Level Rules] section, the access level for "$rules" should be "public", if you access itoutside of "Duck" class.
Chris Sevilleja • 11 days agoBartender Pete Houston
pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API
• Reply •
• Reply •
Matt • 25 days ago
This is an awesome article. As someone looking to get deeper into Laravel, youve made it a lot easier!
Cheers!Mattlilly.io
• Reply •
Spazecookie • a month ago
Really great introduction! Btw: I Guess there's a little Typo :) you probably wanted the $rules property on the model tobe public.
• Reply •
Chris Sevilleja • 11 days agoBartender Spazecookie
Changed to public. Thanks!
• Reply •
Jason Lee • a month ago
Great tutorial for folks like me who are just learner Laravel. the conclusion) was a step at the end where you move the form processing into a controller.
Chris Sevilleja • a month agoBartender Jason Lee
I actually debated doing that, but for simplicities sake, kept it in the routes. To move it into a controller (let'ssay DuckController with a method called createDuck), you would move all that code into that method. Then in