You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1. Adding the `StevenFox\LaravelModelValidation\ValidatesAttributes` trait to the model.
37
-
2. Defining the rules via the `commonValidationRules()` method.
37
+
2. Defining the rules on the model via one or more of the available methods: `baseValidationRules()`, `validationRulesUniqueToCreating()`, `validationRulesUniqueToUpdating()`, `validationRulesForCreating()`, `validationRulesForUpdating()`.
38
38
39
39
```php
40
40
use Illuminate\Database\Eloquent\Model;
@@ -44,13 +44,15 @@ class ValidatingModel extends Model
44
44
{
45
45
use ValidatesAttributes;
46
46
47
-
protected function commonValidationRules(): array
47
+
protected function baseValidationRules(): array
48
48
{
49
49
return [
50
50
// rules go here as ['attribute' => ['rule1', 'rule2', ...]
51
51
// like a normal validation setup
52
52
];
53
53
}
54
+
55
+
// Other methods are available for more control over rules... see below.
54
56
}
55
57
56
58
$model = new ValidatingModel($request->json());
@@ -112,7 +114,7 @@ class ValidatingModel extends Model
112
114
/**
113
115
* Define rules that are common to both creating and updating a model record.
114
116
*/
115
-
protected function commonValidationRules(): array
117
+
protected function baseValidationRules(): array
116
118
{
117
119
return [
118
120
'name' => ['required', 'string', 'max:255'],
@@ -142,7 +144,7 @@ class ValidatingModel extends Model
142
144
143
145
/**
144
146
* Define the rules that are used when creating a record.
145
-
* If you overload this method on your model, the 'commonValidationRules'
147
+
* If you overload this method on your model, the 'baseValidationRules'
146
148
* will not be used by default.
147
149
*/
148
150
protected function validationRulesForCreating(): array
@@ -152,7 +154,7 @@ class ValidatingModel extends Model
152
154
153
155
/**
154
156
* Define the rules that are used when updating a record.
155
-
* If you overload this method on your model, the 'commonValidationRules'
157
+
* If you overload this method on your model, the 'baseValidationRules'
156
158
* will not be used by default.
157
159
*/
158
160
protected function validationRulesForUpdating(): array
@@ -163,13 +165,13 @@ class ValidatingModel extends Model
163
165
```
164
166
165
167
#### Unique Columns
166
-
Specifying an attribute as `unique` is a common validation need. Therefore, this package provides a shortcut that you can use in the `commonValidationRules()` method for your unique columns. The helper function will simply define a `Unique` rule for the attribute, and when the model record already exists in the database, the rule will automatically invoke the `ignoreModel($this)` method.
168
+
Specifying an attribute as `unique` is a common validation need. Therefore, this package provides a shortcut that you can use in the `baseValidationRules()` method for your unique columns. The helper function will simply define a `Unique` rule for the attribute, and when the model record already exists in the database, the rule will automatically invoke the `ignoreModel($this)` method.
167
169
168
170
```php
169
171
/**
170
172
* Define rules that are common to both creating and updating a model record.
0 commit comments