PHP code example of carono / yii2-rule-helper

1. Go to this page and download the library: Download carono/yii2-rule-helper 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-rule-helper example snippets


use carono\yii2\helpers\RuleHelper;

// Returns 'yii\validators\RequiredValidator');

// Returns 'string'
$shortName = RuleHelper::getValidatorShortName('yii\validators\StringValidator');

// Returns null for non-built-in validators
$shortName = RuleHelper::getValidatorShortName('app\validators\CustomValidator');

use carono\yii2\helpers\RuleHelper;
use app\models\User;

// Using validator class name
$hasRequired = RuleHelper::haveValidator(User::class, 'username', 'yii\validators\RequiredValidator');

// Using validator short name
$hasString = RuleHelper::haveValidator($userModel, 'email', 'string');

// Using validator instance (for custom validators)
$customValidator = new CustomValidator();
$hasCustom = RuleHelper::haveValidator($userModel, 'custom_field', $customValidator);

class User extends \yii\db\ActiveRecord
{
    public function rules()
    {
        return [
            [['username', 'email'], 'uired = RuleHelper::haveValidator(User::class, 'username', '

public function actionCheckValidators($id)
{
    $user = User::findOne($id);
    
    if (RuleHelper::haveValidator($user, 'email', 'e string validation for username
    }
}