1

I have a check box that Renders correctly . If there is no data from the database I want the check box to be checked by default.

If I set "data" = True it displays the checkbox checked. Now if I get the data from the database it won't override the checkbox to the boolean value of false. If I remove the data" => true then I am able to get the correct checkbox from the database but I am not able to set the default checkbox.

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

    $builder
        ->add('isFieldFirstname', 'checkbox', array(
        'label'    => 'Show First Name',
        'required' => false,
            "data" => true,
            )
                )

Just to recap

  1. Database - No record found - show default checkbox as checked
  2. If record found use value from database ex isFieldFirstname = false checkbox unchecked
3
  • 1
    Try to set the value as true in the entity __construct() function. Commented Apr 24, 2015 at 20:13
  • @A.L thanks for your response. I tried that but it seems that data option is called last. Commented Apr 24, 2015 at 20:26
  • 1
    If __construct() set the default value of isFieldFirstname as true and you pass it to the form, you shouldn't need to use the data attribute. Commented Apr 24, 2015 at 20:29

1 Answer 1

1

You implement a buildForm method to describe the structure of the form, not the values. The values are from a plain array or a data object. Symfony uses the DataAccess component to map them.

  • drop "data" option in your checkbox
  • call your checkbox "fieldFirstname" (no "is")

// MyFormType.php

public function buildForm(FormBuilderInterface $builder, array $options) {
  $builder
    ->add(
      'fieldFirstname',
      'checkbox',
      [
        'label'    => 'Show First Name',
        'required' => false
      ]
    );
}
  • create a data class

// MyFormData.php

class MyFormData {
  private $fieldFirstname;

  public function isFieldFirstname() {
    return $this->fieldFirstname;
  }

  public function setFieldFirstname($fieldFirstname) {
    $this->fieldFirstname = $fieldFirstname;
  }
}
  • create your action

// MyController.php

// ...
public function myAction() {
  $data = new MyFormData();
  $data->setFieldFirstname(
    ! $myService->isDataInDatabase()
  );

  $form = $this->createForm(
    new MyFormType(),
    $data
  );

  // ... 
}

Didn't test it right now, but should work.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you i misunderstood the usage of "is" thanks for the detail clarification.
oh okay, if that was the problem, than have a look at the DataAccess component which is trying to access public $x, isX(), getX(), hasX() for reading x and public $x, setX($x) for writing x for objects. For arrays it's just the the key $arr['x'] I think and also all methods of ArrayAccess (maybe I forgot some).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.