TOPIC COLLECTION
Validation.
A collection of 24 practical ideas to make everyday Laravel work a little smoother.
24 TIPS01Image ValidationWhile validating uploaded images, you can specify the dimensions you require.Validation02Add Values to the Form Request After ValidationPractical Laravel guidance for add values to the form request after validation, with examples and copyable PHP code.Validation03Access Model Binding in FormrequestsWhen using FormRequests, you can always access the binding model by simply using the following expression $𝘁𝗵𝗶𝘀-…Validation04Rule Which Ensures the Field Under Validation Is Required If Another Field Is AcceptedYou can use required if accepted validation rule which ensures the field under validation is required if another field is accepted (a value of yes, on, 1,…Validation05Custom Validation Error MessagesYou can customize validation error messages per field , rule and language - just create a specific language file resources/lang/xx/validation.php with…Validation06Validate Dates with "Now" or "Yesterday" WordsYou can validate dates by rules before/after and passing various strings as a parameter, like: tomorrow , now , yesterday . Example: 'start date' =…Validation07Validation Rule with Some ConditionsIf your validation rules depend on some condition, you can modify the rules by adding withValidator() to your FormRequest class, and specify your custom…Validation08Change Default Validation MessagesIf you want to change default validation error message for specific field and specific validation rule, just add a messages() method into your FormRequest…Validation09Prepare for ValidationIf you want to modify some field before default Laravel validation, or, in other words, "prepare" that field, guess what - there's a method…Validation10Stop on First Validation ErrorBy default, Laravel validation errors will be returned in a list, checking all validation rules. But if you want the process to stop after the first error,…Validation11Throw 422 Status Code without Using Validate() or Form RequestIf you don't use validate() or Form Request, but still need to throw errors with the same 422 status code and error structure, you can do it manually throw…Validation12Rules Depending on Some Other ConditionsIf your rules are dynamic and depend on some other condition, you can create that array of rules on the flyValidation13With Rule::when() We Can Conditionally Apply Validation RulesThanks to Rule::when() we can conditionally apply validation rules in laravel. In this example we validate the value of the vote only if the user can…Validation14Use This Property in the Request Classes to Stop the Validation of the Whole Request AttributesUse this property in the request classes to stop the validation of the whole request attributes.Validation15Rule::unique Doesn't Take into the Softdeletes Global Scope Applied on the ModelStrange that Rule::unique doesn't take into the SoftDeletes Global Scope applied on the Model, by default.Validation16Validator::sometimes() Method Allows Us to Define When a Validation Rule Should Be AppliedThe laravel Validator::sometimes() method allows us to define when a validation rule should be applied, based on the input provided.Validation17Array Elements ValidationIf you want to validate elements of an array that you submited use dot notation in rules with '\ 'Validation18Password::defaults MethodYou can enforce specific rules when validating user-supplied passwords by using the Password::defaults method. It includes options for requiring letters,…Validation19Form Requests for Validation Redirectionwhen using Form Requests for validation, by default the validation error will redirect back to the previous page, but you can override it. Just define the…Validation20Mac Validation RuleNew mac address validation rule added in Laravel 8.77Validation21Validate Email with Tld Domain RequiredBy default, the email validation rule will accept an email without tld domain (ie: user@laravel )Validation22New Array Validation Rule Required_array_keysLaravel 8.82 adds a required array keys validation rule. The rule checks that all of the specified keys exist in an array.Validation23Position Placeholder in Validation MessagesIn Laravel 9 you can use the :position placeholder in validation messages if you're working with arrays.Validation24Exclude Validation ValueWhen you need to validate a field, but don't actually require it for anything e.g. 'accept terms and conditions', make use of the exclude rule. That way,…Validation