symfony/symfony — Issue #2926: [Form] Adding option 'hidden'
Author: jstout24
Created: 2011-12-19 18:53:56+00:00
State: open

Basically, I'm creating a new issue since PR #1951 got pretty out of hand and my proposal for this functionality differs completely from the PR itself.

Although the 'entity_id' type seemed like a good solution for just EntityType, I feel that merely ALL types should have the ability to be rendered as 'hidden'.

Then @RapotOR came up with this solution to be built into EntityType itself:

```php
add($options['property'], 'text')
->add('id', 'hidden')
->prependClientTransformer(new EntityToArrayTransformer(
$options['choice_list'], $options['property']
))
;
} else {
if ($options['multiple']) {
$builder
->addEventSubscriber(new MergeCollectionListener())
->prependClientTransformer(new EntitiesToArrayTransformer($options['choice_list']))
;
} else {
$builder->prependClientTransformer(new EntityToIdTransformer($options['choice_list']));
}
}
}

// [...]

public function getParent(array $options)
{
return $options['widget'] === 'text' ? 'form' : 'choice';
}
```

This looks like a great idea and everything too, but again, I feel that most types should be able to be rendered as hidden.

Basically, allowing option `` 'hidden' => true `` in types that may need it will fix all these issues everyone has with wanting to add cool javascript functionalities. (javascript date/datetime pickers, entity auto-completion, etc). I've needed this many times.

For example:

**DateType:**

```php

//
//

$builder->add('publishedAt', 'date', array(
'widget' => 'choice', // or 'widget' => 'text'
'hidden' => true
));
```

```php

$builder->add('publishedAt', 'date', array(
'widget' => 'single_text',
'hidden' => true
));
```
**EntityType:**

```php

$builder->add('users', 'entity', array(
'class' => 'AcmeHelloBundle:User',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('u')
->orderBy('u.username', 'ASC');
},
'hidden' => true
));
```

... and if developers want to add an extra non hidden text-field for the auto-completion part, they can just add a property in their form model object so that it can be populated on errors and such.

Anyway, to me, I feel that this would be the most clean and less confusing way of handling hidden fields.

What do you think? @bschussek @Gregwar @jmikola @RapotOR @fabpot ?