Laravel 5.3 - Validation, Validation in laravel 5.3 or at the all applications is the most important aspect while designing of them. It validates the incoming data. By default, base controller class uses a Validates Requests trait which provides a convenient method to validate incoming HTTP requests with a variety of powerful validation rules.
At the last Lessons, we have learn about Forms & HTML Class in laravel 5.3 and localization for Multiple languages in laravel 5.3
How to use Validation in Laravel 5.3
In your Laravel 5.3 project, create new Controller using Artisan CLI following this command
php artisan make:controller ValidationController
your new controller ValidationController has created and stored on app\Http\Controllers\Validation.php
add some function in our ValidationController.php
ValidationController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class ValidationController extends Controller
{
// create new function to show the form login as a our index
public function ShowForm(){
return view('loginex');
}
// create function for our Validation
public function ValidateForm(Request $request){
print_r($request->all());
$this->validate($request,[
'username'=>'required|max:10',
'password'=>'required'
]);
}
}
Now, create new views to show our Login form, that stored on resources\views, at the preview Lessons, we have learn about Authentication Login & Registration Form + Bootstrap in laravel 5.3.
loginex.blade.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Validate Login</title>
</head>
<body>
@if (count($errors) > 0)
<div class="error">
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<?php
echo Form::open(['url'=>'/validation']);
?>
<table border="1">
<tr>
<td>
Login
</td>
</tr>
<tr>
<td>
username
</td>
<td>
<?php echo Form::text('username'); ?>
</td>
</tr>
<tr>
<td>
Password
</td>
<td>
<?php echo Form::password('password'); ?>
</td>
</tr>
<tr>
<td>
Password
</td>
<td>
<?php echo Form::submit('login'); ?>
</td>
</tr>
</table>
<?php echo Form::close(); ?>
</body>
</html>
Next, create new routes that stored on \routes\web.php
web.php
Route::get('/validation', 'ValidationController@ShowForm');
Route::post('/validation', 'ValidationController@ValidateForm');
Next, try to access our validation on your browser following this command
http://localhost:8080/validation
Video Tutorial How to use Validation in Laravel 5.3
See you next Lessions ...
Không có nhận xét nào:
Đăng nhận xét