“This is the 16th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Form validation

How to set dynamic authentication rules?

If our constraint rules are dynamically dependent on other conditions, then we can dynamically create an array of rules

public function save(Request $request) { $validationArray = [ 'title' => 'required', 'name' => 'required', 'logo' => 'file|max:1024', 'position' => 'required', ]; if (! Auth::check()) { $validationArray = array_merge($validationArray, [ 'email' => 'required|email|unique:users', 'password' => 'required|confirmed|min:5', 'name' => 'required' ]); } / /}Copy the code

Custom throw 422 status code

If we don’t use the validate () or the Form Request, but still need to use the same 422 status code throw an error and error structure, you can manually throw throw thrown: : withMessages ()

This function is really not used when no feeling, use when feel really elegant ~

if (! $user || ! Hash: : check ($request - > ", $user - > ")) {throw thrown: : withMessages ([' email '= > /' E-mail format is not correct, ]); }Copy the code

Image authentication

We can specify the desired size when verifying uploaded images

['image' => 'dimensions:max_width=1096,max_height=1096']
Copy the code

Custom validation error information

We need only in resources/lang/xx/validation. PHP file to create the appropriate array structure, can be defined for each field validation error messages, rules, and language.

'custom' = > [' email '= > [' required' = > 'email required,],].Copy the code

Validation rules for certain conditions

If the validation rule depends on certain conditions, you can modify the rule by adding withValidator() to the FormRequest class and specifying custom logic there.

For example, if we only want to add validation rules for certain user roles.

use Illuminate\Validation\Validator; class XxxxxRequest extends FormRequest { public function withValidator(Validator $validator) { if (auth()->user()->is_admin) { $validator->addRules(['xxxx' => 'required']); }}}Copy the code

Change the default validation message

If you want to change the default validation error messages for specific fields and specific validation rules, simply add the Messages () method to the FormRequest class.

class UserRequest extends FormRequest { public function rules() { return ['name' => 'required']; } public function Messages () {return ['name.required' => 'Please fill in the name ']; }}Copy the code

Prospective validation

We want to modify a field before the default Laravel validation, (similar to the usual beforeXxx(){}, afterXxx(){})

The FormRequest class has a method prepareForValidation () that does what we need:

Modify fields before validation

protected function prepareForValidation()
{
    $this->merge([
        'myBlog' => Illuminate\Support\Str::slug($this->myBlog),
    ]);
}
Copy the code

Terminate the program on the first validation error

By default, Laravel validation errors are returned in the list and all validation rules are checked.

But if we want to terminate the process after the first error, use a validation rule called Bail:

$request->validate([
    'title' => 'bail|required|unique:posts|max:255',
    'body' => 'required',
]);
Copy the code

Last but not least

Technical group please come here. Or add my wechat account wangzhongyang0601 to learn and make progress together.

Thank you for your likes, comments and attention. Thank you for your support and thanks to ღ(´ · ᴗ · ‘)