I divided the user account area into different forms, then for the registration form I collect these bits like this:
class RegistrationFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', 'email', array(
'label' => '* Email address:'
))
->add('account_personal', 'my_personalinfo_form', array(
'property_path' => 'account'
))
->add('account_contact', 'my_contactinfo_form', array(
'property_path' => 'account'
))
;
}
The problem is that error messages for account_personalbubble up at the top of the form. For example, "Please enter your name" if the first name remains blank in the personal information form. "Personal" and "contact" forms work great in their own forms on their pages.
Error messages for account_contactare accurate and are displayed next to the correct fields.
, ->add ( account_contact), ; account_personal , account_contact !
, !
----- EDIT -----
:
class PersonalInfoType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', 'text')
->add('first_name', 'text', array(
'required' => true,
'label' => '* First name:'
))
->add('last_name', 'text', array(
'required' => true,
'label' => '* Surname:'
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'My\UserBundle\Entity\Account',
'validation_groups' => array('personalInfo')
));
}
public function getName()
{
return 'my_personalinfo_form';
}
:
class ContactInfoType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('postcode', 'text', array(
'required' => true
))
->add('address_1', 'text', array(
'required' => true
))
->add('address_2', 'text', array(
'required' => false
))
->add('address_3', 'text', array(
'required' => false
))
->add('town', 'text')
->add('phone_daytime', 'text', array(
'required' => true
))
->add('phone_mobile', 'text', array(
'required' => true
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'My\UserBundle\Entity\Account',
'validation_groups' => array('contactInfo')
));
}
public function getName()
{
return 'my_contactinfo_form';
}
Account :
/**
* My\UserBundle\Entity\Account
*
* @ORM\Entity
* @ORM\Table(
* name="accounts"
* )
*/
class Account
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=25, nullable=true)
* @Assert\Choice(choices = {"Mr", "Mrs", "Miss", "Ms", "Dr", "Prof"}, message = "Choose a valid title", groups={"personalInfo"})
*/
protected $title;
/**
* @ORM\Column(type="string", length=150, nullable=true)
* @Assert\NotBlank(message="Please enter your first name", groups={"personalInfo"})
* @Assert\Length(max=150, maxMessage="null|Your first name must have less than {{ limit }} characters", groups={"personalInfo"})
* @Assert\Regex(
* pattern="/\d/",
* match=false,
* message="Your name cannot contain a number"
* )
*/
protected $first_name;
/**
* @ORM\Column(type="string", length=150, nullable=true)
* @Assert\NotBlank(message="Please enter your last name", groups={"personalInfo"})
* @Assert\Length(max=150, maxMessage="null|Your last name must have less than {{ limit }} characters", groups={"personalInfo"})
* @Assert\Regex(
* pattern="/\d/",
* match=false,
* message="Your name cannot contain a number"
* )
*/
protected $last_name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @MyAssert\Phone(message="Your daytime phone number is not valid", groups={"contactInfo"})
*/
protected $phone_daytime;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @MyAssert\MobilePhone(message="Your mobile phone number is not valid", groups={"contactInfo"})
*/
protected $phone_mobile;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank(message="Please enter the first line of your address", groups={"contactInfo"}
* @Assert\Length(max=250, maxMessage="null|The first line of your address must have less than {{ limit }} characters", groups={"contactInfo"})
*/
protected $address_1;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\Length(max=250, maxMessage="null|The second line of your address must have less than {{ limit }} characters", groups={"contactInfo"})
*/
protected $address_2;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\Length(max=250, maxMessage="null|The third line of your address must have less than {{ limit }} characters", groups={"contactInfo"})
*/
protected $address_3;
/**
* @ORM\Column(type="string", length=45, nullable=true)
* @Assert\NotBlank(message="Please enter your town", groups={"contactInfo"})
* @Assert\Length(max=45, maxMessage="null|Your town name must have less than {{ limit }} characters", groups={"contactInfo"})
*/
protected $town;
/**
* @ORM\Column(type="string", length=45, nullable=true)
* @Assert\NotBlank(message="Please enter your postcode", groups={"contactInfo"})
* @MyAssert\Postcode(message="Invalid postcode entered", groups={"contactInfo"})
*/
protected $postcode;
... etc