javascript Yii2创建一个没有模型的表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28331492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 08:48:15  来源:igfitidea点击:

Yii2 create a form without a model

javascriptphpframeworksyii2mailchimp

提问by con322

I was wondering how can I create a form without a model in Yii2 framework as I am creating a mailchimp signup form so a model isn't necessary the below code generates a form however as you can see it uses a model.

我想知道如何在 Yii2 框架中创建一个没有模型的表单,因为我正在创建一个 mailchimp 注册表单,因此模型不是必需的,下面的代码生成了一个表单,但是正如您所看到的,它使用了一个模型。

<?php $form = ActiveForm::begin(['id' => 'login-form']); ?>

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

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

Do I still use activeform, how can I remove the $model variable without it throwing up an error?

我是否仍然使用 activeform,如何删除 $model 变量而不引发错误?

回答by Blizz

Yii2 has this nice little thingy called a DynamicModel. This basically allows you to create a model on the fly so that you can still use all the ActiveFormand validation goodies, but without having to commit to writing an entire model class for it. Might be interesting.

Yii2 有一个漂亮的小东西叫做DynamicModel. 这基本上允许您即时创建模型,以便您仍然可以使用所有ActiveForm和验证的好东西,而不必承诺为其编写整个模型类。可能很有趣。

Example from the documentation:

文档中的示例:

public function actionSearch($name, $email)
{
   $model = DynamicModel::validateData(compact('name', 'email'), [
       [['name', 'email'], 'string', 'max' => 128],
       ['email', 'email'],
   ]);
   if ($model->hasErrors()) {
      // validation fails
   } else {
      // validation succeeds
   }
}

Obviously these instance can also be used for the ActiveForm-widget. You can then run proper validation in your actions first and then pass on your data to MailChimp. Might be handy if you want to run HTML Purifieras part of that validation for the content

显然,这些实例也可以用于ActiveForm-widget。然后,您可以先在您的操作中运行适当的验证,然后将您的数据传递给 MailChimp。如果您想HTML Purifier作为内容验证的一部分运行,可能会很方便

回答by Sumanstm21

use Html Input with active form <?=Html::input('text','','',['class'=>'form-control'])?>

使用带有活动形式的 Html 输入 <?=Html::input('text','','',['class'=>'form-control'])?>

回答by ZeiZ

As @DamienPirsy suggested - use plain. If you want use yii2 features for it - use Class yii\helpers\BaseHtml (http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html) There are all methods to build any form as you want. Then you can operate it in any action in any controller of your application. But this is not true MVC way. That's why Yii/Yii2 advises you to use models.

正如@DamienPirsy 建议的那样 - 使用普通的。如果你想使用 yii2 特性 - 使用 Class yii\helpers\BaseHtml ( http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html) 有所有方法可以根据需要构建任何表单. 然后,您可以在应用程序的任何控制器中的任何操作中操作它。但这不是真正的 MVC 方式。这就是 Yii/Yii2 建议您使用模型的原因。