horat1us / yii2-validation-exception
Yii2 Validation Exception
Installs: 1 853
Dependents: 3
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=7.4
- yiisoft/yii2: ^2.0.40
Requires (Dev)
- horat1us/yii2-asset-free: ^1.0
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.7
This package is auto-updated.
Last update: 2024-10-14 18:49:39 UTC
README
This package add additional methods and exception for model validation.
Main purpose is to prevent mistakes while models validations (forgot to check validation result for failse etc).
Previous implementation was available in horat1us/yii2-base package as ModelExceptionInterface and ModelException.
Installation
Using packagist.org:
composer require horat1us/yii2-validation-exception:^1.0
Structure
- Validation\Failure - common interface for validation errors.
Should be used when ActiveRecord is implemented out of project repository (working with dependencies).
- Validation\ExceptionTrait - implements Failure and provides static failure factory.
- Validation\Exception - Failure implementation using ExceptionTrait.
Should be used when ActiveRecord implemented in project repository and may implement interfaces and use traits.
- Validation\Failure\ModelInterface - extends
\yii\base\Model
with methods validateOrFail and checkOrFail.- Model - trait with implementation.
- [Validation\Failure\ActiveRecordInterface] - extends
\yii\db\ActiveRecord
with saveOrFail method.- ActiveRecord - trait with implementation.
Example
With extension for model and active record:
<?php namespace App; use Horat1us\Yii\Validation; use yii\base; class Model extends base\Model implements Validation\Failure\ModelInterface { use Validation\Failure\Model; public $id; public function rules(): array { return [ [['id',], 'required',], ]; } } $model = new Model; try { $model->validateOrFail(); } catch (Validation\Failure $failure) { // handle }
With exceptions factory:
<?php namespace App; use Horat1us\Yii\Validation; use yii\base; class Model extends base\Model { /** @var string */ public $id; public function rules() { return [ [['id',], 'required',], ]; } } $model = new Model; try { Validation\Exception::validateOrThrow($model); } catch (Validation\Failure $failure) { // handle }