diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 780fd3f5e1..67b00ee97e 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -63,6 +63,7 @@ Yii Framework 2 Change Log - Chg #9411: `DetailView` now automatically sets container tag ID in case it's not specified (samdark) - Chg #9953: `TimestampBehavior::getValue()` changed to make value processing consistent with `AttributeBehavior::getValue()` (silverfire) - New #10083: Added wrapper for PHP webserver (samdark) +- Enh #10078: Added `csrf` option to `Html::beginForm()` to allow disabling the hidden csrf field generation. (machour) - Enh #10098: Changed `yii.confirm` context bind to triggered dom element. (lichunqiang) 2.0.6 August 05, 2015 diff --git a/framework/helpers/BaseHtml.php b/framework/helpers/BaseHtml.php index 59d053b869..bb91ab51fa 100644 --- a/framework/helpers/BaseHtml.php +++ b/framework/helpers/BaseHtml.php @@ -300,6 +300,7 @@ class BaseHtml * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. * If a value is null, the corresponding attribute will not be rendered. * See [[renderTagAttributes()]] for details on how attributes are being rendered. + * The "csrf" special option can be set to FALSE to prevent the form from generating the CSRF hidden field. * @return string the generated form start tag. * @see endForm() */ @@ -316,7 +317,9 @@ class BaseHtml $hiddenInputs[] = static::hiddenInput($request->methodParam, $method); $method = 'post'; } - if ($request->enableCsrfValidation && !strcasecmp($method, 'post')) { + $csrf = ArrayHelper::remove($options, 'csrf', true); + + if ($csrf && $request->enableCsrfValidation && !strcasecmp($method, 'post')) { $hiddenInputs[] = static::hiddenInput($request->csrfParam, $request->getCsrfToken()); } } diff --git a/tests/framework/helpers/HtmlTest.php b/tests/framework/helpers/HtmlTest.php index 63c286726b..a47bb9dfc1 100644 --- a/tests/framework/helpers/HtmlTest.php +++ b/tests/framework/helpers/HtmlTest.php @@ -859,6 +859,25 @@ EOD; $model->description = $value; $this->assertEquals($expectedHtml, Html::activeTextArea($model, 'description', $options)); } + + /** + * Fixes #10078 + */ + public function testCsrfDisable() + { + Yii::$app->request->enableCsrfValidation = true; + Yii::$app->request->cookieValidationKey = 'foobar'; + + $csrfForm = Html::beginForm('/index.php', 'post', ['id' => 'mycsrfform']); + $this->assertEquals( + '
' + . "\n" . '', + $csrfForm + ); + + $noCsrfForm = Html::beginForm('/index.php', 'post', ['csrf' => false, 'id' => 'myform']); + $this->assertEquals('', $noCsrfForm); + } } class HtmlTestModel extends Model