How to use form validation in laravel

Laravel

Posted on 22 May 15 by Vickey
  545



We have inbuilt validation in laravel by using the laravel validation we can validate our form very easily In this tutorial i have a form which have name and email and password fields for validate this form i use the below code

$userdata = array(

  'name' => Input::get('name'),
  'email' => Input::get('email'),
 'password' => Input::get('password'),

);

$rules = array(
    'name'=>'required|alpha_num|min:5',
       'email'=>'required|unique:users,email|email',
      'password'=>'required|alpha_num|min:5'
);

$validator = Validator::make($userdata, $rules);

if($validator->fails())
{ 
    return Redirect::back()->withInput()->withErrors($validator);
}
else{
              // save record in databse

}

for show validation message on view use

     @if($errors->any())

        {{ implode ('', $errors->all('<p><strong>:message</strong></p>'))}}

    @endif

If this post help you reduce time to develop, you can help me to maintain this website :)
www.000webhost.com