سؤال

I've been reading about Symfony2 forms (http://symfony.com/doc/current/book/forms.html) and I've been searching the net for ways to customize my form without any success.

my first problem is about creating arbitrary form fields in my form. Fields which are not based on my DB model/entity. I do not want to save the values in the form fields but I do not need them when processing the form.

e.g. I want 2 radio fields to appear on my approval page. "approve", "rejected". when I try to add the approved radio button on my form, I get an error that says "approve" doesn't have the get/set functions.

$em = $this->getDoctrine()->getEntityManager();
$application = $em
    ->getRepository('MyApp\GenericBundle\Entity\Application')
    ->find($applicationId);

$form = $this->createFormBuilder($application)
    ->setMethod('POST')
    ->add('approved', 'radio')
    ->getForm();

Thanks for the help

هل كانت مفيدة؟

المحلول

Use the mapped attribute that allows you to prevent the field from being persisted

$form = $this->createFormBuilder($application)
    ->setMethod('POST')
    ->add('approved', 'radio', array('mapped' => false))
    ->getForm();

Doc: http://symfony.com/doc/current/reference/forms/types/form.html#mapped

نصائح أخرى

If you're trying to perform two actions on the form you could use alternate submit buttons; One for "Approve" one for "Reject"

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top