Use FormTheme for CollectionType? - symfony

I'm using for my symfony project the Ninsuo JqueryCollection plugin https://symfony-collection.fuz.org/symfony3/, and I have a form that has a collectionType, except that I want the targeted entity to appear with its own layout, but I do not know how to apply it to CollectionType
I have my "subform" like this
SubForm :
{{form_start(form)}}
<div class="card">
<h5 class="card-header">Nouvel utilisateur</h5>
<div class="card-body">
{{form_row(form.email)}}
<div class="row align-items-center">
<div class="col-md-4">
{{form_row(form.nom)}}
</div>
<div class="col-md-4">
{{form_row(form.prenom)}}
</div>
<div class="col-md-4">
{{form_row(form.service)}}
</div>
</div>
{{form_row(form.roles)}}
</div>
</div>
{{form_end(form)}}
And my main Form :
{{form_start(form)}}
{{form_row(form.utilisateurs, {'attr': {'class': 'my-selector jquery-buttons-collection mt-3'} })}}
<button type="submit" class="btn btn-primary">Enregistrer</button>
{{form_end(form)}}
I would like to apply the layout of the 1st form for my form.users. Someone would know how?
EDIT:
My main form :
$builder
->add('utilisateurs', CollectionType::class, [
'entry_type' => AdminUserRegistrationType::class,
'entry_options' => [
'label' => false,
],
'allow_add' => true,
'allow_delete' => true,
'delete_empty' => true,
'by_reference' => true,
'prototype' => true,
'label' => false,
'attr' => array(
'class' => 'my-selector',
'label' => false,
),
'by_reference' => false,
]);
;
With the template :
{{form_start(form)}}
{{form_row(form.utilisateurs, {'attr': {'class': 'my-selector jquery-buttons-collection mt-3'} })}}
<button type="submit" class="btn btn-primary">Enregistrer</button>
{{form_end(form)}}
The form.utilisateurs formType :
$builder
->add('nom', TextType::class)
->add('prenom', TextType::class)
->add('email', EmailType::class)
->add('service', EntityType::class, [
'class' => GroupeValidateurs::class,
'query_builder' => function (GroupeValidateursRepository $repo) {
return $repo->createQueryBuilder("g")
->orderBy("g.nom", 'ASC');
},
'choice_label' => 'nom',
'label' => "Service",
'placeholder' => 'Service'
])
->add('roles', ChoiceType::class, [
'label' => "Rôles",
'choices' => [
'Admin' => 'ROLE_ADMIN',
'Direction' => 'ROLE_DIRECTION',
'RH' => 'ROLE_RH',
],
"expanded" => true,
"multiple" => true,
])
;
And the form theme :
{% block utilisateursType_label %}{% endblock %}
{% block utilisateursType_errors %}{% endblock %}
{% block utilisateursType_widget %}
<div class="card">
<h5 class="card-header">Nouvel utilisateur</h5>
<div class="card-body">
{{form_widget(form.email)}}
<div class="row align-items-center">
<div class="col-md-4">
{{form_widget(form.nom)}}
</div>
<div class="col-md-4">
{{form_widget(form.prenom)}}
</div>
<div class="col-md-4">
{{form_widget(form.service)}}
</div>
</div>
{{form_widget(form.roles)}}
</div>
</div>
{% endblock %}
But I don't know how find the block name

Related

How to add parent div with form builder in symfony

