Reputation: 570
I'm building a form which have default value in some fields:
$builder->add('field', 'text', array('data' => 'Default value');
Then render this form field in Twig like:
{{ form_widget(form.field) }}
It's worked OK, but I don't want 'Default value' set in input tag rendered in HTML, because I only want this default value set internal that End-user doesn't aware about this value. Is there any built-in method in Symfony2 to handle it or I have to make some custom code?
Upvotes: 0
Views: 105
Reputation: 20193
You could modify your entity in order to do this:
class MyEntity{
const DEFAULT_FOO = "Default value";
// ...
private $foo;
// ...
public function setFoo($foo){
if ( $foo === null ){
$foo = self::DEFAULT_FOO;
}
$this->foo = $foo;
}
// ...
}
And then make sure that you set by_reference
in order to ensure setter is being invoked each time:
$builder->add('field', 'text', array(
'by_reference' => true
));
Upvotes: 2
Reputation: 4806
As far i understand
try to add required false and handle it in your controller action.
$builder->add('field', 'text', array('required' => false));
Upvotes: 0