yii.validation未在yii2中定义错误

时间:2015-08-18 12:37:29

标签: yii2

enter image description here

我在yii2中的模态窗口中打开一个ActiveForm。 当我在Modal窗口中打开ActiveForm时出现此错误。我的index.php是

<?php

use yii\helpers\Html;
use yii\bootstrap\Modal;

$this->title = 'Roles';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="role-index">

    <h1><?= Html::encode($this->title) ?></h1>

    <p>
        <?= Html::a('Create Role', '#', [
            'class' => 'btn btn-success',
            'id' => 'create-role-model',
            'data-toggle' => 'modal',
            'data-target' => '#activity-create-modal',
        ]) ?>
    </p>

</div>
<?php

Modal::begin([
    'id' => 'activity-create-modal',
    'header' => '<h2>Hello world</h2>',
    'footer' => Html::button('Close', ['class' => 'btn btn-default', 'data-dismiss' => 'modal'])
        . PHP_EOL . Html::button('Add', ['class' => 'btn btn-primary btn-modal-save']),
]);

$model= new \frontend\models\Role();
echo $this->renderAjax('_form',['model' => $model]);

Modal::end();

?>

我的_form.php是

<?php

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;

/* @var $this yii\web\View */
/* @var $model frontend\models\Role */
/* @var $form yii\widgets\ActiveForm */
?>


    <?php $form = ActiveForm::begin([
        'id' => 'create-ro',
        'enableClientValidation' => true,
    ]); ?>

    <?= $form->field($model, 'id')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'created_on')->textInput() ?>

    <?= $form->field($model, 'updated_on')->textInput() ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

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

然而,当我在index.php中放置没有渲染的ActiveForm时,Yii Validation正在工作。我这样做是错误的。必须采取哪些措施才能启动模态验证

1 个答案:

答案 0 :(得分:2)

您需要echo $this->render('_form',['model' => $model]);,而不是$this->renderAjax()

后者实际上终止整个页面并返回它而没有围绕它的布局(因此包括任何javascript包括等等)。

因为你还在另一个视图中,这绝对不是你需要的。所以只需使用render()即可。这有点令人困惑,因为Controller::render()实际上关闭了页面(并且还添加了javascripts),但是在View(其中$this处于该上下文中)的情况下,它执行{{1}为控制器做的。