首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用yii2中的模式创建表单输入不验证

使用yii2中的模式创建表单输入不验证
EN

Stack Overflow用户
提问于 2022-03-28 20:15:34
回答 1查看 78关注 0票数 0

控制器

代码语言:javascript
复制
public function actionCreate() {
    $model = new Ceremonia();
  if ($this->request->isPost) {
     if ($model->load($this->request->post()) && $model->save()){ Yii::$app->getSession()->setFlash('info',[' Nombre <strong>insertado</strong> correctamente!!!']); return $this->redirect(['index']); 
   }}else{ $model->loadDefaultValues();}
  if(Yii::$app->request->isAjax) { 
    return $this->renderAjax('create',['model' => $model]); }else{ return $this->render('create',['model' => $model]); 
   }
 }

项目中的索引文件:索引

代码语言:javascript
复制
<?= Html::button('<i class="fas fa-plus"></i> Insertar',['value'=>Url::to('create'),'class' => 'btn btn-info','data-toggle'=>'modal','data-target'=>' ajax','id'=>'ModalButtonCreate']); ?>

<?php  
  Modal::begin(['title'=>'Insertar', 'id'=>'modalCreate', /*'size'=>'modal-lg',*/]);
  echo '<div id="modalContentCreate"></div>';
  Modal::end();
?>

项目中的main.js文件:main.js

代码语言:javascript
复制
$(function(){
 $('#ModalButtonCreate').click(function(){  
     $('#modalCreate').modal('show').find('#modalContentCreate').load($(this).attr('value'));
  }); 
});

项目中的AppAsset文件:AppAsset

代码语言:javascript
复制
class AppAsset extends AssetBundle {
     public $basePath = '@webroot';
     public $baseUrl = '@web';
     public $css = ['css/site.css',];
     public $js = ['js/main.js',];
     public $depends = ['yii\web\YiiAsset','yii\bootstrap4\BootstrapAsset',]; 
  }
EN

回答 1

Stack Overflow用户

发布于 2022-04-02 01:12:10

您没有将"Create“文件代码放在这里进行检查。最好是用它来代替AppAsset。因为很明显你的奖章很好

可用代码仅表示单击按钮将显示奖章窗口。然后通过Ajax load方法在jQuery中呈现"Create“页面。

在“创建”页面上,要使用模型类或'ActiveRecord‘类,必须在Html库中使用activeInput或active方法,例如表单中的activeInput。例如,'ActiveRecord‘中的ActiveRecord方法和'model’或'ActiveRecord‘中的validate()方法。

*取决于您对($model->load($this->request->post()) && $model->save())的使用

注意:如果您正在处理模型并需要验证,请考虑使用ActiveForm。链接有两组输入方法。从active开始的,这称为活动输入,而不是从活动开始的。活动输入从指定的模型和属性获取数据,而对于常规输入,则直接指定数据。链接

这里是通过AJAX (创建和更新)使用ajax实现弹出表单的完整指南。

祝好运

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71653269

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档