How can I get only specific elements of an entity with Symfony formbuilder?

In my formbuilder I create a select box from an entity:

$options['choice_label'] = function ( $entity) use ($name) {
                        if( $entity->getCategory() == null) {
                          return $entity->getName();
                        } 
                    };

In the case the Category is NULL, I want to get an option field and if not I do not want an option field.
But what happens is, that in the case the category is not NULL, I get empty option fields, where I am actually do not need an option field at all.

What I get:

<select>
  <option>value with category 1</option>
  <option>value with category 2</option>
  <option>value with category 3</option>
  <option></option>
  <option></option>
  <option></option>
  <option></option>
  <option></option>
  <option></option>
  <option></option>
  <option></option>
</select>

What I need:

<select>
  <option>value with category 1</option>
  <option>value with category 2</option>
  <option>value with category 3</option>
</select>

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *