zoukankan      html  css  js  c++  java
  • yii2 表单 $form

    <?= $form->field($userRole, 'userid', ['options' =>['class' => 'bigDiv'] ])->textInput(['placeholder'=> 'ccc', 'class'=> 'inputSelf', 'maxlength' => true, 'value' => $user->id]) ?>

    <?php
     $form = ActiveForm::begin(['action' => ['sykc','id' => $model->id],
                     'method'=>'post',
                     'options' => ['id'=>'w-sykc-form','enctype' => 'multipart/form-data','autocomplete'=>"off",'data-pjax'=>'true'],
                     'fieldConfig' => [
                         'template' => '{label}<div class="col-lg-5">{input}</div><div class="col-lg-3">{error}</div>',
                         'labelOptions' => ['class' => 'col-sm-2 control-label'],  //修改label的样式
                     ]
             ]);
    ?>
    <?= $form->field($model, 'username')->textInput(['placeholder' => '您的姓名', 'require' => 'true','class' => 'form-control ', 'maxlength' => 255])->label(false) ?> 

    <?= $form->field($model, 'email')->textInput(['placeholder' => '您的邮箱地址', 'require' => 'true', 'class' => 'form-control', 'maxlength' => 255])->label(false) ?>
    <?= $form->field($model,'grade_id')->dropDownList($grades,['prompt'=>'选择年级'])?>

    <?= Html::activeDropDownList($model, 's_id',  ArrayHelper::map(Standard::find()->all(), 's_id', 'name')) ?>
    <div class="form-group">   <?= Html::submitButton('提交', ['class' => 'btn btn-blue-bg-new']) ?>   <div class="help-block"></div> </div> <?php ActiveForm::end(); ?>

      

    $model = new Form;
            $model->scenario = 'dingyue';
            if($model->load(Yii::$app->request->post())){
                if(Yii::$app->request->isAjax){
                    Yii::$app->response->format = yiiwebResponse::FORMAT_JSON;
                    return ActiveForm::validate($model);
                }else{
                    if ($model->save()) {
                        $this->flash('success',  '提交成功!');
                        return $this->redirect(Yii::$app->request->referrer);
                    } else {
                        $this->flash('error', Yii::t('easyii', 'Create error. {0}', $model->formatErrors()));
                        return $this->refresh();
                    }
                }
    
            }
    

      这样写完后会在系统的session中存一个success或者是error的信息,如果不做下面操作,那么呈现的效果表单提交,页面刷新

      这时候用户无法知道自己是否已经提交成功或者失败,需要在页面增加一块代码

    <script>
        <?php foreach(Yii::$app->session->getAllFlashes() as $key => $message) : ?>
        alert('<?=$message?>');
        <?php endforeach; ?>
    </script>
    

      以上5行代码的意思是:如果后台设置了flash,那么前台页面就把提示的信息alert出来

    1 $form = ActiveForm::begin(['action' => ['sykc','id' => $model->id],
    2                 'method'=>'post',
    3                 'options' => ['id'=>'w-sykc-form','enctype' => 'multipart/form-data','autocomplete'=>"off",'data-pjax'=>'true'],
    4                 'fieldConfig' => [
    5                     'template' => '{label}<div class="col-lg-5">{input}</div><div class="col-lg-3">{error}</div>',
    6                     'labelOptions' => ['class' => 'col-sm-2 control-label'],  //修改label的样式
    7                 ]
    8         ]);
    1 <?=Html::activeHiddenInput($model,'address')?>
  • 相关阅读:
    移动前端webApp开发点滴积累20140524
    做业务系统与公众产品的区别
    浅谈浏览器兼容性问题-(4)工具及调试
    浅谈浏览器兼容性问题-(3)他山之石整理
    浅谈浏览器兼容性问题-(2)本人解决方案理解与积累
    使用Using的注意事项
    [转]VMPlayer的Briged网络配置
    [转]移动互联网应用技术架构简介-Restful服务
    理解WCF中的Contracts
    [翻译]在ASP.NET Web API中通过OData支持查询和分页
  • 原文地址:https://www.cnblogs.com/fpcing/p/10083967.html
Copyright © 2011-2022 走看看