I want to know how to configure a textbox in twig to allow only letters, like a field type to write only names for example. I have something like this on my view:
<div class="control-group">
<label class="control-label">{{form_label(form.name,'Name: ')}}</label>
<div class="controls">
{{form_widget(form.name,{'attr':{'placeholder':'Insert only text','min':'1'}, 'id':'1'})}}
</div>
sorry, here´s my symfony php form code
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('name')
->add('surname')
->add('address')
->add('idSex')
//->add('birthdate', 'date',array('required'=>true))
->add('birthdate', 'date', [
'widget' => 'single_text',
'format' => 'yyyy-MM-dd',
'attr' => [
'class' => 'form-control input-inline datepicker',
'data-provide' => 'datepicker',
'data-date-format' => 'yyyy-mm-dd'
]
])
->add('observation')
->add('idCmf')
;
}
You can do this with HTML 5 by using the input's pattern
attribute.
The pattern attribute uses regex, which is really good for this sort of task. One where you want a certain kind of answer, but don't care about specifics.
In your example:
<div class="control-group">
<label class="control-label">{{ form_label(form.name, 'Name: ') }}</label>
<div class="controls">
{{ form_widget(form.name, {'attr':{'pattern': '[a-zA-Z]*', ...} }) }}
</div>
</div>
Or in the formType:
// formType.php
//...
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('name', 'text', array(
'attr' => ['pattern' => '[a-zA-Z]*']
))
// I'm assuming you'd use it for both.
->add('surname', 'text', array(
'attr' => ['pattern' => '[a-zA-Z]*']
))
//...
}