Symfony - 从一个表单向另一个表单的值 [英] Symfony - Pasing values from One Form to another

查看:125
本文介绍了Symfony - 从一个表单向另一个表单的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了关于Symfony窗​​体和Twig的所有内容,但是还没有找到任何解决方案来解决我的问题,所以我决定询问你们。



试图达到的目的只是将重复数据传递给我的注册表单一次。我想坚持在数据库中的数据是用户详细信息,公司名称和地址以及分支名称和地址。我需要将地址详细信息复制到两个实体,即公司和分支。



有没有办法将这些数据在Web界面中形成一次并将其指向两个Symfony表单被添加到实体中并进行验证。

我知道我可以将地址数据从一个实体复制到另一个实体的外部,但它并不正确。 / p>

Main RegistrationFormType:

 类RegistrationFormType extends AbstractType 
{
private $ class;

public function __construct($ class)
{
$ this-> class = $ class;

$ b $ public function buildForm(FormBuilderInterface $ builder,array $ options)
{
$ builder
- > add('first_name')
- > add('last_name')
- > add('username')
- > add('email','email')
- > add ('password','password')
- > add('company',new CompanyType())
- > add('branch',new BranchType())
;


public function setDefaultOptions(OptionsResolverInterface $ resolver)
{
$ resolver-> setDefaults(array(
'data_class'=> $
'意向'=>'注册',
));
}

public function getName()
{
return'registration';


公司类型:

  class CompanyType extends AbstractType 
{
public function buildForm(FormBuilderInterface $ builder,array $ options)
{
$ builder
- > add('companyName')
- > add('foo',new CompanyInfoType(),array('data_class'=>'Acme \UserBundle\Entity\公司'))
;


public function setDefaultOptions(OptionsResolverInterface $ resolver)
{
$ resolver-> setDefaults(array(
'data_class'=>' Acme\UserBundle\Entity\Company',
'validation_groups'=>数组('Registration'),
));
}

public function getName()
{
return'company';


code


分行类型:

  class BranchType extends AbstractType 
{
public function buildForm(FormBuilderInterface $ builder,array $ options)
{
$ builder
- > add('branchName')
- > add('boo',new CompanyInfoType(),array('data_class'=>'Acme \UserBundle\Entity\\ \\分支'))
;


public function setDefaultOptions(OptionsResolverInterface $ resolver)
{
$ resolver-> setDefaults(array(
'data_class'=>' Acme\UserBundle\\Entity\Branch',
'validation_groups'=>数组('Registration'),
));
}

public function getName()
{
return'branch';


$ / code $ / pre

CompanyInfoType

  class CompanyInfoType extends AbstractType 
{
public function buildForm(FormBuilderInterface $ builder,array $ options)
{
$ builder
- > add('streetNumber')
- > add('address')
- > add('city')
- > add(' ''
- > add('country')
- > add('contactName')
- > add('phone')
- > add ('email')
;


public function setDefaultOptions(OptionsResolverInterface $ resolver)
{
$ resolver-> setDefaults(array(
'inherit_data'=> true
));
}

public function getName()
{
return'company_info';


$ / code $ / pre
$ b $ p感谢!

//编辑



我只想解释为什么我想将公司详细信息复制到分支,因为它看起来像有点奇怪。

这样做是因为我想要在客户想要将其帐户升级到企业时准备好结构。通过默认注册,他们基本上只有一个分支机构与公司总部相同,如果他们增加更多分支机构,公司细节保持不变,因为这是他们的主要分支机构。现有的用户和其他东西已经映射到该分支,因此添加新分支不需要任何数据库或其他结构变化。

// EDIT2
由于 linuxatico ,我设法让这种方式在表单中得到体现,但我相信它可以更加漂亮地解决。这里是我是如何做到的:

$ p $ public function buildForm(FormBuilderInterface $ builder,array $ options)
{
$ builder
- > add('first_name')
- > add('last_name')
- > add('username')
- >添加('email','email')
- > add('password','password')
- > add('company',new CompanyType())
- > add('branch',new BranchType())
;

$ builder-> addEventListener(FormEvents :: SUBMIT,function(FormEvent $ event){
$ company = $ event-> getForm() - > get('company' ) - > getData();
$ event-> getForm() - > get('branch') - > getData()
- > setBranchName($ company-> getCompanyName ())
- > setStreetNumber($ company-> getStreetNumber())
- > setAddress($ company-> getAddress())
- > setCity($ company - > getCity())
- > setCountry($ company-> getCountry())
- > setZip($ company-> getZip())
- > setContactName($ company-> getContactName())
;
});
}


解决方案

根据你给我们提供的信息,你认为这是个好主意,看起来你在设计软件方面存在结构性问题,并且肯定有另一种更好的方法来解决你的问题。



无论如何,如果您想继续使用您的解决方案,我可以建议使用AJAX,最好是JQuery,以便您可以向第二个窗体发出POST请求(如果第一个窗体正确)。 p>

伪代码:

  $。post({url1,
key_value_array_data,
function(){
$ .post({url2,
key_value_array_data,
function(){
// custom_code
});
}
});

编辑:这肯定会有帮助: https://api.jquery.com/serialize/



编辑#2:Tou还可以使用Symfony的内部事件系统拦截表单提交并根据需要自定义行为: http:/ / /symfony.com/doc/current/cookbook/form/dynamic_form_modification.html


I've read everything about Symfony forms and Twig, but haven't found any solution for my problem, so I decided to ask you guys.

What I'm trying to achieve is to pass duplicate data to my registration form only once. Data that I want to persist in database are user details, company name and address and branch name and address. I need to copy address details to two entities i.e. Company and Branch.

Is there a way to pass this data to form in web interface only once and point it to two Symfony forms to be added to entities and validated.

I know I can copy address data from one entity to another outside of the form, but it doesn't feel right.

Main RegistrationFormType:

class RegistrationFormType extends AbstractType
{
    private $class;

    public function __construct($class)
    {
        $this->class = $class;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('first_name')
            ->add('last_name')
            ->add('username')
            ->add('email', 'email')
            ->add('password', 'password')
            ->add('company', new CompanyType())
            ->add('branch', new BranchType())
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => $this->class,
            'intention'  => 'registration',
        ));
    }

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

CompanyType:

class CompanyType extends AbstractType
{   
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('companyName')
            ->add('foo', new CompanyInfoType(), array('data_class' => 'Acme\UserBundle\Entity\Company'))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\UserBundle\Entity\Company',
            'validation_groups' => array('Registration'),
        ));
    }

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

Branch Type:

class BranchType extends AbstractType
{   
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('branchName')
            ->add('boo', new CompanyInfoType(), array('data_class' => 'Acme\UserBundle\Entity\Branch'))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\UserBundle\Entity\Branch',
            'validation_groups' => array('Registration'),
        ));
    }

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

CompanyInfoType:

class CompanyInfoType extends AbstractType
{   
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('streetNumber')
            ->add('address')
            ->add('city')
            ->add('zip')
            ->add('country')
            ->add('contactName')
            ->add('phone')
            ->add('email')
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'inherit_data' => true
        ));
    }

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

Thanks!

// EDIT

I just want to explain why I want to copy company details to branch since it does look a little a bit weird.

It is done that way just because I want to have structure prepared in case customer wants to upgrade their account to "enterprise". With default registration they basically have only one branch which is equal to company HQ and if they add more branches, company details stay the same since that's their main "branch". The existing users and other stuff is already mapped to that branch, so adding new branch doesn't require any database or other structural changes.

// EDIT2 I managed to get this working inside form thanks to linuxatico, but I'm sure it can be solved even prettier. Here is how I did it:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('first_name')
        ->add('last_name')
        ->add('username')
        ->add('email', 'email')
        ->add('password', 'password')
        ->add('company', new CompanyType())
        ->add('branch', new BranchType())
    ;

    $builder->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) {
        $company = $event->getForm()->get('company')->getData();
        $event->getForm()->get('branch')->getData()
            ->setBranchName($company->getCompanyName())
            ->setStreetNumber($company->getStreetNumber())
            ->setAddress($company->getAddress())
            ->setCity($company->getCity())
            ->setCountry($company->getCountry())
            ->setZip($company->getZip())
            ->setContactName($company->getContactName())
        ;
    });
}

解决方案

Well, I don't think it's a good idea what you're doing, by the info you gave us it seems you have a structural problem on how you design your software and there sure is another better way to solve your problem.

Anyway, if you want to go on with your solution, I can suggest the use of AJAX, preferably JQuery, so that you can make a POST request to the second form if the first goes right.

In pseudocode:

$.post({ url1,
         key_value_array_data,
         function(){
             $.post({ url2,
                      key_value_array_data,
                      function(){
                           //custom_code
                    });
        }
}); 

EDIT: this will sure help: https://api.jquery.com/serialize/

EDIT #2: Tou can also use Symfony's internal Event system to intercept the form submission and customize the behaviour as you wish: http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html

这篇关于Symfony - 从一个表单向另一个表单的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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