Symfony嵌入了表单和约束

时间:2016-02-24 09:36:14

标签: php symfony-forms symfony value-objects

想象一下像这样的简单实体

def selectPacket(x):
    if x == '00': 
        return '00'
    elif x == "01":
        Date = datetime.now()
        return str(Date.microsecond)
    elif x == "02":
        return '98'
    elif x == "03":
        return '98'
    elif x == "04":
        return '98'
    elif x == "05":
        return '98'
    else: 
        return '99'

和这样的表单类型

class User {
  /**
   * @ORM\Embedded(class="Role")
   */
  protected $role;
}

和Role实体是RoleType表单

class UserType extends AbstractType
{
  /**
   * @param FormBuilderInterface $builder
   * @param array $options
   */
  public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('role', RoleType::class);
}

/**
 * @param OptionsResolver $resolver
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(
        array(
            'data_class' => 'XXXBundle\Entity\User',
            'csrf_protection' => false,
        )
    );
}
}

使用典型的form-> isValid,选择约束断言无法正常工作......实际上并未验证角色名称值。

1 个答案:

答案 0 :(得分:0)

我找到了Symfony3的解决方案

    /**
     * @ORM\Embedded(class="Role")
     * @Assert\Valid()
     */
    protected $role;