@@ -22,7 +22,7 @@ Here user model is mentioned as an example. You could use this in any model you
2222### User.php model
2323 use Enigma\ValidatorTrait;
2424
25- class User extends Model
25+ class User extends Model
2626 {
2727 use ValidatorTrait;
2828
@@ -32,40 +32,76 @@ Here user model is mentioned as an example. You could use this in any model you
3232 public static function boot()
3333 {
3434 parent::boot();
35-
35+
3636 // Add this method for validating the current model on model saving event
3737 static::validateOnSaving();
3838 }
3939
40+ public $validationRules = [
41+ 'name' => 'required|max:10',
42+ 'email' => 'required|email',
43+ ];
44+
45+ public $validationMessages = [
46+ 'name.required' => 'Name field is required.',
47+ 'email.email' => 'The given email is in invalid format.',
48+ ];
49+
50+ public $validationAttributes = [
51+ 'name' => 'User Name'
52+ ];
53+
54+ /**
55+ * Code to be executed before the validation goes here.
56+ */
57+ public function beforeValidation()
58+ {
59+ // Some code goes here..
60+ }
61+
62+ /**
63+ * Code to be executed after the validation goes here.
64+ */
65+ public function afterValidation()
66+ {
67+ // Some code goes here..
68+ }
69+ }
70+
71+ ### Other options
72+ You could mention the validation rules, attributes and messages as a property as well as method.
73+
4074 /**
4175 * Validation rules to validate.
42- *
76+ *
4377 * @return array
4478 */
4579 public function validationRules()
4680 {
81+ // You can process your code here and return the rules as however you want.
4782 return [
4883 'name' => 'required|max:10',
4984 'email' => 'required|email',
5085 ];
5186 }
52-
87+
5388 /**
5489 * Custom messages to replace the validation messages.
55- *
90+ *
5691 * @return array
5792 */
5893 public function validationMessages()
5994 {
95+ // You can process your code here and return the messages as however you want.
6096 return [
6197 'name.required' => 'Name field is required.',
6298 'email.email' => 'The given email is in invalid format.',
6399 ];
64100 }
65-
101+
66102 /**
67103 * Custom attribute names to replace the validation attribute name.
68- *
104+ *
69105 * @return array
70106 */
71107 public function validationAttributes()
@@ -74,25 +110,7 @@ Here user model is mentioned as an example. You could use this in any model you
74110 'name' => 'User Name'
75111 ];
76112 }
77-
78- /**
79- * Code to be executed before the validation goes here.
80- */
81- public function beforeValidation()
82- {
83- // Some code goes here..
84- }
85-
86- /**
87- * Code to be executed after the validation goes here.
88- */
89- public function afterValidation()
90- {
91- // Some code goes here..
92- }
93- }
94113
95- ### Other options
96114You could mention the validation only for creating itself or on any model event just add ` $model->validate() ` .
97115
98116 /**
@@ -101,20 +119,20 @@ You could mention the validation only for creating itself or on any model event
101119 public static function boot()
102120 {
103121 parent::boot();
104-
122+
105123 // You can mention like this for validating the model on custom events as your wish
106- static ::creating(function($model){
124+ self ::creating(function($model){
107125 $model->validate();
108126 });
109-
110- // Or you may an alias like `static ::validateOnCreating()`.
127+
128+ // Or you can make use of the alias `self ::validateOnCreating()`.
111129 }
112130
113- Refer the available methods in the validationTrait .
131+ Refer the available methods in the ValidationTrait .
114132
115133## License
116134
117135Laravel Model Validation is open-sourced software licensed under the [ MIT license] ( https://opensource.org/licenses/MIT ) .
118136
119137
120- [ ![ FOSSA Status] ( https://app.fossa.io/api/projects/git%2Bgithub.com%2Ftheriddleofenigma%2Flaravel-model-validation.svg?type=large )] ( https://app.fossa.io/projects/git%2Bgithub.com%2Ftheriddleofenigma%2Flaravel-model-validation?ref=badge_large )
138+ [ ![ FOSSA Status] ( https://app.fossa.io/api/projects/git%2Bgithub.com%2Ftheriddleofenigma%2Flaravel-model-validation.svg?type=large )] ( https://app.fossa.io/projects/git%2Bgithub.com%2Ftheriddleofenigma%2Flaravel-model-validation?ref=badge_large )
0 commit comments