I am still new to Symfony2. I am using Symfony version 2.3 and twig version 1.0. and I'm trying to change the name value.
Here is my code:
<div class="control-group">
{{ form_label(form.menuTitle, null, {'label_attr': {'class': 'control-label'}}) }}
{{ form_errors(form.menuTitle)}}
<div class="controls">
<div class="span12">
{{ form_widget(form.menuTitle, {'attr': {'class': 'span6'}}) }}
</div>
</div>
</div>
How can I do it?
If you want to change the label of a child within your form array you can do this by adding the "label" attribute in the form builder. Here's a good description: In Symfony2, how do I get labels from the FormBuilder
Otherwise please be a bit more specific what you mean by change "name value" ?
Related
I am trying to style my input field in Symfony 3.4 with symfony forms for some duration(hours and minutes only). I am trying with clockpicker but I don't know where I am wrong. If someone can help me or there is a better solution. I have downloaded and included my js and css in web folder.
I am trying to make it like this in the moment:
My twig:
<div class="form-group col-md-4">
<label for="time_for_cooking">Време за готвене(минути):</label>
{% if form_errors(form.recipeCookingTime) %}
{{ form_widget(form.recipeCookingTime, { 'attr': {'class': 'form- control clockpicker-control clockpicker is-invalid'} }) }}
<div class="col">
<small class="text-danger">
{{ form_errors(form.recipeCookingTime) }}
</small>
</div>
{% else %}
{{ form_widget(form.recipeCookingTime, { 'attr': {'class': 'form control clockpicker-control clockpicker'} }) }}
{% endif %}
</div>
My RecipeType.php
->add('recipeCookingTime', TimeType::class)
document ready
<script>
$(document).ready(function() {
('.clockpicker').clockpicker();
} );
</script>
and what its generated, I think the problem is these selects that being genereted
In your FormType you have to set the html5-attribute to false to get an input.
See here https://symfony.com/doc/current/reference/forms/types/time.html#html5
I have a form that has let's say : formField1, formField2, ... formField10.
In my twig view, I want the 5 first form fields to be displayed.
My code looks like something like this :
{{ form_widget(form.formField1, {'attr': {'class': 'form-control'}}) }}
{{ form_widget(form.formField2, {'attr': {'class': 'form-control'}}) }}
{{ form_widget(form.formField3, {'attr': {'class': 'form-control'}}) }}
{{ form_widget(form.formField4, {'attr': {'class': 'form-control'}}) }}
{{ form_widget(form.formField5, {'attr': {'class': 'form-control'}}) }}
So when I submit this, it's fine for those fields : everything works fine in the databse.
But for the other fields (6 to 10), my database shows "NULL" for everything...
So my solution for now is to do this :
<div class="form-group hidden">
<div class="col-md-10 col-sm-9 col-xs-12">
{{ form_rest(form) }}
</div>
</div>
But it's not very safe (F12 + remove hidden property :))
Any idea how NOT to touch those other fields ?
I tried to disable them too :
{{ form_widget(form.formFieldX, {'attr': {'class': 'form-control'}, 'disabled': true}) }}
but it doesn't work either (set to NULL).
The only safe solution is to exclude these fields from the form type (for example by adding options to turn them on/off under certain conditions).
The Form component, by default, clears (i.e. sets them to null) all form fields that are part of the form type, but not of the request. This can be turned off, but this option is rather meant to allow support for HTTP PATCH requests. Furthermore, if you still kept the fields in your form type, your users could as well manually add the needed HTML code for it and submit them meaning they would again be changed.
I'm using Symfony to make a website for a tennis club, and I'm beating my head down about something :
I want to display an input field based on the option selected in a dropdown list.
This is the scenario :
I'm the admin of the website, and I want to make a reservation. If the reservation is a tournament (selected from a ChoiceType list), I want to display an input field to enter the tournament name.
I want to do something that would look like this in my twig view :
<div class="form-group">
<div class="col-xs-4">
{{ form_label(form.reservationType) }}
{{ form_widget(form.reservationType, {'attr': {'class': 'form-control' }}) }}
</div>
</div>
{% if reservationType == "tournament" %}
<div class="form-group">
<div class="col-xs-4>
{{ form_label(form.tournamentName) }}
{{ form_widget(form.tournamentName) }}
</div>
</div>
{% endif %}
Is it possible to do that just with twig ?
Thanks in advance!
You must use jQuery to solve this issue :
$(document).ready(function(){
$('.reservation').change(
var reservation = $(this).val();
if (reservation == 'xxxx'){
$('.tourName').show();
}else{
$('.tourName').hide();
}
);
});
<div class="form-group">
<div class="col-xs-4">
{{ form_label(form.reservationType) }}
{{ form_widget(form.reservationType, {'attr': {'class': 'form-control reservation' }}) }}
</div>
</div>
<div class="form-group">
<div class="col-xs-4>
{{ form_label(form.tournamentName) }}
{{ form_widget(form.tournamentName, {'attr': {'class': 'hidden tourName' }}) }}
</div>
</div>
No it is not possible only with twig.
What you can do is add a script to your template:
<div class="form-group">
<div class="col-xs-4">
{{ form_label(form.reservationType) }}
{{ form_widget(form.reservationType, {'attr': {'class': 'form-control reservation-type' }}) }}
</div>
</div>
<script type="text/javascript" src="{{ asset('bundles/yourBundle/theNameOfTheScriptYouPutInRessourcesPublic.js') }}"></script>
Then in the script (with jquery) you just listen to change event on the select to insert the input.
$('select.reservation-type').change(function(
if($(this).val() == 'tournament')
{
$('<input type="text" />').appendTo($(this).parent('form-group'));
}
));
If your inputs need twig variables or something you can add the inputs as hidden in the twig template and then in the script you just change the type from hidden to text or whatever you need:
$('select.reservation-type').change(function(
if($(this).val() == 'tournament')
{
$('input[name="tournament-name"]').prop('type', 'text');
}
));
If you don't want to use javascript you could consider using a form event listener: http://symfony.com/doc/current/form/dynamic_form_modification.html
I would like to use prototype like here http://symfony.com/doc/current/cookbook/form/form_collections.html#allowing-new-tags-with-the-prototype, but my form looks:
<div class="form-group row">
<div class="col-md-4">{{ form_widget(productAttribute.attribute, {'attr': {'class': 'form-control'}}) }}</div>
<div class="col-md-6">{{ form_widget(productAttribute.value, {'attr': {'class': 'form-control'}}) }}</div>
</div>
Before the loop is a div with data-prototype attribute which contains form fields without wrapped divs and classes and I don't know how to get wrapped divs with classes there. Thank you for your advice.
Twig side:
<div id="template" style="display: none">
{{ form_widget(form.tags.vars.prototype)|e }}
</div>
Js side:
var templateContent = $.("#template").html();
Or, twig:
{% set subform = form.tags.vars.prototype %}
{{ form_row(subform.fielName) }}
...
I'm new to symfony and twig. I want to build my html forms using BootStrap CSS Framework. So my form looks like this in twig file:
<form action="{{ path('register')}}" class="form-horizontal span10 offset7" id="frmRegistration" method="POST" {{ form_enctype(form) }} novalidate>
<fieldset>
{{ form_widget(form._token) }}
<div class="control-group">
{{ form_label(form.userName, null, {'label_attr': {'class':'control-label'}})}}
<div class="controls">
{{ form_widget(form.userName, {'attr': {'data-path': path('ajax_user_exists') } }) }}
</div>
</div>
<div class="control-group">
{{ form_label(form.password.first, null, {'label_attr': {'class':'control-label'}})}}
<div class="controls">
{{ form_widget(form.password.first) }}
</div>
</div>
<div class="control-group">
{{ form_label(form.password.second, null, {'label_attr': {'class':'control-label'}})}}
<div class="controls">
{{ form_widget(form.password.second) }}
</div>
</div>
<div class="control-group">
<div class="controls">
<input id="register_submit" type="submit" value="تاييد" class="btn btn-primary" />
</div>
</div>
</fieldset>
Now I want to show validation error messages exactly in front of each input. My problem is that how can I do this using twig? If I use {{ form_row(form.firstName) }} it will generate the label and input. But I cant wrap them inside the Bootstrap form structure. Any help is appreciated in advance.
UPDATE
Sorry for not being precise when reading symfony documentation. I found the solution. Using {{ form_errors(form.firstName) }} solved the problem.
This is still showing up as unanswered and thought it would be helpful if the answer was added at the end so this could be closed.
To add an error message on a form row, use:
{{ form_errors(form.firstName) }}
You will need to add the widget with this and the label if you want a label to show. Full rendering for each row requires all three:
{{ form_label(form.firstName) }}
{{ form_errors(form.firstName) }}
{{ form_widget(form.firstName) }}