How to add wrapper around multiple checkbox on Symfony 4 form - forms

I have a list of checkbox created in a form with the following
->add('ISPreNbStudents', ChoiceType::class, [
'multiple' => false,
'choices' => [
'1' => 1,
'2' => 2,
'3' => 3,
'4' => 4,
'5' => 5,
],
'expanded' => true
])
I can then display this in the twig file with this : {{ form_widget(form.ISOptMonths) }}
The problem is that it now display a list of label and input like this
<input type="radio" id="availability_ISPreNbStudents_0" name="availability[ISPreNbStudents]" value="1">
<label for="availability_ISPreNbStudents_0">1</label>
<input type="radio" id="availability_ISPreNbStudents_1" name="availability[ISPreNbStudents]" value="2">
<label for="availability_ISPreNbStudents_1">2</label>
I need to put wrapper around each label/input like this
<div class="styled-input-single">
<input type="checkbox" name="case-1" id="1" />
<label for="1">1</label>
</div>
<div class="styled-input-single">
<input type="checkbox" name="case-2" id="2" />
<label for="2">2</label>
</div>
How can I achieve this ?

I've finally found the solution :
Here is how you print your list :
{{ form_row(registrationForm.firstRecourse) }}
Now is you want to add wrapper around every option :
<div class="row">
{{ form_label(registrationForm.ISPreNbStudents) }}
{% for checkbox in registrationForm.ISPreNbStudents.children %}
<div class="styled-input-single">
{{ form_widget(checkbox) }}
{{ form_label(checkbox) }}
{{ form_errors(checkbox) }}
</div>
{% endfor %}
</div>

You have to use Symfony Customize Form Rendering.
This way you can access your form field by field, like:
{{ form_start(form) }}
<div class="my-custom-class-for-errors">
{{ form_errors(form) }}
</div>
<div class="row">
<div class="col">
{{ form_row(form.task) }}
</div>
<div class="col" id="some-custom-id">
{{ form_row(form.dueDate) }}
</div>
</div>
{{ form_end(form) }}
You are able to render the fields manually also:
<div class="styled-input-single">
{{ form_widget(form.name_of_form_element) }}
{{ form_label(form.name_of_form_element) }}
<!-- and for errors of this field -->
{{ form_errors(form.name_of_form_element) }}
</div>
For rendering a select or option box manually check this link:
https://symfonycasts.com

Related

Form symfony multiple choice

