PHP code example of andrewdanilov / yii2-helpers

1. Go to this page and download the library: Download andrewdanilov/yii2-helpers 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/ */

    

andrewdanilov / yii2-helpers example snippets


$attribute_rules = [
  'name' => [ModelHelper::ATTR_RULE_STRIP_TAGS],
  'text' => [ModelHelper::ATTR_RULE_CLEAN_JS, ModelHelper::ATTR_RULE_STRIP_TAGS],
  '<attribute>' => [<attr_rule1>, <attr_rule2>, <attr_rule3>, ...],
  '*' => [<attr_rule4>, ...], // для всех атрибутов, даже если для них уже применялось правило
  '?' => [<attr_rule5>, ...], // только для атрибутов, для которых не указано правило
];

public function setAttributes($values, $safeOnly = true)
{
    $values = ModelHelper::cleanLoadedValues($values, [
        'title' => [ModelHelper::ATTR_RULE_STRIP_TAGS],
        'text' => [ModelHelper::ATTR_RULE_CLEAN_JS],
    ]);
    parent::setAttributes($values, $safeOnly);
}

$model = new MyModel();
$model->some_attr = 'some_wrong_value';
$model->validate();
echo \andrewdanilov\helpers\ModelHelper::getFirstError($model);