Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/vjik/yii2-rules-validator

Yii2 validator with nested rules
https://github.com/vjik/yii2-rules-validator

Last synced: about 5 hours ago
JSON representation

Yii2 validator with nested rules

Awesome Lists containing this project

README

        

# Yii2 validator with nested rules

## Installation

The preferred way to install this extension is through [composer](https://getcomposer.org/download/):

```
composer require vjik/yii2-rules-validator
```

## Examples

### Use in model

```php
class MyModel extends Model
{

public $country;

public function rules()
{
return [
[
'country',
RulesValidator::class,
'rules' => [
['trim'],
['string', 'max' => 191],
['validateCountry'],
],
],
];
}

public function validateCountry($attribute, $params, $validator)
{
if (!in_array($this->$attribute, ['Russia', 'USA'])) {
$this->addError($attribute, 'The country must be either "Russia" or "USA".');
}
}
}
```

### Rule Inheritance

Rule class:

```php
class MyRulesValidator extends RulesValidator
{

protected function rules(): array
{
return [
['trim'],
['string', 'max' => 191],
['validateCountry'],
];
}

public function validateCountry($model, $attribute, $params, $validator)
{
if (!in_array($model->$attribute, ['Russia', 'USA'])) {
$model->addError($attribute, 'The country must be either "Russia" or "USA".');
}
}
}
```

Model:

```php
class MyModel extends Model
{

public $country;

public function rules()
{
return [
['country', MyRulesValidator::class],
];
}
}
```