yii2 ActiveForm

// View

// 生成 form 表单

 <?php
    use \yii\widgets\ActiveForm;
    use \yii\helpers\Html;
    $form = ActiveForm::begin([
        'id' => 'form_id',
        'method' => 'post',
        'action' => 'index.php?r=site/index',
        'options' => [
          'class' => 'form-class'
        ],

  // 配置模板
        'fieldConfig' => [
            'template' => "{label}<div class='class-name'>{input}{error}</div>",
            'labelOptions' => [
                    'class' => 'lclass-name'
            ]

        ]
    ]);
?>

// 生成各种input标签

<?php
        echo $form->field($article, 'title', ['options' => ['class' => 'class']])
            ->label('标题' . Html::tag('span', ' * ', ['class' => '']))
            ->textInput([

    // 默认生成 'name' => 'Article['title']' 所以不用写
                'class' => 'class-name',
                'id' => 'Article_title',
                'maxlength' => 32,
                'minlength' => 4
            ]);  // 下拉列表 dropDownList  文件 fileInput 以此类推
?>

猜你喜欢

转载自www.cnblogs.com/AI-geek/p/10502959.html
今日推荐