douwang6635 2014-10-23 13:23
浏览 29

Symfony2 - 以嵌入形式执行独特验证

In a project, I have an entity "customer" linked in a ManyToOne relationship to an entity "company". When a customer registers, he has to fill a form with attributes of "customer" and the name of "company"; therefore, I embedded my CompanyType in my CustomerType form.

The problem is with the validation : the name of each company should be unique and therefore I added " 'cascade_validation' => true" in CustomerType , unique=true in the entity file and the validation @UniqueEntity on the name of company. The error is well seen by doctrine, but the UniqueEntity validation doesn't work right, and the user is redirected to a 500 error page.

I tried a lot of things without any success, anyone has any idea on how to fix this ?

Here are my files :

In the Customer Entity :

/**
 * @ORM\ManyToOne(targetEntity="Site\UserBundle\Entity\Company", inversedBy="customers", cascade="persist")
 * @ORM\ JoinColumn(nullable=false)
 */
private $company;

In the Company entity

/**
 * Company
 *
 * @ORM\Table()
 * @ORM\Entity
 * @UniqueEntity(fields={"name"}, groups={"nameCompany"})
 */
class Company
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\OneToMany(targetEntity="Site\UserBundle\Entity\Customer", mappedBy="company")
 */
private $customers;

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=255, unique=true)
 */
private $name;

In the Customer Form

public function buildForm (FormBuilderInterface $builder, array $options)
{
    $builder->add('company', new CompanyType(), array('validation_groups' => array('nameCompany')));
}

/**
 *
 * @param OptionsResolverInterface $resolver            
 */
public function setDefaultOptions (OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(
            array(
                    'data_class' => 'Site\UserBundle\Entity\Customer',
                    'cascade_validation' => true,
            ));
}

In the Company Form :

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', null,          
            array(
                    'label' => 'entity.customer.company',
                    'translation_domain' => 'SiteUserBundle'
            ));
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Site\UserBundle\Entity\Company',
        'validation_groups' => array('nameCompany'),
    ));
}

Thanks A LOT in advance for any help !

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)