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
The street and zipcode fields in your AuthorRegistrationTypeare not related to your Address entity. What is the reason you did it this way? I would create a separate form type for your Address entity:
namespace WebsiteBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
class AddressType extends AbstractType
{
protected function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('street', TextType::class)
->add('zipCode', TextType::class)
;
}
}
Then you can embed this in your AuthorRegistrationType:
namespace WebsiteBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
class AuthorRegistrationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add("firstname")
->add("lastname")
->add("address", AddressType::class)
->add('save', SubmitType::class);
}
}
xabbuh is right:
I added in the controller the following:
$address = new Address();
$author->setAddress($address);
before
$form = $this->createForm(AuthorRegistrationType::class, $author, array(
'required' => false
));
And in AddressType:
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'WebsiteBundle\Entity\Address'
));
}
And it works!
In your entity Author you have an attribute address, which is another entity Address. In my opinion this is called embedded entity, not inherited. However I'm not a native English speaker, so I cannot guarantee.
The issue is in your entity Author:
/**
* #Assert\Valid
*/
protected $address;
This will validate the field itself, but not the fields street and zipCode from the entity Address. To achieve that, you should tell the validation to traverse to the entity Address and look up for the validations in there:
/**
* #Assert\Valid(
* traverse = true
* )
*/
protected $address;
This should do the job.
EDIT:
I couldn't see the relation between these two entities immediately in your examples. Now I see that Author can have only one Address. That means the above suggested code won't do the work for you. It is necessary only if you have a relation where an Author has a field address which is a collection, i.e one author can have many addresses.
Related
I have a lot of Categories in database.
Here is Category Entity
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity
* #ORM\Table(name="categories")
*/
class Category
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\ManyToOne(targetEntity="Category")
*/
protected $rootCategory;
/**
* #ORM\Column(type="text")
*/
protected $name;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return Category
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set rootCategory
*
* #param \AppBundle\Entity\Category $rootCategory
*
* #return Category
*/
public function setRootCategory(\AppBundle\Entity\Category $rootCategory = null)
{
$this->rootCategory = $rootCategory;
return $this;
}
/**
* Get rootCategory
*
* #return \AppBundle\Entity\Category
*/
public function getRootCategory()
{
return $this->rootCategory;
}
}
I want to get all categories in my edit form
EditFormType:
namespace AppBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use AppBundle\Controller\CategoryController;
class EditPhotoFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$categoryController = new CategoryController();
$builder->add('title', 'text');
$builder->add('description', 'textarea');
$builder->add('category', EntityType::class, array(
'class' => 'AppBundle:Category',
'choices' => $categoryController->getCategories(),
));
}
public function getName()
{
return 'app_photo_edit';
}
}
getCategories()
public function getCategories() {
$em = $this->getDoctrine()->getManager();
return $em->getRepository('AppBundle:Category')->findAll();
}
I am getting next error:
Error: Call to a member function has() on null
Thats because there is not Doctrine in controller object. Where should i get Doctrine and Repository in this case?
How should i do it correct way?
First, you should NEVER instantiate any Controller class yourself. Controller classes are used by Symfony's Kernel to handle a request, and they are loaded automatically with dependencies to do so.
Right here, you don't even need to require the EntityManager in your FormType, because EntityType has a built-in option query_builder to do what you need:
$builder->add('category', EntityType::class, array(
'class' => 'AppBundle:Category',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('c');
},
);
This should do the trick. (check here for more details)
However, if one day you really need to import a dependancy inside your Form (whether it is EntityManager or another service), here's how you should do:
A. import the given dependency in your constructor:
private $dependency;
public function __construct(Dependency $dependency)
{
$this->$dependency = $dependency;
}
B. Declare your Form as a Service, with your dependency's id as argument:
<service id="app.form.type.edit_photo"
class="AppBundle\Form\Type\EditPhotoFormType">
<tag name="form.type" />
<argument type="service" id="app.dependencies.your_dependency" />
</service>
Then use $this->dependency in your Form wherever you need.
Hope this helps! :)
So I'm trying to create a registration form in Symfony 2 which contains my "Person" entity. The person entity has a one-to-many join, and I want the registration form to allow the user to select a single instance of this "Many" side of the join.
The structure is Users and Institutions. A user can have many institutions. I want a user to select a single institution at registration time (but the model allows for more later).
The basic structure is:
RegistrationType -> PersonType -> PersonInstitutionType
…with corresponding models:
Registration (simple model) -> Person (doctrine entity) -> PersonInstitution (doctrine entity, oneToMany relation from Person)
I tried to pre-populate an empty Person & PersonInstitution record in the RegistrationController but it gives me the error:
Expected argument of type "string or Symfony\Component\Form\FormTypeInterface", "TB\CtoBundle\Entity\PersonInstitution" given
(ok above has been fixed).
I've moved the code from my website to here below, trying to remove all the irrelevant bits.
src/TB/CtoBundle/Form/Model/Registration.php
namespace TB\CtoBundle\Form\Model;
use TB\CtoBundle\Entity\Person;
class Registration
{
/**
* #var Person
*/
private $person
private $termsAccepted;
}
src/TB/CtoBundle/Form/RegistrationType.php
namespace TB\CtoBundle\Form;
use TB\CtoBundle\Form\PersonType;
class RegistrationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('person', new PersonType());
$builder->add('termsAccepted','checkbox');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'TB\CtoBundle\Form\Model\Registration',
'cascade_validation' => true,
));
}
public function getName()
{
return 'registration';
}
}
src/TB/CtoBundle/Entity/Person.php
namespace TB\CtoBundle\Entity;
use TB\CtoBundle\Entity\PersonInstitution
/**
* #ORM\Entity()
*/
class Person
{
/**
* #var ArrayCollection
* #ORM\OneToMany(targetEntity="PersonInstitution", mappedBy="person", cascade={"persist"})
*/
private $institutions;
}
src/TB/CtoBundle/Form/PersonType.php
namespace TB\CtoBundle\Form;
class PersonType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('institutions', 'collection', array('type' => new PersonInstitutionType()))
;
}
/**
* #param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'TB\CtoBundle\Entity\Person',
));
}
/**
* #return string
*/
public function getName()
{
return 'tb_ctobundle_person';
}
}
src/TB/CtoBundle/Entity/PersonInstitution.php
namespace TB\CtoBundle\Entity
/**
* PersonInstitution
*
* #ORM\Table()
* #ORM\Entity
*/
class PersonInstitution
{
/**
* #ORM\ManyToOne(targetEntity="Person", inversedBy="institutions", cascade={"persist"})
*/
private $person;
/**
* #ORM\ManyToOne(targetEntity="Institution", inversedBy="members")
*/
private $institution;
/**
* #ORM\Column(type="boolean")
*/
private $approved;
}
src/TB/CtoBundle/Form/PersonInstititionType.php
namespace TB\CtoBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class PersonInstitutionType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('approved')
->add('person')
->add('institution')
;
}
/**
* #param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'TB\CtoBundle\Entity\PersonInstitution'
));
}
/**
* #return string
*/
public function getName()
{
return 'tb_ctobundle_personinstitution';
}
}
src/TB/CtoBundle/Controller/Registration.php
namespace TB\CtoBundle\Controller;
class RegisterController extends Controller
{
/**
*
* #param Request $request
* #return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function registerAction(Request $request)
{
$registration = new Registration;
$person = new Person();
$institution = new PersonInstitution();
$person->addInstitution($institution);
$registration->setPerson($person);
// this causes error:
// Entities passed to the choice field must be managed. Maybe persist them in the entity manager?
// $institution->setPerson($person);
$form = $this->createForm(new RegistrationType(), $registration);
$form->handleRequest($request);
if($form->isValid()) {
$registration = $form->getData();
$person = $registration->getPerson();
// new registration - account status is "pending"
$person->setAccountStatus("P");
// I'd like to get rid of this if possible
// for each "PersonInstitution" record, set the 'person' value
foreach($person->getInstitutions() as $rec) {
$rec->setPerson($person);
}
$em = $this->getDoctrine()->getManager();
$em->persist($person);
$em->flush();
}
return $this->render('TBCtoBundle:Register:register.html.twig', array('form' => $form->createView()));
}
}
Here is a detailed solution for adding an Collection field to Person entity and formType.
Your complex question with Registration entity can be solved with this.
I suggest you to use this 3 entity related connection if it is really needed. (only because of termsAccepted data!?)
If you won't change your opinion, then use this annotation:
Registration code:
use TB\CtoBundle\Entity\Person;
/**
* #ORM\OneToOne(targetEntity="Person")
* #var Person
*/
protected $person;
Person code:
use TB\CtoBundle\Entity\PersonInstitution;
/**
* #ORM\OneToMany(targetEntity="PersonInstitution", mappedBy = "person")
* #var ArrayCollection
*/
private $institutions;
/* I suggest you to define these functions:
setInstitutions(ArrayCollection $institutions),
getInstitutions()
addInstitution(PersonInstitution $institution)
removeInstitution(PersonInstitution $institution)
*/
PersonInstitution code:
use TB\CtoBundle\Entity\Person;
/**
* #ORM\ManyToOne(targetEntity="Person", inversedBy="institutions", cascade={"persist"}))
* #var Person
*/
private $person;
PersonType code:
use TB\CtoBundle\Form\PersonInstitutionType;
->add('institutions', 'collection', array(
'type' => new PersonInstitutionType(), // here is your mistake!
// Other options can be selected here.
//'allow_add' => TRUE,
//'allow_delete' => TRUE,
//'prototype' => TRUE,
//'by_reference' => FALSE,
));
PersonController code:
use TB\CtoBundle\Entity\Person;
use TB\CtoBundle\Entity\PersonInstitution;
/**
* ...
*/
public funtcion newAction()
{
$person = new Person;
$institution = new PersonInstitution;
$institution->setPerson($person);
$person->addInstitution($institution);
$form = $this->createForm(new PersonType($), $person); // you can use formFactory too.
// If institution field is required, then you have to check,
// that is there any institution able to chose in the form by the user.
// Might you can redirect to institution newAction in that case.
return array( '...' => $others, 'form' => $form);
}
If you need more help in twig code, then ask for it.
I want to handle all validations with #Assert so I'm using Models for my web forms (Form Type) which are not mapped to database. The question I have is, is it an acceptable practise in Symfony world?
I know that one disadvantage of this way is not being able to automatically generate setters and getters. I read up on it but didn't get a clear picture so that's why I'm asking.
A rough example:
LoginType.php
namespace User\RecordBundle\Resources\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class LoginType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->setAction($options['action'])
->setMethod('POST')
->add('username', 'text', array('label' => 'Username'))
->add('button', 'submit', array('label' => 'Login'));
}
/**
* #param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array('data_class' => 'User\RecordBundle\Entity\UserEntity'));
}
/**
* Returns the name of this type.
*
* #return string The name of this type
*/
public function getName()
{
return 'login';
}
}
LoginModel.php
namespace User\RecordBundle\Resources\Form\Model;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Class LoginModel
* Mapped to Login form
*
* #package User\RecordBundle\Resources\Form\Model
*/
class LoginModel
{
/**
* #Assert\NotBlank(message = "The Username field should not be blank.")
*
* #var string $username
*/
protected $username;
/**
* #return string $username
*/
public function getUsername()
{
return $this->username;
}
/**
* #param string $username
* #return $this
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
}
This case: your FormType is not related to any Entity, must be rare in a well planned application. So rarely Model with FormType solution can be used, I don't have any objections to it. Remark: Specifically for User handling I recommend you to use friends of symfony created: FOS\UserBundle\FOSUserBundle().
You said that you're new in Symfony, so I summarized here the general practice of making a Form, which is related to an Entity and user will be available to fill some part of it.
class code:
class Entity
{
/**
* #Assert\NotBlank(message = "The data is empty.")
*/
private $data;
}
form type code:
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;
class EntityType extends AbstractType
{
/**
* #var \Doctrine\Common\Persistence\ObjectManager
*/
protected $om;
protected $admin;
protected $edit;
public function __construct($om, $admin = false, $new = false)
{
$this->om = $om;
$this->admin = $admin;
$this->new = $;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
// You can show specific parts of the Entity for the admin to fill and reduced group of fields for the common user.
// You can set specific behaviour in case of creation and editing.
// You can add a collection type field, and select specific entities with ObjectManager. To make your form more flexible.
$builder
->add('data', 'text', array(
'label' => 'Data text',
'label_attr' => array(
'class' => 'reqfield'
),
'attr' => array(
'class' => 'css-class-of-the-field'
'...' => $etc,
))
// You don't need to add submit button
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Application\EntityBundle\Entity\Entity'
));
}
public function getName()
{
return 'application_entity_bundle_entity';
}
}
// Actions like: new and edit, can use your formType above.
I am having an issue with Validating an EWZ recaptcha field in a form that I have in Symfony2.
Here is my Captcha Entity:
<?php
namespace Acme\FormBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints as Recaptcha;
/**
* Captcha
*
* #ORM\Table()
* #ORM\Entity
*/
class Captcha
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var boolean
*
* #ORM\Column(name="captcha", type="boolean")
*/
private $captcha;
/**
* #var integer
*
* #ORM\Column(name="uniqueid", type="integer")
*/
private $uniqueid;
/**
* #Recaptcha\True
*/
public $recaptcha;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set captcha
*
* #param boolean $captcha
* #return Captcha
*/
public function setCaptcha($captcha)
{
$this->captcha = $captcha;
return $this;
}
/**
* Get captcha
*
* #return boolean
*/
public function getCaptcha()
{
return $this->captcha;
}
/**
* Set uniqueid
*
* #param integer $uniqueid
* #return Captcha
*/
public function setUniqueid($uniqueid)
{
$this->uniqueid = $uniqueid;
return $this;
}
/**
* Get uniqueid
*
* #return integer
*/
public function getUniqueid()
{
return $this->uniqueid;
}
}
Here is my Form:
<?php
namespace Acme\FormBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\True;
class CaptchaType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('recaptcha', 'ewz_recaptcha')
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\FormBundle\Entity\Captcha'
));
}
public function getName()
{
return 'captchaType';
}
}
Here is my Controller:
<?php
namespace Acme\FormBundle\Controller;
use Acme\FormBundle\Entity\User;
use Acme\FormBundle\Entity\Workstation;
use Acme\FormBundle\Entity\Captcha;
use Acme\FormBundle\Form\CaptchaType;
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\True;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Form\FormBuilderInterface;
class DefaultController extends Controller
{
public function indexAction(Request $request)
{
$captcha = new Captcha();
$form = $this->createForm(new CaptchaType(), $captcha);
if ($request->isMethod('POST')) {
echo "1";
$form->bind($request);
echo "2";
if ($form->isValid()) {
echo "3A";
// perform some action, such as saving the task to the database
$session = $this->getRequest()->getSession();
$temptime = strtotime("now");
$session->set('uniqueid', $temptime);
return $this->redirect($this->generateUrl('customer_info'));
}else{
return $this->redirect($this->generateUrl('captcha'));
}
echo "3B";
}
return $this->render('AcmeFormBundle:Default:index.html.twig', array('form' => $form->createView()));
}
When I check to see if the form is valid, it never equals true. I'm sure that I'm missing something pretty easy, but I have been trying to figure this out for the past 4 hours, and I'm stuck.
Any help is greatly appreciated.
Thanks
After a lot Googling, I ended up fixing my problem. I ended up changing my buildform to the following:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('recaptcha', 'ewz_recaptcha', array(
'attr' => array(
'options' => array(
'theme' => 'clean'
)
),
'mapped' => false,
'constraints' => array(
new True()
),
'error_bubbling' => true
));
}
And changed my config.yml to this:
validation: { enable_annotations: false }
That fixed my issue. Kind of wish this was a little more clear in the installation instructions.
Oh well.
is there an easy way to allow choice fields in symfony to validate correctly with data that was added via js? So for example you load an empty field then populate it with js/ajax calls then select one of the options an press submit, but the validator always throws this option is not valid errors...
To give some background, Ive got a custom form type that uses the choice type as a parent, and also a custom data transformer that converts the options into entity's (which I can confirm works because if I change the form type to text and manually enter the id corresponding to the choice I would want to select, the form submits fine).
Any idea's? Im happy to provide any files you might want to have a look at?
Edit nullstateType.php
<?php
namespace ISFP\Index\IndexBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use ISFP\Index\IndexBundle\Form\Transformer\nullstateTransformer;
use Doctrine\Common\Persistence\ObjectManager;
class nullstateType extends AbstractType
{
/**
* #var ObjectManager
*/
private $om;
/**
* #param ObjectManager $om
*/
public function __construct(ObjectManager $om)
{
$this->om = $om;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$transformer = new nullstateTransformer($this->om);
$builder->prependNormTransformer($transformer);
}
public function setAllowedValues(OptionsResolverInterface $resolver)
{
return array( 'widget' => array('choice'));
}
public function getDefaultOptions(array $options)
{
return array(
'invalid_message' => 'The selected state does not exist',
'property_path' => false
);
}
public function getParent()
{
return 'choice';
}
public function getName()
{
return 'nullstate';
}
}
nullstateTransformer.php
<?php
namespace ISFP\Index\IndexBundle\Form\Transformer;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Doctrine\Common\Persistence\ObjectManager;
use ISFP\Index\IndexBundle\Entity\State;
class nullstateTransformer implements DataTransformerInterface
{
/**
* #var ObjectManager
*/
private $om;
/**
* #param ObjectManager $om
*/
public function __construct(ObjectManager $om)
{
$this->om = $om;
}
/**
* Transforms an object (state) to a string (id).
*
* #param Issue|null $state
* #return string
*/
public function transform($state)
{
if (null === $state) {
return "";
}
return $this->om->getRepository('ISFPIndexEntityBundle:State')->getId();
}
/**
* Transforms a string (id) to an object (state).
*
* #param string $id
* #return Issue|null
* #throws TransformationFailedException if object (state) is not found.
*/
public function reverseTransform($id)
{
if (!$id) {
return null;
}
$state = $this->om
->getRepository('ISFPIndexEntityBundle:State')
->findOneById(intval($id))
;
if (null === $state) {
throw new TransformationFailedException(sprintf(
'An state with id "%s" does not exist!',
$id
));
}
return $state;
}
}