Download the PHP package szunisoft/laravel-rule-groups without Composer
On this page you can find all versions of the php package szunisoft/laravel-rule-groups. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-rule-groups
Rule Groups
With Laravel it's very easy to run predefined validation rules and customize them. It is also possible to create your own rule in several ways.
One of our projects uses tons of validations and most of them are the same or very similar to each other. This package provides an easy way to make reusable validation rule groups.
The primary advantage of this package is the code reusability with centralized rule group controlling and managing.
Installing
If you are on a lower version of Laravel and you don't have package discovery yet please add the ServiceProvider to the configuration file.
Configuration
By default the package will generate all rule groups into the app/RuleGroups directory. You can change it by publishing the configuration file.
Creating Rule Groups
Writing Rule Groups
Locate the generated class. Default location is app/RuleGroups.
Basic Usage - Reusability
You can easily use rule groups in your controllers. See the example.
This will be equivalent with the following:
Now you can use this validation group in any other controllers or wherever you want to validate.
Advanced Usage - On demand configuration
In this chapter we'll take a closer look at how we can modify these groups in extremist situations.
Let's say we have a registration page where the user must specify the information of the managed company but we also need billing information. That's okay, easy and simple. But what if the managed company and the billing company are not the same one?
Take a look at the following Rule Group
Now let's use it in our controller
This will turn into this
Let's say it's not enough for us and we need almost the same rules but not exactly.
We want to validate the billed company inputs only when the user want to specify different one.
Available methods
After using the static method you can use the following ones:
Attribute management
addRulesTo($attribute, $rules)
Adds new rules to the attribute. Attribute will be created if not exists.overwriteRulesOf($attribute, $rules)
Overwrites rules of a specific attribute.without($attribute)
Removes an attribute and it's rules from the group.forgetRulesOf($attribute)
Removes all rules from an attribute.addToAll($rules)
Apply the given rules on all attributes.
Utility
restore()
Restores the group to it's initial state.prefix($prefix)
Applies prefix on all attributes. You can use deep dotting array access pattern (x.y.z)
Don't forget to invoke the method.