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>
Related
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>
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
I'm currently working on a private social network for fun and to improve my skills.
Anyway, I want to display a form to leave a comment on a post using a loop but I have issues trying that.
I'm using Symfony, twig and bootstrap, and I'd like to keep js, jquery or ajax away.
Here is my code:
My form builder:
class CommentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', null, [
"label" => "Your title"
])
->add('content', null, [
"label" => "Your content"
])
->add('submit', SubmitType::class, [
"label" => "Publish your shitty comment!"
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Comment::class,
]);
}
}
and my view:
<ul class="nav col-4 offset-3">
{% for post in posts %}
<li class="postItem">
<article class="postItem">
<p> {{ post.title }} </p>
<img id="postPics" src="{{ asset('./pictures/posts/' ~ post.picture) }}">
{{ form(leaveCommentForm) }}
<h3>{{ post.title }}</h3>
<p>{{ post.content }}</p>
<author>{{ post.user.username }}</author>
<div class="commentPost">
{% for comment in post.comments %}
<article>
<h6 style="font-weight: bold">{{ comment.title }} -
By
<author>{{ comment.user.username }}
on {{ comment.dateCreated | date('Y-M-d') }}</author>
</h6>
<p>{{ comment.content }}</p>
</article>
{% endfor %}
</div>
<input type="hidden" value="{{ post.id }}" name="postId"> {{ post.id }}
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
Leave a comment
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="myModalLabel">Leave a comment</h4>
</div>
<div class="modal-body">
{{ form_start(leaveCommentForm) }}
<input type="hidden" value="{{ post.id }}" name="postId"> {{ post.id }}
{{ form_end(leaveCommentForm) }}
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
</article>
</li>
{% endfor %}
</ul>
As you can see I want to display my form on each iteration of post (with or without modal I don't really care I was just trying some things).
My problems here are:
*If I use a modal, I can't get my post.id inside my modal and so I can't send it to my controller.
Actually I get the value of first post.id for every posts.
*If I don't use a modal I can't even see my form on my page.
If you have a solution that would be great!
Thanks in advance guys!
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 :)
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>