Skip to content

Commit 3642601

Browse files
committed
change commonValidationRules() to baseValidationRules()
1 parent edcd231 commit 3642601

File tree

4 files changed

+13
-18
lines changed

4 files changed

+13
-18
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ composer require steven-fox/laravel-model-validation
3434
### The `ValidatesAttributes` Trait
3535
Add validation functionality to a Model by:
3636
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()`.
3838

3939
```php
4040
use Illuminate\Database\Eloquent\Model;
@@ -44,13 +44,15 @@ class ValidatingModel extends Model
4444
{
4545
use ValidatesAttributes;
4646

47-
protected function commonValidationRules(): array
47+
protected function baseValidationRules(): array
4848
{
4949
return [
5050
// rules go here as ['attribute' => ['rule1', 'rule2', ...]
5151
// like a normal validation setup
5252
];
5353
}
54+
55+
// Other methods are available for more control over rules... see below.
5456
}
5557

5658
$model = new ValidatingModel($request->json());
@@ -112,7 +114,7 @@ class ValidatingModel extends Model
112114
/**
113115
* Define rules that are common to both creating and updating a model record.
114116
*/
115-
protected function commonValidationRules(): array
117+
protected function baseValidationRules(): array
116118
{
117119
return [
118120
'name' => ['required', 'string', 'max:255'],
@@ -142,7 +144,7 @@ class ValidatingModel extends Model
142144

143145
/**
144146
* 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'
146148
* will not be used by default.
147149
*/
148150
protected function validationRulesForCreating(): array
@@ -152,7 +154,7 @@ class ValidatingModel extends Model
152154

153155
/**
154156
* 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'
156158
* will not be used by default.
157159
*/
158160
protected function validationRulesForUpdating(): array
@@ -163,13 +165,13 @@ class ValidatingModel extends Model
163165
```
164166

165167
#### 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.
167169

168170
```php
169171
/**
170172
* Define rules that are common to both creating and updating a model record.
171173
*/
172-
protected function commonValidationRules(): array
174+
protected function baseValidationRules(): array
173175
{
174176
return [
175177
'email' => [

src/ValidatesAttributes.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,15 +295,15 @@ public function clearMixinValidationRules(): static
295295
protected function validationRulesForCreating(): array
296296
{
297297
return [
298-
...$this->commonValidationRules(),
298+
...$this->baseValidationRules(),
299299
...$this->validationRulesUniqueToCreating(),
300300
];
301301
}
302302

303303
protected function validationRulesForUpdating(): array
304304
{
305305
return [
306-
...$this->commonValidationRules(),
306+
...$this->baseValidationRules(),
307307
...$this->validationRulesUniqueToUpdating(),
308308
];
309309
}
@@ -318,7 +318,7 @@ protected function validationRulesUniqueToCreating(): array
318318
return [];
319319
}
320320

321-
protected function commonValidationRules(): array
321+
protected function baseValidationRules(): array
322322
{
323323
return [];
324324
}

tests/Feature/ModelAttributeValidationTest.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@
55
use StevenFox\LaravelModelValidation\Tests\Fixtures\ValidatesWhenSavingModel;
66
use StevenFox\LaravelModelValidation\Tests\Fixtures\ValidatingModel;
77

8-
it('does stuff', function () {
9-
$m = new ValidatingModel();
10-
$mm = new ValidatesWhenSavingModel();
11-
12-
$mm->save();
13-
});
14-
158
it('registers a "creating" model event listeners when shouldValidateWhenSaving() is true', function () {
169
Event::forget('*');
1710
Event::fake();

tests/Fixtures/ValidatingModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class ValidatingModel extends Model
1515

1616
protected $guarded = [];
1717

18-
protected function commonValidationRules(): array
18+
protected function baseValidationRules(): array
1919
{
2020
return [
2121
'required_string' => ['required', 'string'],

0 commit comments

Comments
 (0)