Symfony2 表单组件 - 在 name 属性中创建没有表单名称的字段 [英] Symfony2 Form Component - creating fields without the forms name in the name attribute

查看:35
本文介绍了Symfony2 表单组件 - 在 name 属性中创建没有表单名称的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试通过 Silex 微框架使用 Symfony2 表单组件.

我的登录表单生成如下:

$app = $this->app;$constraint = new AssertCollection(array('用户名' =>新的断言NotBlank(),'密码' =>新的断言NotBlank(),));$builder = $app['form.factory']->createBuilder('form', $data, array('validation_constraint' => $constraint));$form = $builder->add('username', 'text', array('label' => 'Username'))->add('password', 'password', array('label' => 'Password'))->getForm();返回 $form;

问题是生成的表单如下所示:

<input type="hidden" value="******" name="form[_token]" id="form__token"><section class=""><label class=" required" for="form_username">用户名</label><div><input type="text" value="" name="form[username]" id="form_username" class="text"></div></节><section class=""><label class=" required" for="form_password">密码</label><div><input type="password" value="" name="form[password]" id="form_password" class="password"></div></节><部分><div><button class="fr submit">登录</button></div></节></fieldset>

而我希望 name 和 id 属性如下:

...<div><input type="password" value="" name="password" id="password" class="password"></div>

我在网上搜索并发现了有关property_path"选项的建议,但我相信这与在实际 Symfony2 框架本身中使用时用于处理数据的类有关.

我已经浏览了表单组件文件,并且设置的要点是:

Symfony/Component/Form/Extension/Core/Type/FieldType.php - 第 71 行

public function buildView(FormView $view, FormInterface $form){$name = $form->getName();如果 ($view->hasParent()) {$parentId = $view->getParent()->get('id');$parentFullName = $view->getParent()->get('full_name');$id = sprintf('%s_%s', $parentId, $name);$fullName = sprintf('%s[%s]', $parentFullName, $name);} 别的 {$id = $name;$fullName = $name;}...

不幸的是,FormFactory 使用了 FormBuilder,然后它与 Form 类一起工作,我没有足够的时间来分析组件的整个内部工作.

我确实知道将字段添加到 FormBuilder 中的children"数组中,并带有相应的选项列表.当调用 getForm 函数时,会实例化一个新的 Form,并使用 add() 方法将每个子 FieldType 输入到 Form 中.这个 Form->add() 方法自动将 Form 设置为每个孩子的父级:

公共函数添加(FormInterface $child){$this->children[$child->getName()] = $child;$child->setParent($this);如果 ($this->dataMapper) {$this->dataMapper->mapDataToForm($this->getClientData(), $child);}返回 $this;}

没有开始覆盖这些类只是为了删除这个,其他人知道只显示字段名称的最佳方法吗?

可以在 form_div_layout.html.twig widget_attributes 块中只提取 'name' 而不是 'full_name' 但我不确定这是否理想(因为 id 保持不变)或者是否还有另一个可以解决问题的方法或可注入选项.

解决方案

使用 createBuilder 函数代替:

$builder = $app['form.factory']->createNamedBuilder(null, 'form', $data, array('validation_constraint' => $constraint));

第一个参数是表单名称.

Bernhard Schussek 本人在 https://stackoverflow.com/a/13474522/520114

上的示例

I'm currently trying to use the Symfony2 Form Component though the Silex microframework.

My login form is generated as follows:

$app = $this->app;

$constraint = new AssertCollection(array(
    'username' => new AssertNotBlank(),
    'password' => new AssertNotBlank(),
));

$builder = $app['form.factory']->createBuilder('form', $data, array('validation_constraint' => $constraint));

$form = $builder
    ->add('username', 'text', array('label' => 'Username'))
    ->add('password', 'password', array('label' => 'Password'))
    ->getForm()
;

return $form;

The issue is that the resulting form is being created as follows:

<fieldset>
    <input type="hidden" value="******" name="form[_token]" id="form__token">
    <section class="">
        <label class=" required" for="form_username">Username</label>
        <div><input type="text" value="" name="form[username]" id="form_username" class="text"></div>
    </section>
    <section class="">
        <label class=" required" for="form_password">Password</label>
        <div><input type="password" value="" name="form[password]" id="form_password" class="password"></div>
    </section>
    <section>
        <div><button class="fr submit">Login</button></div>
    </section>
</fieldset>

Whereas I want the name and id attributes to be as follows:

<div><input type="text" value="" name="username" id="username" class="text"></div>
...
<div><input type="password" value="" name="password" id="password" class="password"></div>

I've scoured the web and discovered advice on the 'property_path' option but I believe this is to do with the class used to process the data when used in the actual Symfony2 framework itself.

I've been through the Form Component files and the point as which this is being set is in:

Symfony/Component/Form/Extension/Core/Type/FieldType.php - line 71

public function buildView(FormView $view, FormInterface $form)
{
    $name = $form->getName();

    if ($view->hasParent()) {
        $parentId = $view->getParent()->get('id');
        $parentFullName = $view->getParent()->get('full_name');
        $id = sprintf('%s_%s', $parentId, $name);
        $fullName = sprintf('%s[%s]', $parentFullName, $name);
    } else {
        $id = $name;
        $fullName = $name;
    }
    ...

Unforunately the FormFactory utilises the FormBuilder which then works with the Form class and I've not had enough time to analyse the whole inner workings of the Component.

I do know that fields are added into the 'children' array within the FormBuilder with a corresponding list of options. When the getForm function is called, a new Form is instantiated and each child FieldType is entered into the Form using the add() method. This Form->add() method automatically sets the Form as the parent of each child:

public function add(FormInterface $child)
{
    $this->children[$child->getName()] = $child;

    $child->setParent($this);

    if ($this->dataMapper) {
        $this->dataMapper->mapDataToForm($this->getClientData(), $child);
    }

    return $this;
}

Without starting to override these classes just to remove this does anyone else know of the best method of just displaying the fields name?

It is possible to just pull 'name' instead of 'full_name' in the form_div_layout.html.twig widget_attributes block but I wasn't sure as to whether this was ideal (as the id remains unchanged) or if there was another method or injectable options that would do the trick.

解决方案

Instead of the createBuilder function use:

$builder = $app['form.factory']->createNamedBuilder(null, 'form', $data, array('validation_constraint' => $constraint));

First parameter being the form name.

Example by Bernhard Schussek himself at https://stackoverflow.com/a/13474522/520114

这篇关于Symfony2 表单组件 - 在 name 属性中创建没有表单名称的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