form created by form builder
<form name="post" method="post">
<div id="post">
<div class="form-group">
<input type="text" id="post_title" name="post[title]" required="required" class="form-control"/>
</div>
<div class="form-group">
<textarea id="post_content" name="post[content]" required="required" class="form-control"></textarea>
</div>
<div>
<button type="submit" id="post_postAdd" name="post[postAdd]" class="form-control btn-primary">Create</button>
</div>
<input type="hidden" id="post__token" name="post[_token]" value="ab35508c9.6MnqGe6WDVVVtkdRga_Ahtt5kDssuXri3JBx4HcVdOw.gKCQYdugYSw2_x88wN2y6Z5P_mtJyxaqm95GhkRDP52qoYFjucJOGyTuIA" />
</div>
</form>
i want this. How i create parent (div) elements one or more like below with form builder. Not in twig.
<div class="oneMore"> <!-- I need this element -->
<div class="iWantToAddThis"> <!-- I need this element -->
<div class="form-group">
...input...
</div>
<div class="form-group">
...input...
</div>
</div>
</div>
PostType.php. I created form like below.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class, [
'attr' => [
'class' => 'form-control'
],
'row_attr' => [
'class' => 'form-group'
]
])
->add('content',TextareaType::class, [
'attr' => [
'class' => 'form-control'
],
'row_attr' => [
'class' => 'form-group'
]
])
->add('postAdd', SubmitType::class, [
'attr' => [
'class' => 'form-control btn-primary'
]
]);
}
_form.html.twig (I don't want to add anything in twig)
{{ form(form) }}

Symfony: Printing Multiple Forms Per View

I created a form with builderform. The view shows a list of private messages when you click one of the messages, a modal window opened, all of this works, but when I copy the form into the modal, only the last private message shows this form, others modal windows doesn't print this.
This is my form:
class MessageReplyFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('reply', TextareaType::class, array(
'label'=>false,
'required'=>true,
"attr" => array('placeholder'=>'Escribe aquí tu mensaje...')
))
->add('image', FileType::class, [
'label' => false,
'required' => false,
'data_class' => null,
'mapped' => false,
'attr'=>[
'class' => 'input-image',
'lang'=>'es',
'placeholder' => "Subir imagen ..."
],
'constraints' => [ new Image(['maxSize' => '500k'])
]
])
->add('send', SubmitType::class, array(
"label" => 'send',
"attr" => array(
"class" => "form-submit btn btn-default btn-vibrisas btn-reply"
)
))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => MessageReply::class,
]);
}
}
Here my view...
<div class="card">
<div class="card-body publications-container">
<h1 class="pub">Mensajes privados</h1>
<hr>
{% for p in private_messages %}
<div class="card message-card">
<div class="card-body message-body" >
<!-- Information about each message -->
</div>
</div>
<!-- The Modal -->
<div id="openMessage-{{ p.id }}" tabindex="-1" class="openMessage modal fade" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<!-- basic message information -->
</div>
<!-- Modal body -->
<div class="modal-body">
<div class="message-f">
{{ p.emitter.name }}<br>
{{ p.message }}<br>
{% if p.image != null %}
<div class="openImage" data-toggle="modal" data-target="#openImage-{{ p.id }}" href="/uploads/users/messages/{{ p.image }}">
<img class="message-image" src="{{ asset('/uploads/users/messages/'~p.image) }}"/>
</div>
<div id="openImage-{{ p.id }}" class="openImage modal" tabindex="-1" role="dialog">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<img src="{{ asset('/uploads/users/messages/'~p.image) }}"/>
</div>
</div>
</div>
{% endif %}
</div>
{% form_theme message_reply 'bootstrap_4_layout.html.twig' %}
{{ form_start(message_reply) }}
<input type="text" name="idmessage" required class="form-control border-input hidden" placeholder="" value="{{ p.id }}">
{{ form_end(message_reply) }}
</div>
</div>
</div>
</div>
<!-- end modal -->
{% endfor %}
</div>

Child in Form Collection not submitted

