PHP code example of carono / yii2-validation-exception

1. Go to this page and download the library: Download carono/yii2-validation-exception library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

carono / yii2-validation-exception example snippets


use carono\yii2\exceptions\ValidationException;
use yii\base\Model;

$model = new Model();
$model->addError('attribute', 'Validation error message');

// Throw exception with the first validation error
throw new ValidationException($model);

// Or use it in a try-catch block
try {
    if (!$model->validate()) {
        throw new ValidationException($model);
    }
    // Continue with valid model
} catch (ValidationException $e) {
    // Access the error message
    echo $e->getMessage(); // "Validation error message"
    
    // Access the model instance
    $failedModel = $e->model;
}

php composer.phar