初始化自定义类型表单数据 [英] Initialize custom type form data

查看:30
本文介绍了初始化自定义类型表单数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了一个这样的自定义表单:

I have defined a custom form like this:

class EditOwnerProfileType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add("user", new UserType())
            ->add("dog", new DogType())
            ->add('save', 'submit');
    }

    public function getName()
    {
        return 'edit_owner';
    }
}

我想创建这个表单并用一些这样的数据初始化它:

I want to create this form and initialize it with some data like this:

    $user = new User();
    $user->setLatitude(1.1)
        ->setLongitude(2.2)
        ->setAddress("custom address");

    $dog = new Dog();
    $dog->setDogName("Bruno")
        ->setDogSize("small")
        ->setDogBreed("Bulldog");

    $formData = array(
        "user" => $user,
        "dog" => $dog
    );

    $form = $this->createForm(new EditOwnerProfileType(), $formData, array("csrf_protection" => false))->handleRequest($request);

DogTypeUserType 只有 NotBlank 约束

每次我想验证数据时,它总是为每个字段抛出错误,如下所示:

Every time i want to validate data, it allways throws error for every field like this:

"errors": {
  "user": {
    "latitude": [
      "This value should not be blank."
    ],
    "longitude": [
      "This value should not be blank."
    ],
    "address": [
      "This value should not be blank."
    ]
  },
  "dog": {
    "dogName": [
      "This value should not be blank."
    ],
    "dogSize": [
      "This value should not be blank."
    ],
    "dogBreed": [
      "This value should not be blank."
    ]
  },

难道我不应该初始化所有值吗?那么,如果用户没有为此字段传递任何值,是否使用我定义的值进行初始化?在 EditOwnerProfileType 表单上初始化值的正确方法是什么?

Isn't supposed that i'm initializing all the values? So, if user don't pass any value for this field, is initialized with the values I defined? Which is the correct way to initialize values on EditOwnerProfileType form?

我尝试更改表单创建(仅用于测试),但也没有用.

I tried to change the form creation (just for testing), but didn't work either.

$form = $this->createForm(new EditOwnerProfileType(), $formData, array("csrf_protection" => false));
$form->setData($formData);
$form->handleRequest($request);

包含 DogType 和 UserType 代码

To include DogType and UserType code

class DogType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('dogSize')
            ->add('dogBreed')            
        ;
    }    

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Dog',
        ));
    }

    public function getName()
    {
        return 'dog_type';
    }
}

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('latitude')
            ->add('longitude')
            ->add('address')
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\User',
        ));
    }

    public function getName()
    {
        return 'user_type';
    }
}

我使用的是 Symfony 2.7.9

I'm using Symfony 2.7.9

推荐答案

我回答了我自己的问题,问题是...我误解了 Symfony 表单组件.

I answer my own question, the problem was... I was misunderstanding the Symfony Form Component.

我可以像这样初始化表单:

I can initialize the form like:

$user = new User();
$user->setLatitude(1.1)
    ->setLongitude(2.2)
    ->setAddress("custom address");

$dog = new Dog();
$dog->setDogName("Bruno")
    ->setDogSize("small")
    ->setDogBreed("Bulldog");

$formData = array(
    "user" => $user,
    "dog" => $dog
);

$form = $this->createForm(new EditOwnerProfileType(), $formData, array("csrf_protection" => false))

此时,表单已正确初始化,所有字段都有正确的值.

At this point, the form is correctly initialized and all fields have the correct values.

在请求中传递一个参数edit_owner[dog][name]=othername并执行:

Passing one parameter in the request edit_owner[dog][name]=othername and doing:

$form->handleRequest($request);

我希望只更改 edit_owner[dog][name](其余字段保持其默认值),但我注意到表单的其余字段已被处理并且解析为 blank 值并替换我的初始值".

I expected only the edit_owner[dog][name] to be changed (And the rest of fields maintain its default value), but i noticed that the rest of fields of the form were processed and parsed as blank values and replaced my "initial values".

这就是问题所在,Symfony 就是这样工作的.我做这个答案是为了对其他人有用.

That was the problem and Symfony works on that way. I make this answer so it can be useful for someone else.

这篇关于初始化自定义类型表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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