Hire a web Developer and Designer to upgrade and boost your online presence with cutting edge Technologies

Monday, August 24, 2015

Form validation post and retain form data laravel

Form validation post and retain form data laravel :-

There is a basic thing to make a form, form validation (validate a form), post form data, and retain posted form data (get form data on controller) and save in database.
Below we will see all steps for Form validation, post and retain form data in laravel :-

1. Create forms in laravel

2. From validation in laravel

3. Post form data in laravel

4. Retain post data in controller laravel

first we need to create a form using blade templating then we need to call controller method to submit data and add validation and make routes for both get and post data.
View : Creating forms :-
Create a login.blade.php under view/pages/login.blade.php

@extends('layout.default')
@section('content')
     <div class="text-content">
        <div class="span7 offset1">
          <h1></h1>
           <div class="secure">Secure Login form</div>
           {{ Form::open(array('url'=>'account/login','method'=>'POST')) }}
           <div class="control-group">
            <div class="controls">
              {{ Form::text('email','',array('id'=>'','class'=>'form-control span6','placeholder' => 'Email or Customer Reference')) }}
	      <p class="errors">{{$errors->first('email')}}</p>
            </div>
           </div>
           <div class="control-group">
             <div class="controls">
             {{ Form::password('password',array('class'=>'form-control span6', 'placeholder' => 'Please Enter your Password')) }}
		<p class="errors">{{$errors->first('password')}}</p>
             </div>
	   </div>
           <div id="success"> </div>
           {{ Form::submit('Login', array('class'=>'send-btn')) }}
           {{ Form::close() }}
      </div>
@stop
Controller : Form validation, get post data:-
Create a AccountController.php under controller/AccountController.php

class AccountController extends BaseController {
	public function login()
	{
	      // Getting posted data.
              $data = Input::all();
              
              // Adding custom messages for validation
	      $messages = array(
			'email.required' => 'please enter email address',
			'password.required' => 'please enter password',
			);
              // apply validation rules to fields
	      $rules = array(
			'email' => 'required|email',
			'password' => 'required',
		       );
              // for default messages no need to pass $messages.
	      $validator = Validator::make($data, $rules, $messages);
	      if ($validator->fails()){
                // validation fails redirect back to page.
		return Redirect::to('/account/login')->withInput()->withErrors($validator);
	      }
	      else{
                // do your insertion stuff/ db stuff and then redirect.
		return Redirect::to('/account/login');
	      }
	}
}
Routes :- setting up routes
Add below code to app/routes.php

Route::get('account/login', function()
{
   return View::make('pages.login');
});
Route::post('account/login', 'AccountController@login');
For more information about validation rules follow Validation
Now we have all set form data submission in laravel, retrieve post data of form validation and routing in laravel.

No comments:

Post a Comment