visit
Photo by on
Now, before the request enters the application, the data it carries must be validated.
There are two ways to accomplish data validation in Laravel: Inside the controllers, or using Form requests.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function store(Request $request)
{
$request->validate([
'name' => 'required|string|min:3',
'email' => 'required|email|min:6',
]);
// here we know data are valid so we can pass them to database or other services
}
}
php artisan make:request StoreUserRequest
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreUserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|string|min:3',
'email' => 'required|email|min:6',
];
}
}
<?php
namespace App\Http\Controllers;
use App\Http\Requests\StoreUserRequest;
class UserController extends Controller
{
public function store(StoreUserRequest $request)
{
// here we know data are valid so we can pass them to database or other services
}
}
php artisan make:rule SecurePassword
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class SecurePassword implements Rule
{
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
return !in_array($value, [
'picture1',
'password',
'password1',
'12345678',
'111111',
...
]);
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The chosen password is unsecure. Try again with a less common string.';
}
}
Add two configuration property to store the api keys of the new services in the config/service.php
file:
return [
...,
'mailboxlayer' => [
'key' => env('MAILBOXLAYER_KEY'),
],
'vatlayer' => [
'key' => env('VATLAYER_KEY'),
],
];
php artisan make:rule EmailSpam
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class EmailSpam implements Rule
{
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
if (app()->environment('local')) {
return true;
}
return !config('services.mailboxlayer.key') || $this->check($value);
}
/**
* Perform email check.
*
* @param string $email
* @return bool
*/
protected function check(string $email): bool
{
try{
$response = file_get_contents('//apilayer.net/api/check?'.http_build_query([
'access_key' => config('services.mailboxlayer.key'),
'email' => '[mailbox-layer-account-email]',
'smtp' => 1,
]));
$response = json_decode($response, true);
return $response['format_valid'] && !$response['disposable'];
} catch (\Exception $exception) {
report($exception);
if (app()->environment('local')) {
return false;
}
// Don't block production environment in case of apilayer error
return true;
}
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'Invalid email address.';
}
}
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
class CurrentPassword implements Rule
{
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
return Hash::check($value, Auth::user()->password);
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'Your current password is incorrect.';
}
}