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()
]);
}
// ....
}
validate laravel
$validatedData = $request->validate([
'title' => ['required', 'unique:posts', 'max:255'],
'body' => ['required'],
]);
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...
}
validated() laravel
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 validate string
$email = 'fake@email';
$validator = Validator::make(['email' => $email], [
'email' => 'email'
]);
if ($validator->fails()) {
return 'Invalid email';
}
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