M
M
Makar Gerasimov2016-10-19 23:19:53
Yii
Makar Gerasimov, 2016-10-19 23:19:53

How to throw an addError error if the attribute is an array?

Hello! As I understood addError does not accept 'fields[name]' as parameters. (he cuts off the brackets)
Please tell me the solutions to the problem ...
Submission:

<?php foreach( $modelsField as $_model ): ?>
                <?php
                $info = false;

                if( !empty($model->id) and ($result = $_model->getFieldValue($model->id)->one()) ) {
                    $info = $result->value;
                }
                ?>
                <?= $form->field($_model, 'other['.$_model->name.']')
                    ->textInput(['value' => $info])
                    ->label($_model->name) ?>
            <?php endforeach; ?>

Model, callback function for checking the other field:
public function checkOther($attribute, $params)
    {
        foreach( $this->$attribute as $key => $value ) {
            $this->addError('other['.$key.']', 'test');
            break;
        }
    }

Found a solution to the problem. But I couldn't add magic getters and setters. Does anyone have examples of implementation? Link

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Makar Gerasimov, 2016-10-20
@MacFiss

And so, the solution to the problem. The model has a method called onUnsafeAttribute. This method is called if any variable was passed to the model that was not declared. The concept is as follows:
1) In the view, other[name1..n] use other_name1..n
2) Define a variable in the $other model of type array
3) Redefine the onUnsafeAttribute function and write to our array.
4) In the rules, we will announce the callback to our variable and in it we will iterate over our array.

<?php
namespace backend\models;

use Yii;
use backend\models\Model;
use backend\filters\Filter;

class MySuperModel extends Model
{
    public $name;
    public $other = [];

    public function rules()
    {
        return [
            [['name','other'], 'required'],
            ['other', 'checkOther'],
        ];
    }

    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => 'Name',
        ];
    }

    public function onUnsafeAttribute($name, $value)
    {
        if( ! strpos('other_', $name) ) {
            list($other, $name) = explode('_', $name);
            $this->other[$name] = $value;
        }
    }
    
    public function checkOther(string $attribute)
    {
        if( ! is_array($this->$attribute) ) {
            return false;
        }
        
        $errors = [];

        foreach( $this->$attribute as $name => $value ) {
            $filter = Filter::run($this->site_id, $name, $value);
            $name = 'other_' . $name;
            
            if( ! $filter ) {
                $errors[$name] = Yii::t('messages', 'Field does not exist');
                continue;
            }
        }

        if( !empty($errors) ) {
            return $this->addErrors($errors);
        }

        return true;
    }
}

M
Maxim Timofeev, 2016-10-20
@webinar

Looks like the problem is here:

class Model extends Component implements IteratorAggregate, ArrayAccess, Arrayable
{
public function addError($attribute, $error = '')
    {
        $this->_errors[$attribute][] = $error;
    }
}

Try redefining.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question