1. Go to this page and download the library: Download vjik/yii2-rules-validator 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/ */
vjik / yii2-rules-validator example snippets
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".');
}
}
}
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".');
}
}
}
class MyModel extends Model
{
public $country;
public function rules()
{
return [
['country', MyRulesValidator::class],
];
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.