Skip to content

Commit

Permalink
Merge pull request #7 from theriddleofenigma/option-to-assign-properties
Browse files Browse the repository at this point in the history
Improvements for declaring the methods as properties in the eloquent …
  • Loading branch information
theriddleofenigma authored Oct 27, 2020
2 parents 8cbd165 + 73a590b commit 06603cc
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 42 deletions.
80 changes: 49 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Here user model is mentioned as an example. You could use this in any model you
### User.php model
use Enigma\ValidatorTrait;

class User extends Model
class User extends Model
{
use ValidatorTrait;

Expand All @@ -32,40 +32,76 @@ Here user model is mentioned as an example. You could use this in any model you
public static function boot()
{
parent::boot();

// Add this method for validating the current model on model saving event
static::validateOnSaving();
}

public $validationRules = [
'name' => 'required|max:10',
'email' => 'required|email',
];

public $validationMessages = [
'name.required' => 'Name field is required.',
'email.email' => 'The given email is in invalid format.',
];

public $validationAttributes = [
'name' => 'User Name'
];

/**
* Code to be executed before the validation goes here.
*/
public function beforeValidation()
{
// Some code goes here..
}

/**
* Code to be executed after the validation goes here.
*/
public function afterValidation()
{
// Some code goes here..
}
}

### Other options
You could mention the validation rules, attributes and messages as a property as well as method.

/**
* Validation rules to validate.
*
*
* @return array
*/
public function validationRules()
{
// You can process your code here and return the rules as however you want.
return [
'name' => 'required|max:10',
'email' => 'required|email',
];
}

/**
* Custom messages to replace the validation messages.
*
*
* @return array
*/
public function validationMessages()
{
// You can process your code here and return the messages as however you want.
return [
'name.required' => 'Name field is required.',
'email.email' => 'The given email is in invalid format.',
];
}

/**
* Custom attribute names to replace the validation attribute name.
*
*
* @return array
*/
public function validationAttributes()
Expand All @@ -74,25 +110,7 @@ Here user model is mentioned as an example. You could use this in any model you
'name' => 'User Name'
];
}
/**
* Code to be executed before the validation goes here.
*/
public function beforeValidation()
{
// Some code goes here..
}
/**
* Code to be executed after the validation goes here.
*/
public function afterValidation()
{
// Some code goes here..
}
}

### Other options
You could mention the validation only for creating itself or on any model event just add `$model->validate()`.

/**
Expand All @@ -101,20 +119,20 @@ You could mention the validation only for creating itself or on any model event
public static function boot()
{
parent::boot();

// You can mention like this for validating the model on custom events as your wish
static::creating(function($model){
self::creating(function($model){
$model->validate();
});
// Or you may an alias like `static::validateOnCreating()`.

// Or you can make use of the alias `self::validateOnCreating()`.
}

Refer the available methods in the validationTrait.
Refer the available methods in the ValidationTrait.

## License

Laravel Model Validation is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).


[![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)
[![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)
62 changes: 51 additions & 11 deletions src/ModelValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,9 @@ public function __construct($model)
*/
public function initialize()
{
if (method_exists($this->model, 'validationMessages')) {
$this->customMessages = array_merge($this->customMessages, $this->model->validationMessages());
}

if (method_exists($this->model, 'validationAttributes')) {
$this->customAttributes = array_merge($this->customAttributes, $this->model->validationAttributes());
}

if (method_exists($this->model, 'validationRules')) {
$this->rules = array_merge($this->rules, $this->model->validationRules());
}
$this->customMessages = $this->getMessages();
$this->customAttributes = $this->getAttributes();
$this->rules = $this->getRules();

return $this;
}
Expand All @@ -86,4 +78,52 @@ public function validate()

return $this;
}

/**
* Get the validation messages.
*
* @return array
*/
protected function getMessages()
{
if (method_exists($this->model, 'validationMessages')) {
return $this->model->validationMessages();
}

if (property_exists($this->model, 'validationMessages')) {
return $this->model->validationMessages;
}
}

/**
* Get the validation attributes.
*
* @return array
*/
protected function getAttributes()
{
if (method_exists($this->model, 'validationAttributes')) {
return $this->model->validationAttributes();
}

if (property_exists($this->model, 'validationAttributes')) {
return $this->model->validationAttributes;
}
}

/**
* Get the validation rules.
*
* @return array
*/
protected function getRules()
{
if (method_exists($this->model, 'validationRules')) {
return $this->model->validationRules();
}

if (property_exists($this->model, 'validationRules')) {
return $this->model->validationRules;
}
}
}

0 comments on commit 06603cc

Please sign in to comment.