我有2个表单类型的实体: User2Type.php是Pca2Type.php的子表单。
如果表单没有正确填写,User2Type.php的错误会继续冒泡到"用户" Pca2Type.php的财产。 如何获取错误,使它们显示在相应字段旁边。我希望用户名,电子邮件和密码错误显示在其字段旁边。
我将发布表单类型。
Pca2Type.php
namespace Homecare\HomecareBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class Pca2Type extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('firstName')
->add('lastName')
->add('umpi')
->add('agency', 'entity', array( 'class' => 'HomecareHomecareBundle:Agency', 'property' => 'agencyName', 'placeholder' => 'N/A' ) )
->add('create','submit', array(
'label' => 'Create Pca'
) )
;
$builder->add('users', 'collection', array('type' => new User4Type(), 'allow_add' => true, 'by_reference' => false, 'error_bubbling' => false ));
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Homecare\HomecareBundle\Entity\Pca',
'validation_groups' => array( 'adminCreatePca' ),
));
}
/**
* @return string
*/
public function getName()
{
return 'homecare_homecarebundle_pca';
}
}
User2Type.php
namespace Homecare\HomecareBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;
use Homecare\HomecareBundle\Entity\Recipient;
use Homecare\HomecareBundle\Entity\Pca;
use Homecare\HomecareBundle\Entity\User;
class User2Type extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('firstName')
->add('lastName')
->add('username')
->add('email', 'email')
->add('password', 'password')
->add('agency', 'entity', array( 'class' => 'HomecareHomecareBundle:Agency', 'property' => 'agencyName', 'placeholder' => 'N/A' ) )
->add('save','submit')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Homecare\HomecareBundle\Entity\User',
'validation_groups' => array( 'adminCreateAdmin', 'adminCreateAgency' )
));
}
/**
* @return string
*/
public function getName()
{
return 'homecare_homecarebundle_user2';
}
}