I have a classic form in Symfony and there is one input with 4 different values, I would like to render those 4 values in my twig instead of a drop-down box.
I tried to do it with a loop but I can't do it for an array.
Here is my code :
$isAdmin = !empty($this->getSap()->fetchSapQuery('RECAP_AUTHORITY_ADM', []));
$flux = $this->getSap()->fetchSapQuery('RECAP_FLUX_LIST', []);
$form = $this->createFormBuilder()
->add('Secteur', ChoiceType::class, array(
'choices' => $this->getSap()->fetchSapQuery('RECAP_SECTEUR_LIST', []),
'label' => $this->trans('Secteur'),
'choice_label' => 'LibLong',
'choice_value' => 'SecteurId',
'required' => true,
'placeholder' => '',
'attr' => [
//'data-app-auto-select-unique-option' => 1,
'multiple' => 'multiple', // secteur monoselection
],
))
Here is my Twig :
{% block body %}
<div class="row">
<div class="col-sm-12">
<div class="card-box ">
<h2 class="m-t-0 header-title">{{ pageTitle }}</h2>
<form id="searchForm" class="form-horizontal search-form " role="form" action="{{
path('api_post', {id: odata_search}) }}" method="post">
{#{{ dump(form.Secteur) }}#}
{{ form_row(form.Secteur) }}
{{ form_row(form.Flux) }}
{{ form_row(form.Canal) }}
{{ form_row(form.Periode) }}
{{ form_row(form.CodeExterne) }}
{{ form_row(form.EQART) }}
{{ form_row(form.Division) }}
{{ form_row(form.Username) }}
<div class="form-group">
<div class="col-md-offset-3 col-md-4">
<button id="search" type="submit" class="btn btn-success" data-notification-
clear="true">{{ "Rechercher"|trans }}</button>
{% if isAdmin %}
<button id="btnNew" type="button" class="btn btn-success" disabled><i
class="fa fa-plus-circle"></i> {{ "Creer un rapport vierge"|trans }}
</button>
{% endif %}
</div>

Symfony 2 csrf validation token

i want to delete an advert without going to /delete/{id} path and just deleting with a modal pop-in.
In the dev environment, no problem , my form validation works great like that :
form.html.twig :
<div class="well">
{# form_start(form, {'attr': {'class': ''}}) #}
<form name="monext_perfclientbundle_advert" method="post" action="" class="" enctype="multipart/form-data">
{# Les erreurs générales du formulaire. #}
{{ form_errors(form) }}
<div class="form-group">
{# Génération du label. #}
{{ form_label(form.title, "Titre de l'annonce", {'label_attr': {'class': 'col-sm-3 control-label'}}) }}
{# Affichage des erreurs pour ce champ précis. #}
{{ form_errors(form.title) }}
<div class="col-sm-4">
{# Génération de l'input. #}
{{ form_widget(form.title, {'attr': {'class': 'form-control'}}) }}
</div>
</div>
<div class="form-group">
{{ form_label(form.content, "Contenu de l'annonce", {'label_attr': {'class': 'col-sm-3 control-label'}}) }}
{{ form_errors(form.content) }}
<div class="col-sm-4">
{{ form_widget(form.content, {'attr': {'class': 'ckeditor'}}) }}
</div>
</div>
{{ form_row(form.category) }}
<button id="addFile">Ajouter un fichier</button>
<div id="uploads"></div><br>
{{ form_widget(form.save, {'attr': {'class': 'btn btn-primary'}}) }}
{{ form_rest(form) }}
{{ form_end(form) }}
</form>
info.html.twig:
<div class="modal-footer parente">
<div>Annuler</div>
<div>
<form class="nomargin" action="{{ path('info_delete', {'id': advert.id}) }}" method="post">
<input type="hidden" name="_csrf_token" value="{{ csrf_token('authenticate') }}">
<input type="submit" value="Supprimer" class="btn btn-danger"/>{{ form_rest(form) }}
</form>
</div>
</div>
deleteAction() :
$form = $this->createFormBuilder()->getForm();
if ($form->handleRequest($request)->isValid()) {
$em->remove($advert);
$em->flush();
$request->getSession()->getFlashBag()->add('info', "L'annonce a bien été supprimée.");
return $this->redirect($this->generateUrl('info'));
}
return $this->render('MonextPerfclientBundle:Default:delete.html.twig', array(
'advert' => $advert,
'form' => $form->createView()
));
It doesnt work in production environment i dont know why..
my dump :
The CSRF token is invalid. Please try to resubmit the form
i got it thanks to Luax my friend !
{{ form_start(form, {'action': path('info_delete', {'id': advert.id})}) }}
<input type="hidden" name="form[_token]" value="{{ csrf_token('form') }}">
<input type="submit" value="Supprimer" class="btn btn-danger"/>{{ form_rest(form) }}
{{ form_end(form) }}
Issue resolved :)

This form should not contain extra fields. on multile submite button

I have a two part form in symfony2 of a single table db.
On my edit page i display whole form with two save buttons.
when i edit some field and try to save it that shows the extra field error
my controller code is
$form = $this->createForm(new PatientfullType(), $test, array());
$formadd = $this->createForm(new PatientfulladdType(), $test, array());
.
.
if ($form->get('save')->isClicked()) {
return $this->render('AdminBundle:FormsController:patientformfull.html.twig', array('formadd' => $formadd->createView(),'form' => $form->createView() ));
// probably redirect to the add page again
}
else
{
return $this->render('AdminBundle:FormsController:patientformfull.html.twig', array('formadd' => $formadd->createView(),'form' => $form->createView() ));
}
my twig file has two object
form and formadd
<div class="row" style="margin-bottom: 15px;">
<div class="col-xs-3">
{{ form_label(form.sex) }}
</div>
<div class="col-xs-9 col-sm-4 col-md-3">
{{ form_widget(form.sex, {'attr': {'class': 'form-control'}}) }}
</div>
</div>
<div class="row" style="margin-bottom: 15px;">
<div class="col-xs-3">
{{ form_label(formadd.ChronicDiseases) }}
</div>
<div class="col-xs-9 col-sm-4 col-md-3">
{{ form_widget(formadd.ChronicDiseases, {'attr': {'class': 'form-control'}}) }}
</div>
</div>
</div><div class="col-xs-9 col-sm-5">
{{ form_widget(form.save , {'attr': {'class': 'btn btn-success'}}) }}
{{ form_widget(form.cancel , {'attr': {'onclick': 'cancelRedirect()','class': 'btn btn-default'}}) }}
</div>
</div><div class="col-xs-9 col-sm-5">
{{ form_widget(formadd.save1 , {'attr': {'class': 'btn btn-success'}}) }}
</div>

Default checked radio input not working in Laravel with Bootstrap syntax

Using Laravel 4's Radio form helper, I cannot use Bootstrap 3's radio button syntax and get the inputs to be checked by default.
Here is a normal radio button setup, that works properly
{{Form::radio('awesome_radio_input', 'yes', true)}} Yes
{{Form::radio('awesome_radio_input', 'no')}} No
And here is the BS3 syntax, where I am not getting any radio inputs checked by default
<div class="radio-inline">
<label>
{{Form::radio('silly_radio_input', 'yes', true)}}
Yes
</label>
</div>
<div class="radio-inline">
<label>
{{Form::radio('silly_radio_input', 'no')}}
No
</label>
</div>
Any ideas as to why the syntax would break the Radio helper?
Try this :
<div class="form-group">
{{ Form::label('gender', 'Gender') }}
<div class="form-inline">
<div class="radio">
{{ Form::radio('gender', 'true', true) }}
{{ Form::label('men', 'Men') }}
</div>
<div class="radio">
{{ Form::radio('gender', 'false') }}
{{ Form::label('woman', 'Woman') }}
</div>
</div>
</div>

how to display all form error at one place

I have seen various posts regarding same issue but nothing could become useful for me.
This is my Form
<form action="" method="post" {{ form_enctype(form) }} novalidate>
{{ form_errors(form) }}
<div class="login_field">
{{ form_label(form.name) }}
{{ form_widget(form.name) }}
</div>
<div class="clear"></div>
<div class="login_field">
{{ form_label(form.status) }}
{{ form_widget(form.status) }}
</div>
<div class="login_field">
<label> </label>
<input type="submit" value="Create" class="submit_btn" />
</div>
</form>
Error are not being displayed at all. How can i get out of this issue?
You have to include form_errors for every field ...
<div class="login_field">
{{ form_errors(form.name) }}
{{ form_label(form.name) }}
{{ form_widget(form.name) }}
</div>
... or just use form_row to render all the three of them together ...
<div class="login_field">
{{ form_row(form.name) }}
</div>
... or let your form errors bubble up to the top using the error_bubbling option for your form-fields in your FormType class. This means they will then be rendered through {{ form_errors(form) }}.
$builder->add('fieldname', 'text', array(
'error_bubbling' => true,
));
quick tip: you can include the submit button in your FormType since symfony 2.3 and don't have to manually render it (reference).
To display all the errors at top of form do like below
<ul>
{% for error in form.vars.errors.form.getErrors(true) %}
<li>{{ error.message }}</li>
{% endfor %}
</ul>