@@ -22,7 +22,7 @@ Here user model is mentioned as an example. You could use this in any model you
22
22
### User.php model
23
23
use Enigma\ValidatorTrait;
24
24
25
- class User extends Model
25
+ class User extends Model
26
26
{
27
27
use ValidatorTrait;
28
28
@@ -32,40 +32,76 @@ Here user model is mentioned as an example. You could use this in any model you
32
32
public static function boot()
33
33
{
34
34
parent::boot();
35
-
35
+
36
36
// Add this method for validating the current model on model saving event
37
37
static::validateOnSaving();
38
38
}
39
39
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
+
40
74
/**
41
75
* Validation rules to validate.
42
- *
76
+ *
43
77
* @return array
44
78
*/
45
79
public function validationRules()
46
80
{
81
+ // You can process your code here and return the rules as however you want.
47
82
return [
48
83
'name' => 'required|max:10',
49
84
'email' => 'required|email',
50
85
];
51
86
}
52
-
87
+
53
88
/**
54
89
* Custom messages to replace the validation messages.
55
- *
90
+ *
56
91
* @return array
57
92
*/
58
93
public function validationMessages()
59
94
{
95
+ // You can process your code here and return the messages as however you want.
60
96
return [
61
97
'name.required' => 'Name field is required.',
62
98
'email.email' => 'The given email is in invalid format.',
63
99
];
64
100
}
65
-
101
+
66
102
/**
67
103
* Custom attribute names to replace the validation attribute name.
68
- *
104
+ *
69
105
* @return array
70
106
*/
71
107
public function validationAttributes()
@@ -74,25 +110,7 @@ Here user model is mentioned as an example. You could use this in any model you
74
110
'name' => 'User Name'
75
111
];
76
112
}
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
- }
94
113
95
- ### Other options
96
114
You could mention the validation only for creating itself or on any model event just add ` $model->validate() ` .
97
115
98
116
/**
@@ -101,20 +119,20 @@ You could mention the validation only for creating itself or on any model event
101
119
public static function boot()
102
120
{
103
121
parent::boot();
104
-
122
+
105
123
// 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){
107
125
$model->validate();
108
126
});
109
-
110
- // Or you may an alias like `static ::validateOnCreating()`.
127
+
128
+ // Or you can make use of the alias `self ::validateOnCreating()`.
111
129
}
112
130
113
- Refer the available methods in the validationTrait .
131
+ Refer the available methods in the ValidationTrait .
114
132
115
133
## License
116
134
117
135
Laravel Model Validation is open-sourced software licensed under the [ MIT license] ( https://opensource.org/licenses/MIT ) .
118
136
119
137
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