Reputation: 68376
I want to render a form widget. This is the raw HTML I want to generate:
<input type="email" class="input-long" placeholder="Enter Email" name="email" id="email" required="required">
Using this:
{{ form_row(form.email, { 'type' : 'email', 'attr' : { 'class' : 'input-long', 'placeholder': "Enter Email", 'name': "email", 'id':"email", 'required': "required"}}) }}
did not generated the required output (for example, the input type was "text").
How do I custom render a form widget to specify the input type, placeholder etc?
Update
Here is my form class:
class ContactType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('fist_name')
->add('last_name')
->add('email')
->add('token')
->add('is_validated')
->add('created_at')
->add('updated_at')
->add('promotion')
->add('refferer')
;
}
public function getName()
{
return 'acme_contactlistbundle_contacttype';
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Acme\ContactlistBundle\Entity\Contact',
);
}
}
{{ form.email }}
renders this:
<label for="acme_contactlistbundle_contacttype_email" class=" required">Email</label><input id="acme_contactlistbundle_contacttype_email" name="acme_contactlistbundle_contacttype[email]" required="required" type="text">
When form built adding widget email as add->('email','email')
It rendered this:
<label for="acme_contactlistbundle_contacttype_email" class=" required">Email</label><input id="acme_contactlistbundle_contacttype_email" name="acme_contactlistbundle_contacttype[email]" required="required" type="email">
BUT, I don't want the label rendered (it is messing up my carefully laid out page!).
In the end, I got fed up and used this ugly piece of hackery ... (fobj is the form object passed in from the controller action):
Upvotes: 3
Views: 9947
Reputation: 17976
If your requirements to form rendering are different from what Symfony2 provides, you then could define your own widget. Just extend form theme and redefine needed block:
{% form_theme form _self %}
{% block field_widget %}
{% set type = type|default('text') %}
<input type="{{ type }}" {{ block('widget_attributes') }} value="{{ value }}" />
{% endblock field_widget %}
And, of course, check documentation on form theming
UPDATE
If you want to render only <input>
tag without <label>
- then you should redefine your field widget in next way:
{% block _contact_email_row %}
<div>
{{ form_errors(form) }}
{{ form_widget(form) }}
</div>
{% endblock %}
_contact
is for your Type
name.
_email
is for field name.
Upvotes: 1