0

I am using the following (simple) code for a model to be used with a form in yii2:

class ChooseAgeCategoriesForm extends Model
{

    public $ageCategories;

    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            ['ageCategories', 'safe'],
            [['ageCategories'], 'each', 'rule' => ['integer']],
        ];
    }
}

However, then trying to load the model like this:

$model = new ChooseAgeCategoriesForm();

if ($model->load(Yii::$app->request->post())) {
        $acIDs = $model->ageCategories;
}

The code is never executed. In fact, $model->load returns Bool(false) but $model->errors is empty. Dumping the content of post yields this result:

array(2) { ["_csrf"]=> string(56) "SzhjdWIuc0oIQSA0BEs4BDp8KzYEXwM5CG80RhNBRDkPUzIXO1gBew==" ["ageCategories"]=> array(9) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4" [4]=> string(1) "5" [5]=> string(1) "6" [6]=> string(1) "7" [7]=> string(1) "9" [8]=> string(2) "10" } }

So yes, the field is actually sent and filled with an array of numbers. What don't I see here, what is the problem? Thanks in advance. I also tried to add ['ageCategories', 'exist', 'allowArray' => true], as another rule, but this does not work either. Any ideas?

Form code

As requested, the code of the form itself:

<?php $form = ActiveForm::begin([
    'action' => Url::to(['/result/team-result']),
    'method' => 'post',
    'options' => ['id' => 'chooseAgeCategory', 'style' => "margin-bottom: 15px;"]
]); ?>

<?= Select2::widget([
    'name' => 'ageCategories',
    'id' => 'ac-id',
    'value' => $acIDs,
    'data' => ArrayHelper::map(AgeCategory::find()->all(), 'id', 'name'),
    'options' => ['multiple' => true, 'placeholder' => Yii::t('view', 'Select Age Category')],
    'pluginEvents' => [
        "change" => "function() { this.form.submit(); }",
    ]
]) ?>

<?php $form->end(); ?>
4
  • Show your form's code Commented Apr 25, 2017 at 14:13
  • Added the form's code to the question. Commented Apr 25, 2017 at 14:14
  • What is variable $model's class? Commented Apr 25, 2017 at 14:20
  • $model is initialized as $model = new ChooseAgeCategoriesForm();, hence ChooseAgeCategoriesForm which is derived from yii\base\Model. Commented Apr 25, 2017 at 14:22

2 Answers 2

1

Please change your select2 widget code like below. Yii2 wrapping model name with field name like

ChooseAgeCategoriesForm[ageCategories]

If you inspect your form you will come to know. SO you have to give model value in the select2 widget.

<?= Select2::widget([
    'model' => $model,
    'attribute' => 'ageCategories',
    'id' => 'ac-id',
    'value' => $acIDs,
    'data' => ArrayHelper::map(AgeCategory::find()->all(), 'id', 'name'),
    'options' => ['multiple' => true, 'placeholder' => Yii::t('view', 'Select Age Category')],
    'pluginEvents' => [
        "change" => "function() { this.form.submit(); }",
    ]
]) ?>

Ref: http://demos.krajee.com/widget-details/select2

Sign up to request clarification or add additional context in comments.

Comments

1

I would suggest you use the Select2 using the model attribute, so that would be automatically linked to the field easing the loading process and also using your model's validation functions:

<?= Select2::widget([
    'name' => 'ageCategories',
    'id' => 'ac-id',
    'value' => $acIDs,
    'data' => ArrayHelper::map(AgeCategory::find()->all(), 'id', 'name'),
    'options' => ['multiple' => true, 'placeholder' => Yii::t('view', 'Select Age Category')],
    'pluginEvents' => [
        "change" => "function() { this.form.submit(); }",
    ]
]) ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.