Skip to content

Commit c3cd189

Browse files
Improvements for declaring the methods as properties in the eloquent model.
1 parent 5311283 commit c3cd189

File tree

2 files changed

+100
-42
lines changed

2 files changed

+100
-42
lines changed

README.md

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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
96114
You 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

117135
Laravel 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)

src/ModelValidator.php

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,9 @@ public function __construct($model)
5555
*/
5656
public function initialize()
5757
{
58-
if (method_exists($this->model, 'validationMessages')) {
59-
$this->customMessages = array_merge($this->customMessages, $this->model->validationMessages());
60-
}
61-
62-
if (method_exists($this->model, 'validationAttributes')) {
63-
$this->customAttributes = array_merge($this->customAttributes, $this->model->validationAttributes());
64-
}
65-
66-
if (method_exists($this->model, 'validationRules')) {
67-
$this->rules = array_merge($this->rules, $this->model->validationRules());
68-
}
58+
$this->customMessages = $this->getMessages();
59+
$this->customAttributes = $this->getAttributes();
60+
$this->rules = $this->getRules();
6961

7062
return $this;
7163
}
@@ -86,4 +78,52 @@ public function validate()
8678

8779
return $this;
8880
}
81+
82+
/**
83+
* Get the validation messages.
84+
*
85+
* @return array
86+
*/
87+
protected function getMessages()
88+
{
89+
if (method_exists($this->model, 'validationMessages')) {
90+
return $this->model->validationMessages();
91+
}
92+
93+
if(property_exists($this->model, 'validationMessages')) {
94+
return $this->model->validationMessages;
95+
}
96+
}
97+
98+
/**
99+
* Get the validation attributes.
100+
*
101+
* @return array
102+
*/
103+
protected function getAttributes()
104+
{
105+
if (method_exists($this->model, 'validationAttributes')) {
106+
return $this->model->validationAttributes();
107+
}
108+
109+
if(property_exists($this->model, 'validationAttributes')) {
110+
return $this->model->validationAttributes;
111+
}
112+
}
113+
114+
/**
115+
* Get the validation rules.
116+
*
117+
* @return array
118+
*/
119+
protected function getRules()
120+
{
121+
if (method_exists($this->model, 'validationRules')) {
122+
return $this->model->validationRules();
123+
}
124+
125+
if(property_exists($this->model, 'validationRules')) {
126+
return $this->model->validationRules;
127+
}
128+
}
89129
}

0 commit comments

Comments
 (0)