2

I created a form with one checkbox.

UserSettingsType.php:

public function buildForm(FormBuilderInterface $builder, array $options) {

    $builder->add('newsletter', 'checkbox', array(
        'label' => 'Newsletter erhalten',
        'attr' => array(
            'class' => 'form-control',
        ),
        'required' => false,
    ));

}

In the UserSettings.php Entity:

/**
 * @ORM\Column(name="newsletter", type="boolean")
 */
protected $newsletter;

In the User.php:

/**
 * @ORM\Column(type="integer", nullable=true)
 */
protected $user_settings_id;

/**
 * @ORM\OneToOne(targetEntity="UserSettings", cascade={"persist"})
 * @ORM\JoinColumn(name="user_settings_id", referencedColumnName="id")
 */
protected $settings;

In the PageController.php i handle the settings action:

public function settingsAction() {

    $user = $this->getUser();

    if ($user->getSettings() !== null) {
        $settings = $user->getSettings();
    } else {
        $settings = new UserSettings($user);
    }

    $settings_form = $this->createForm(new UserSettingsType(), $settings);

    $request = $this->getRequest();

    if ($request->getMethod() == 'POST') {

        $em = $this->getDoctrine()->getManager();

        $settings_form->bind($request);

        if ($settings_form->isValid()) {

            $user->setSettings($settings);
            $em->persist($user);
            $em->flush();

        }

    }

    return $this->render('MyCompMyAppBundle:Page:settings.html.twig', array(
        'settings_form' => $settings_form->createView(),
    ));

}

I want to change the checkbox values from false (unchecked) / true (checked) to 'no' / 'yes' and change the definition of the newsletter field to: * @ORM\Column(name="newsletter", type="string", columnDefinition="ENUM('yes', 'no')") It would be nice if there would be 'yes' and 'no' enum values in the database. Please correct me if i am wrong: There is no way to change this via form element attributes, right? I heard something about a DataTransformer:. But is there any easier way to realize this?

Mathlight
  • 6,436
  • 17
  • 62
  • 107
titanbird
  • 183
  • 3
  • 15
  • Why are you wanting to use no/yes rather then true/false? – qooplmao Jun 17 '14 at 11:34
  • This is a habit. It is better to read in the database and in MySQL statements. But maybe I should change my habit easily? – titanbird Jun 17 '14 at 11:47
  • 2
    I would recommend that change of habit rather than trying to change everything else to fit. Also yes/no only fits in English so it's not very portable from a language side of things. Other good things come from being able to do boolean checks like `if ($user->getNewsletter())` rather the the longer (and possibly easily messed up with iffy spellings) `if ($user->getNewsletter() === 'yes'))`. – qooplmao Jun 17 '14 at 11:53

2 Answers2

2

do you want checkbox or radio button? for checkbox in peresentation use:

    $builder->add('newsletter', 'choice', array(
        'label' => 'Newsletter erhalten',
        'attr' => array(
            'class' => 'form-control',
        ),
        'choices' => array(array('yes' => 'yes'), array('no' => 'no')),
        'expanded' => true,
        'multiple' => true,
        'required' => false,
    ));
mohsenJsh
  • 2,048
  • 3
  • 25
  • 49
1

Don't use ENUM for this!

In MySQL, use either data type BIT(1) or TINYINT(1) (=same as BOOLEAN). See here: Which MySQL data type to use for storing boolean values

In PostgreSQL, there is a true BOOLEAN type. But no ENUM. So if you're ever thinking about migrating, better get rid of ENUM ;-)

Community
  • 1
  • 1
Thomas Landauer
  • 7,857
  • 10
  • 47
  • 99