DekGenius.com
PHP
laravel form validation
use IlluminateSupportFacadesValidator;
// ....
// On your Store function
public function store(Request $request, $id)
// Validation
$validator = Validator::make($request, [
'name' => 'required',
'username' => 'required|unique:users,username,NULL,id,deleted_at,NULL',
'email' => 'nullable|email|unique:users,email,NULL,id,deleted_at,NULL',
'password' => 'required',
]);
// Return the message
if($validator->fails()){
return response()->json([
'error' => true,
'message' => $validator->errors()
]);
}
// ....
}
// On your Update function
public function update(Request $request, $id)
{
// Validation
$validator = Validator::make($input, [
'name' => 'required',
'username' => 'required|unique:users,username,' . $id. ',id,deleted_at,NULL',
'email' => 'nullable|email|unique:users,email,' . $id. ',id,deleted_at,NULL',
'roles' => 'required'
]);
// Return the message
if($validator->fails()){
return response()->json([
'error' => true,
'msg' => $validator->errors()
]);
}
// ....
}
validation in laravel
use IlluminateSupportFacadesValidator;
class PostController extends Controller
{
/**
* Store a new blog post.
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
if ($validator->fails()) {
return redirect('post/create')
->withErrors($validator)
->withInput();
}
// Retrieve the validated input...
$validated = $validator->validated();
// Retrieve a portion of the validated input...
$validated = $validator->safe()->only(['name', 'email']);
$validated = $validator->safe()->except(['name', 'email']);
// Store the blog post...
}
}
laravel validation
$this->validate([ // 1st array is field rules
'userid' =>'required|min:3|max:100',
'username' =>'required|min:3',
'password' =>'required|max:15|confirmed',
], [ // 2nd array is the rules custom message
'required' => 'The :attribute field is mandatory.'
], [ // 3rd array is the fields custom name
'userid' => 'User ID'
]);
laravel validation
use IlluminateSupportFacadesValidator;
$customMessage = [
'title.max' => "title is too large",
];
$rules = [
'id' => 'integer|exists:master_advert_bundles',
'title' => ['required', 'unique:posts', 'max:255'],
'body' => ['required']
];
$validate = validation($request->all(), $rules);
$validate = Validator::make($request->all(), $rules, $customMessage);
if ($validate->fails()) {
return $validate->messages();
}
Laravel Validation
$validated = $request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
validation laravel
/**
* Store a new blog post.
*
* @param IlluminateHttpRequest $request
* @return IlluminateHttpResponse
*/
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
// The blog post is valid...
}
laravel validation types
# <values> = foo,bar,...
# <field> = array field
# <characters> = amount of characters
# accepted # active_url
# after:<tomorrow> # after_or_equal:<tomorrow>
# alpha # alpha_dash
# alpha_num # array
# bail # before:<today>
# before_or_equal:<today> # between:min,max
# boolean # confirmed
# date # date_equals:<today>
# date_format:<format> # different:<name>
# digits:<value> # digits_between:min,max
# dimensions:<min/max_with> # distinct
# email # ends_with:<values>
# exclude_if:<field>,<value> # exclude_unless:<field>,<value>
# exists:<table>,<column> # file
# filled # gt:<field>
# gte:<field> # image
# in:<values> # in_array:<field>
# integer # ip
# ipv4 # ipv6
# json # lt:<field>
# lte:<field> # max:<value>
# mimetypes:video/avi,... # mimes:jpeg,bmp,png
# min:<value> # not_in:<values>
# not_regex:<pattern> # nullable
# numeric # password:<auth guard>
# present # regex:<pattern>
# required # required_if:<field>,<value>
# required_unless:<field>,<value> # required_with:<fields>
# required_with_all:<fields> # required_without:<fields>
# required_without_all:<fields> # same:<field>
# size:<characters> # starts_with:<values>
# string # timezone
# unique:<table>,<column> # url
# uuid
Laravel validation
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
if ($validator->fails()) {
return redirect('post/create')
->withErrors($validator)
->withInput();
}
// Retrieve the validated input...
$validated = $validator->validated();
// Retrieve a portion of the validated input...
$validated = $validator->safe()->only(['name', 'email']);
$validated = $validator->safe()->except(['name', 'email']);
// Store the blog post...
}
laravel validation
use IlluminateValidationRule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);
laravel validation
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Validator::extend(...);
Validator::replacer('foo', function ($message, $attribute, $rule, $parameters) {
return str_replace(...);
});
}
laravel validation
laravel form validation
php artisan make:controller ValidationController --plain
laravel validation
Validator::extendImplicit('foo', function ($attribute, $value, $parameters, $validator) {
return $value == 'foo';
});
laravel validation
Rule::unique('users')->ignore($user->id, 'user_id')
laravel validation
Validator::extend('foo', 'FooValidator@validate');
laravel validation
Rule::unique('users')->ignore($user)
laravel validation
"foo" => "Your input was invalid!",
"accepted" => "The :attribute must be accepted.",
// The rest of the validation error messages...
laravel validation
$rules = ['name' => 'unique:users,name'];
$input = ['name' => ''];
Validator::make($input, $rules)->passes(); // true
© 2022 Copyright:
DekGenius.com