Search code examples
phpyii2form-submitactive-form

Yii2 ActiveForm ceased to work


I have a very strange problem with an Yii2 form, with input fields and a submit button. The submit button is stone dead, and rules for the fields do not apply. Nothing happens when button is pushed.

It has been working perfectly before, though. All I did was moving the call to another model.

When it is working I do the call from a menu (kartik sidenav) in the layout file (main.php): $fruitbasket[]= ['label' => 'Add New...', 'url' => ['/fruit-bananas /create']];

When it is NOT working, I have moved this call to another view (fruit/index), and changed the above code (in main.php) to 'label' => Icon::show('plus') . 'Add New... ', 'url' => ['/fruit/index'], .. and in the fruit/index file I do another kartik sidenav widget, like this:

<?php>
echo SideNav::widget([
     ...

'items' => [
                [
                    'label' => Icon::show('folder-open') . '<span  class=sideitems>Bananas</span>', 'url' => ['/fruit-bananas/create'],
                ],
            ]   
         ]);
    ?>

It is the same link to fruit-bananas/create, and it seems to work. The form renders as it should. But now I have the problems I described. In the first case the submit button and rules are working. In the latter case it is not. I hope this is understandable. EDIT: adding some info: actionCreate:

public function actionCreate()
{
    $model = new FruitBananas();
    $items = ArrayHelper::map(Bananas::find()->all(),'id','brands');
    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('create', [
            'model' => $model,
            'items' => $items   
        ]);
    }
}

ActiveForm:

<?php $form = ActiveForm::begin(); ?>   
<?= $form->field($model, 'material')->dropDownList($items, ['id' => 'form-field-len']) ?>
<div class="form-group">
    <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update',  ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>

Solution

  • Finally, I solved my problem. The cause of this was not where you might look immediately. It was in AppAsset.php, where I decommented a line I had added there previously:

    public $js = [
        'https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js'
    ];
    

    I added this line there once, and then decommented it, while trying to get Semantic UI, and D3.js to work. Apparently it has to be active. Which worries me a little. Such basic functionality as described above, should do with the jQuery already built in for Yii2? In general is a messy thing with all these libraries online. Maybe a subject for an upcoming thread here?