使用 Zend Framework 2 创建表单 – 方法属性


Creating forms with Zend Framework 2 – method attribute

我正在使用Zend Framework 2创建一个表单。由于我无法找到全面的文档,因此我进行了一些实验。

通常,我使用 $this->setAttribute('method', 'post'); 在表单或相应的视图中设置表单的 method 属性。

现在我注意到,当根本不指定方法属性时,method="POST"属性会自动出现在打开表单标签中。

这是从哪里来的?这是 Zend Framework 2 的默认行为还是浏览器的默认行为?

Zend'Form'Form的属性已在属性数组中硬编码。

/**
 * Seed attributes
 *
 * @var array
 */
protected $attributes = array(
    'method' => 'POST',
);

有趣的是,用于呈现表单的视图帮助程序 Zend'Form'View'Helper'Form 实际上GET作为默认方法值。

官方文档中有一个例子:

// Set the method attribute for the form
$form->setAttribute('method', 'post');

渲染 Zend Form 的默认方法是 GET,参见 Zend''Form''View''Helper''Form::openTag():

$attributes = array(
    'action' => '',
    'method' => 'get',
);