首页 文章

Symfony2表单组件 - 在name属性中创建没有表单名称的字段

提问于
浏览
24

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

我的登录表单生成如下:

$app = $this->app;

$constraint = new Assert\Collection(array(
    'username' => new Assert\NotBlank(),
    'password' => new Assert\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()
;

return $form;

问题是生成的表单创建如下:

<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>

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

<div><input type="text" value="" name="username" id="username" class="text"></div>
...
<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();

    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;
    }
    ...

不幸的是,FormFactory使用FormBuilder然后使用Form类,我没有足够的时间来分析Component的整个内部工作 .

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

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

    $child->setParent($this);

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

    return $this;
}

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

可以在form_div_layout.html.twig widget_attributes块中拉出'name'而不是'full_name',但我不确定这是否理想(因为id保持不变)或者是否有其他方法或者可注射可以做到这一点的选项 .

3 回答

  • 24

    而不是createBuilder函数使用:

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

    第一个参数是表单名称 .

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

  • 7

    在Symfony3中,重写子类中的AbstractType :: getBlockPrefix以返回null .

  • 26

    如果您不使用子表单,可以将表单的 getName 方法设置为空字符串:

    class FormType extend AbstractType {
        // ...
    
        public function getName() {
            return '';
        }
    }
    

相关问题