Lift provides three attributes to help you validate your model's public properties.
Rules
โ ๏ธ The rules will be validated only when you save your model (create or update)
The Rules attribute allows you to set your model's public properties validation rules the same way as you would do with the rules function on a FormRequest, but you can set it directly on your public properties.
You can also pass a second parameter to the Rules attribute to set a custom error message for the validation rule.
useIlluminate\Database\Eloquent\Model;useWendellAdriel\Lift\Attributes\Fillable;useWendellAdriel\Lift\Attributes\Rules;useWendellAdriel\Lift\Lift;finalclassProductextendsModel{useLift; #[Rules(['required','string'], ['required'=>'The Product name can not be empty'])] #[Fillable]publicstring $name;}
The validation messages work with localization, so you can set your messages ina lang file and under the hood, Lift will use the __() helper to get the message.
CreateRules
โ ๏ธ The rules will be validated only when you create your model
The CreateRules attribute works the same way as the Rules attribute, but the rules will be validated only when you create your model.
In the example below the name property will be validated with the set rules for both when creating and updating the model. The email and password properties will be validated only when creating the model.
useIlluminate\Database\Eloquent\Model;useWendellAdriel\Lift\Attributes\CreateRules;useWendellAdriel\Lift\Attributes\Fillable;useWendellAdriel\Lift\Attributes\PrimaryKey;useWendellAdriel\Lift\Attributes\Rules;useWendellAdriel\Lift\Lift;classUserextendsModel{useLift; #[PrimaryKey]publicint $id; #[Fillable] #[Rules(rules: ['required','string'], messages: ['required'=>'The user name cannot be empty'])]publicstring $name; #[Fillable] #[CreateRules(rules: ['required','email'], messages: ['required'=>'The user email cannot be empty'])]publicstring $email; #[Fillable] #[CreateRules(['required','string','min:8'])]publicstring $password;}
UpdateRules
โ ๏ธ The rules will be validated only when you update your model
The UpdateRules attribute works the same way as the Rules attribute, but the rules will be validated only when you update your model.
In the example below the name property will be validated with the set rules for both when creating and updating the model. The email and password properties will be validated only when updating the model.
useIlluminate\Database\Eloquent\Model;useWendellAdriel\Lift\Attributes\Fillable;useWendellAdriel\Lift\Attributes\PrimaryKey;useWendellAdriel\Lift\Attributes\Rules;useWendellAdriel\Lift\Attributes\UpdateRules;useWendellAdriel\Lift\Lift;classUserextendsModel{useLift; #[PrimaryKey]publicint $id; #[Fillable] #[Rules(rules: ['required','string'], messages: ['required'=>'The user name cannot be empty'])]publicstring $name; #[Fillable] #[UpdateRules(rules: ['required','email'], messages: ['required'=>'The user email cannot be empty'])]publicstring $email; #[Fillable] #[UpdateRules(['required','string','min:8'])]publicstring $password;}
Mixing Rules
You can also mix the three validation attributes to set different rules for creating and updating your model.
useIlluminate\Database\Eloquent\Model;useWendellAdriel\Lift\Attributes\CreateRules;useWendellAdriel\Lift\Attributes\Fillable;useWendellAdriel\Lift\Attributes\PrimaryKey;useWendellAdriel\Lift\Attributes\Rules;useWendellAdriel\Lift\Attributes\UpdateRules;useWendellAdriel\Lift\Lift;classUserextendsModel{useLift; #[PrimaryKey]publicint $id; #[Fillable] #[Rules(rules: ['required','string'], messages: ['required'=>'The user name cannot be empty'])]publicstring $name; #[Fillable] #[CreateRules(rules: ['required','email'], messages: ['required'=>'The user email cannot be empty'])] #[UpdateRules(['sometimes','email'])]publicstring $email; #[Fillable] #[CreateRules(['required','string','min:8'])] #[UpdateRules(rules: ['sometimes', 'string', 'min:8'], messages: ['min' => 'The password must be at least 8 characters long'])]
publicstring $password;}