I installed Symfony3 and I'm trying to validate a formchild (entity child/not mapped fields) inside a normal form, with @Assert\Valid annotation. I couldn't do it so I tried the example from the Manual: http://symfony.com/doc/current/reference/constraints/Valid.html
This example, in Symfony 3, doesn't work (at least for me). This is where @Assert\Valid is used. How does Symfony knows in this case (example from manual) to valid the Address Entity and not any other Entity?
/**
* @Assert\Valid
*/
protected $address;
Has someone tried to test the example from the manual to see if it works?
Can someone please provide the working example from the manual? I don't know what I'm doing wrong..
This is my TestingController.php:
namespace WebsiteBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use WebsiteBundle\Entity\Author;
use WebsiteBundle\Form\Type\AuthorRegistrationType;
class TestingController extends Controller
{
public function registerAccountAction(Request $request)
{
$author = new Author();
$form = $this->createForm(AuthorRegistrationType::class, $author, array(
'required' => false
));
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
echo "It works";
}
return $this->render('TemplatesBundle:Default:testing_registration.html.twig', array(
'form' => $form->createView(),
));
}
}
The AuthorRegistrationType.php:
namespace WebsiteBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
class AuthorRegistrationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add("firstname")
->add("lastname")
->add("zipcode", TextType::class, array('mapped' => false))
->add("street", TextType::class, array('mapped' => false))
->add('save', SubmitType::class);
}
}
Author Entity:
namespace WebsiteBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class Author
{
/**
* @Assert\NotBlank
* @Assert\Length(min = 4)
*/
protected $firstname;
/**
* @Assert\NotBlank
*/
protected $lastname;
/**
* @Assert\Valid
*/
protected $address;
/**
* @return mixed
*/
public function getFirstname()
{
return $this->firstname;
}
/**
* @param mixed $firstname
*/
public function setFirstname($firstname)
{
$this->firstname = $firstname;
}
/**
* @return mixed
*/
public function getLastname()
{
return $this->lastname;
}
/**
* @param mixed $lastname
*/
public function setLastname($lastname)
{
$this->lastname = $lastname;
}
/**
* @return mixed
*/
public function getAddress()
{
return $this->address;
}
/**
* @param mixed $address
*/
public function setAddress(Address $address)
{
$this->address = $address;
}
}
Address Entity:
namespace WebsiteBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class Address
{
/**
* @Assert\NotBlank()
*/
protected $street;
/**
* @Assert\NotBlank
* @Assert\Length(max = 5)
*/
protected $zipCode;
/**
* @return mixed
*/
public function getStreet()
{
return $this->street;
}
/**
* @param mixed $street
*/
public function setStreet($street)
{
$this->street = $street;
}
/**
* @return mixed
*/
public function getZipCode()
{
return $this->zipCode;
}
/**
* @param mixed $zipCode
*/
public function setZipCode($zipCode)
{
$this->zipCode = $zipCode;
}
}
This is what I get now:
Firstname
This value should not be blank.
Lastname
This value should not be blank.
Street
Zipcode
So: the main Entity is validated, but the inherited one (Street/Zipcode) is "ignored"..
How can I validate (with this metod, not creating Custom Validation) the child entity?
Thanks