php Yii2:编写表单动作的正确方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27958932/
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-08-25 23:38:35  来源:igfitidea点击:

Yii2: Proper way of writting the form's action

phpyii2

提问by Michael St Clair

I have written form tag of YII2 specific as

我已经写了 YII2 的表单标签,具体为

<?php $form = ActiveForm::begin(['id' => 'builder/saveform','options' => ['method' => 'post']]) ?>

but when i run this, my external javascript is catching an error showing

但是当我运行这个时,我的外部 javascript 捕捉到一个错误显示

Error: Syntax error, unrecognized expression: #builder/saveform

What is the error

什么是错误

回答by Michael St Clair

To change default action add it as the first argument in this format ['<controller>/<action>']

要更改默认操作,请将其添加为此格式的第一个参数 ['<controller>/<action>']

<?php $form = ActiveForm::begin(['action' => ['builder/saveform'],'options' => ['method' => 'post']]) ?>

Example from the Yii2 guide

Yii2 指南中的示例

Also, keep in mind that the method defaults to post, so specifying that is unnecessary.

另外,请记住该方法默认为 post,因此无需指定。

回答by Dheeraj singh

 $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data'], 'action' => Yii::$app->urlManager->createUrl(['site/signup'])]);

You can also use this snippet for custom form action and other attributes. createUrl method will not affect your url pattern.

您还可以将此代码段用于自定义表单操作和其他属性。createUrl 方法不会影响您的 url 模式。

回答by bds

Since there is still no accepted answer, the exact answer to your question is in the Error: "unrecognized expression: '#builder/saveform' ". You have your 'id' HTML tagof the Form being assigned a value of 'builder/saveform', which contains "/" - an illegal character in terms of HTML4 Specification. I suppose Yii has a "validation" in place resulting in your error.

由于仍然没有公认的答案,您的问题的确切答案是错误:“无法识别的表达式:'#builder/saveform'”。您将表单的'id' HTML 标记分配了一个值 'builder/saveform',其中包含“/” - 根据HTML4 规范是非法字符。我想 Yii 有一个“验证”,导致你的错误。

Now, I believe you are trying to specify the relative URL for the Form submission. For that, please refer to Michael St Clair's answer.

现在,我相信您正在尝试为表单提交指定相对 URL。为此,请参阅 Michael St Clair 的回答。

回答by bds

for the sake now i have written the following code

现在我写了下面的代码

<?php $form = ActiveForm::begin(['action' => 'index.php?r=builder/saveform','options' => ['method' => 'post']]) ?>

it is working but is it the proper way to write???

它正在工作,但它是正确的写作方式吗???

回答by Lionel Morrison

I was able to do it by adding the action into the options like so

我能够通过将操作添加到选项中来做到这一点

<?php $form = ActiveForm::begin(['id' => 'contact-form', 'options' => ['method' => 'post', 'action' => 'site/add']]); ?>

回答by iltaf khalid

For Yii2

对于 Yii2

$form = ActiveForm::begin([
                            'id'      => 'login-form',
                            'options'=>['autocomplete'=>'off','method' => 'post', ],
                            'action' => '/frontend/web/user-management/auth/login',
                            'validateOnBlur'=>false,
                            'fieldConfig' => [
                                    'template'=>"{input}\n{error}",
                            ],
                                        ]);