Validation
About Laravel Validation

Laravel provide us builtin Laravel Validation so that Laravel provide us many validation rules.

Routes

<?php
Route::get('/myform','Usercontroller@index');
Route::post('/submitform','Usercontroller@submit');

View

<html>
<head></head>
<body>

@if (count($errors) > 0)
<div style="color:#F00;">
@foreach ($errors->all() as $error_msg)
{{ $error_msg }}<br>
@endforeach
</div>
@endif

{!! Form::open(['url' => 'submitform']) !!}
<table>
<tr>
<td>Name</td>
<td>{!! Form::text('username'); !!}</td>
</tr>
<tr>
<td>Email</td>
<td>{!! Form::text('email'); !!}</td>
</tr>
<tr>
<td>Gender</td>
<td>{!! Form::radio('gender', 'male'); !!}Male{!! Form::radio('gender', 'female'); !!}Female</td>
</tr>
<tr>
<td>City</td>
<td>{!! Form::select('city', ['' => 'Select City', '1' => 'Delhi', '2' => 'Mumbai']); !!}</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>{!! Form::submit('Submit') !!}</td>
</tr>
</table>
{!! Form::close() !!}
</body>
</html>

Controller

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;

class Usercontroller extends Controller
{
//
public function index(){
return view('myform');
}

public function submit(Request $request){

$this->validate($request,[
'username'=>'required|max:8',
'email'=>'required',
'city'=>'required',
'gender'=>'required'
]);
}
}


Previous