public static beginForm ( array | string $action = '', string $method = 'post', array $options = [] ) : string | ||
$action | array | string | the form action URL. This parameter will be processed by [[Url::to()]]. |
$method | string | the form submission method, such as "post", "get", "put", "delete" (case-insensitive). Since most browsers only support "post" and "get", if other methods are given, they will be simulated using "post", and a hidden input will be added which contains the actual method type. See [[\yii\web\Request::methodParam]] for more details. |
$options | array | the tag options in terms of name-value pairs. These will be rendered as 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. Special options: - `csrf`: whether to generate the CSRF hidden input. Defaults to true. |
return | string | the generated form start tag. |
public static function beginForm($action = '', $method = 'post', $options = [])
{
$action = Url::to($action);
$hiddenInputs = [];
$request = Yii::$app->getRequest();
if ($request instanceof Request) {
if (strcasecmp($method, 'get') && strcasecmp($method, 'post')) {
// simulate PUT, DELETE, etc. via POST
$hiddenInputs[] = static::hiddenInput($request->methodParam, $method);
$method = 'post';
}
$csrf = ArrayHelper::remove($options, 'csrf', true);
if ($csrf && $request->enableCsrfValidation && strcasecmp($method, 'post') === 0) {
$hiddenInputs[] = static::hiddenInput($request->csrfParam, $request->getCsrfToken());
}
}
if (!strcasecmp($method, 'get') && ($pos = strpos($action, '?')) !== false) {
// query parameters in the action are ignored for GET method
// we use hidden fields to add them back
foreach (explode('&', substr($action, $pos + 1)) as $pair) {
if (($pos1 = strpos($pair, '=')) !== false) {
$hiddenInputs[] = static::hiddenInput(urldecode(substr($pair, 0, $pos1)), urldecode(substr($pair, $pos1 + 1)));
} else {
$hiddenInputs[] = static::hiddenInput(urldecode($pair), '');
}
}
$action = substr($action, 0, $pos);
}
$options['action'] = $action;
$options['method'] = $method;
$form = static::beginTag('form', $options);
if (!empty($hiddenInputs)) {
$form .= "\n" . implode("\n", $hiddenInputs);
}
return $form;
}