I have a form with a collection field. When I submit the parent form, children added in the collection field are not submitted.
Here is what the Profiler says :
Request/Response tab :
I added a child in creneaux collection but as you can see, there isn't any child submitted in creneaux.
I don't have any error in the submitted form though.
I can't find where I'm mistaken.
Parent form :
class MiseEnLigneFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('nom', TextType::class, ['label' => 'Titre'])
->add('tempsEpreuve', TimeType::class,
['label' => 'Durée',
'label_attr' => ['class' => 'active'],
'required' => true,
'widget' => 'single_text'])
->add('creneaux', CollectionType::class,
['label' => false,
'label_attr' => ['class' => 'active'],
'entry_type' => CreneauFormType::class,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false
])
->add('polycopies', CollectionType::class,
['label' => false,
'label_attr' => ['class' => 'active'],
'entry_type' => PolycopieFormType::class,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false
]);
}
Child form :
class CreneauFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('dateDebut', DateTimeType::class, ['label' =>'Début',
'label_attr' => ['class'=>'active'],
'format' => 'dd-MM-yyyy HH:mm',
'widget' => 'single_text',
'attr' => ['data-field'=>'datetime']])
->add('dateFin', DateTimeType::class, ['label' => 'Fin',
'label_attr' => ['class'=>'active'],
'format' => 'dd-MM-yyyy HH:mm',
'widget' => 'single_text',
'attr' => ['data-field'=>'datetime']])
->add('test', CheckboxType::class, ['label' => false, 'required' => false]);
}
Controller :
$originalCreneaux = new ArrayCollection();
foreach ($colle->getCreneaux() as $creneau) {
$originalCreneaux->add($creneau);
}
$form = $this->createForm(MiseEnLigneFormType::class, $colle);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
return $this->render('Creneau/edit.html.twig', ['form' => $form->createView(),
'colle' => $colle,
'matiereName' => $matiereName]);
}
}
Twig :
{{ form_start(form) }}
<div class="card card-content">
{{ form_widget(form.nom) }}
{{ form_label(form.nom) }}
{{ form_widget(form.tempsEpreuve) }}
{{ form_label(form.tempsEpreuve) }}
</div>
<div class="card card-content">
<table class="polycopies centered"
data-prototype="{{ form_widget(form.polycopies.vars.prototype)|e }}">
<thead>
<tr>
<th>Nom</th>
<th>Supprimer</th>
</tr>
</thead>
<tbody class="poly_body">
{% for poly in form.polycopies %}
<tr>
<td>
{{ form_widget(poly.nom) }}
{{ form_label(poly.nom) }}
</td>
<td>
<a href="" class="delete_poly_link">
<i class="material-icons">delete</i>
</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="" id="liensAjouterPolycopies">
<a href="" class="add_poly_link btn-flat" data-tooltip="Ajouter un polycopié">
<i class="material-icons">add</i>Ajouter un polycopié
</a>
</div>
<div class="card card-content">
<span class="card-title">Créneaux</span>
<table class="creneaux centered"
data-prototype="{{ form_widget(form.creneaux.vars.prototype)|e }}">
<thead>
<tr>
<th>Date de début</th>
<th>Date de fin</th>
<th>Test</th>
<th>Supprimer</th>
</tr>
</thead>
<tbody class="creneau_body">
{% for creneau in form.creneaux %}
<tr>
<td class="center-align">
{{ form_widget(creneau.dateDebut) }}
{{ form_label(creneau.dateDebut) }}
</td>
<td class="center-align">
{{ form_widget(creneau.dateFin) }}
{{ form_label(creneau.dateFin) }}
</td>
<td class="center-align">
{{ form_widget(creneau.test) }}
{{ form_label(creneau.test) }}
</td>
<td class="center-align">
<a href="" class="delete_creneau_link">
<i class="material-icons">delete</i>
</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{{ form_rest(form) }}
</div>
<button class="btn-large fullwidth" type="submit" value="Sauvegarder">
<i class="material-icons left">save</i>
Enregistrer
</button>
{{ form_end(form) }}
<script>
var collectionHolder = $('table.creneaux');
var num = $('.creneau_body').children().length + 1;
jQuery(document).ready(function () {
$(document).on("click", "a.add_creneau_link", function (e) {
e.preventDefault();
addCreneauForm(collectionHolder);
return false;
});
$(document).on("click","a.delete_creneau_link", function () {
var $this = $(this);
$this.closest('tr').remove();
return false;
});
function addCreneauForm(collectionHolder) {
var prototype = collectionHolder.attr('data-prototype');
var newForm = prototype.replace(/__name__/g, num);
num += 1;
$tableau = newForm.split("<div>");
var form = '';
for (var i = 1; i < $tableau.length; i++) {
form += "<td class='center-align'>" + $tableau[i] + "</td>";
}
form += "<td class='center-align'><a href='' class='delete_creneau_link'><i class='material-icons'>delete</i></a></td>";
var $form = $('<tr></tr>').append(form);
$form.insertAfter('.creneaux tr:last');
}
</script>
There is no persist nor flush after the submit event
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($creneau);
$entityManager->flush();
Did you accidently forget ?

In Laravel 5.3 when I submit the form all inputs fields are not showing in controller

Form created in create.blade.php
{!! Form::open(['route' => 'employees.store', 'class' => 'form', 'enctype' => 'multipart/form-data']) !!}
<div class="form-group">
{!! Form::label('name', 'Name:', ['class' => 'control-label']) !!}
{!! Form::text('name', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('male', 'Male:', ['class' => 'control-label']) !!}
{!! Form::radio('gender', 'Male', false, ['id' => 'male']) !!}
{!! Form::label('female', 'Female:', ['class' => 'control-label']) !!}
{!! Form::radio('gender', 'Female', false, ['id' => 'female']) !!}
</div>
<div class="form-group">
{!! Form::label('dob', 'DOB:', ['class' => 'control-label']) !!}
{!! Form::date('dob', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('image', 'Image:', ['class' => 'control-label']) !!}
{!! Form::file('image') !!}
</div>
<div class="form-group">
<strong>Hobbies:</strong><br>
{!! Form::label('playing', 'Cricket', ['class' => 'control-label']) !!}
{!! Form::checkbox('hobbies[]', 'Cricket', false, ['id' => 'playing']) !!}
{!! Form::label('music', 'Music', ['class' => 'control-label']) !!}
{!! Form::checkbox('hobbies[]', 'Music', false, ['id' => 'music']) !!}
{!! Form::label('travelling', 'Travelling', ['class' => 'control-label']) !!}
{!! Form::checkbox('hobbies[]', 'Travelling', false, ['id' => 'travelling']) !!}
</div>
<div class="form-group">
{!! Form::label('email', 'Email:', ['class' => 'control-label']) !!}
{!! Form::text('email', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('number', 'Number:', ['class' => 'control-label']) !!}
{!! Form::text('number', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('city', 'City:', ['class' => 'control-label']) !!}
{!! Form::select('city', ['Bikaner' => 'Bikaner', 'Jaipur' => 'Jaipur', 'Delhi' => 'Delhi'], false, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('address', 'Address:', ['class' => 'control-label']) !!}
{!! Form::textarea('address', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Create', ['class' => 'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
Controller
public function store(Request $request)
{
$input = $request->all();
echo "<pre>";print_R($input);exit;
}
When I submit the form with Gender, Image, Hobbies fields as empty then these fields does not appears in the $input in the controller, but when I submit form after filling these fields then it is shown in $input.
So why they don't appear when these fields are empty?

Create fixed navbar adn fixed scrolled sidebar in adminLTE integration with yii2

I use yii with adminLTE from this git, dmstr.
So, I copied the layout from example-view's folder to my view/layout
My question is
How to make the navbar is fixed
The sidebar is fixed , but can scrolled ?
This is my code :
navbar
<?php
use yii\helpers\Html;
/* #var $this \yii\web\View */
/* #var $content string */
?>
<header class="main-header">
<?= Html::a('<span class="logo-mini">TMS</span><span class="logo-lg">' . Yii::$app->name . '</span>', Yii::$app->homeUrl, ['class' => 'logo']) ?>
<nav class="navbar navbar-static-top" role="navigation">
//Some element
</nav>
</header>
sidebar
<aside class="main-sidebar">
<section class="sidebar">
<!-- Sidebar user panel -->
<div class="user-panel">
<div class="pull-left image">
<img src="<?= $directoryAsset ?>/img/user2-160x160.jpg" class="img-circle" alt="User Image"/>
</div>
<div class="pull-left info">
<p>Dzil Jalal</p>
<i class="fa fa-circle text-success"></i> Online
</div>
</div>
<!-- search form -->
<form action="#" method="get" class="sidebar-form">
<div class="input-group">
<input type="text" name="q" class="form-control" placeholder="Search..."/>
<span class="input-group-btn">
<button type='submit' name='search' id='search-btn' class="btn btn-flat"><i class="fa fa-search"></i>
</button>
</span>
</div>
</form>
<!-- /.search form -->
<?=
dmstr\widgets\Menu::widget(
[
'options' => ['class' => 'sidebar-menu'],
'items' => [
['label' => 'Menu Yii2', 'options' => ['class' => 'header']],
['label' => 'Gii', 'icon' => 'fa fa-file-code-o', 'url' => ['/gii']],
['label' => 'Debug', 'icon' => 'fa fa-dashboard', 'url' => ['/debug']],
['label' => 'Login', 'url' => ['site/login'], 'visible' => Yii::$app->user->isGuest],
[
'label' => 'Same tools',
'icon' => 'fa fa-share',
'url' => '#',
'items' => [
['label' => 'Gii', 'icon' => 'fa fa-file-code-o', 'url' => ['/gii'],],
['label' => 'Debug', 'icon' => 'fa fa-dashboard', 'url' => ['/debug'],],
[
'label' => 'Level One',
'icon' => 'fa fa-circle-o',
'url' => '#',
'items' => [
['label' => 'Level Two', 'icon' => 'fa fa-circle-o', 'url' => '#',],
[
'label' => 'Level Two',
'icon' => 'fa fa-circle-o',
'url' => '#',
'items' => [
['label' => 'Level Three', 'icon' => 'fa fa-circle-o', 'url' => '#',],
['label' => 'Level Three', 'icon' => 'fa fa-circle-o', 'url' => '#',],
],
],
],
],
],
],
],
]
)
?>
</section>
</aside>
For the help, it so appreciated
To make the navbar fixed, put
class="navbar navbar-fixed-top"
instead of
class="navbar navbar-static-top"

Resources