How to add parent div with form builder in symfony - 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) }}

Related

Symfony - Asterisk on required label, bug on ChoiceType field

in my project Symfony 4 I want to put asterisk next to the labels whose fields are mandatory.
According to the doc ', there are several ways to do this: https://symfony.com/doc/4.0/form/form_customization.html#adding-a-required-asterisk-to-field-labels
But the CSS method is the easier.
So in my CSS file, I added this code :
label.required:after {
content: " *";
color: red;
font-weight: bold;
}
It works to a detail. In forms, when I have a ChoiceType field, the asterisk does not go on the main label but on all possible choices.
With this HTML render :
<div class="col-2">
<fieldset class="form-group">
<legend class="col-form-label required">Ajouter/Supprimer</legend>
<div id="role_choix">
<div class="form-check">
<input type="radio" id="role_choix_0" name="role[choix]" required="required" class="form-check-input" value="ajouter">
<label class="form-check-label required" for="role_choix_0">Ajouter</label>
</div>
<div class="form-check">
<input type="radio" id="role_choix_1" name="role[choix]" required="required" class="form-check-input" value="supprimer">
<label class="form-check-label required" for="role_choix_1">Supprimer</label>
</div>
</div>
</fieldset>
</div>
This is my formType :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('choix', ChoiceType::class, [
'label' => 'Ajouter/Supprimer',
'choices' => [
'Ajouter' => 'ajouter',
'Supprimer' => 'supprimer',
],
'multiple' => false,
'expanded' => true,
])
->add('role', ChoiceType::class, [
'label' => "Ajouter un rôle à cet utilisateur",
'choices' => [
'Admin' => 'admin',
'Direction' => 'direction',
'RH' => 'rh',
]
])
;
}
And my Twig :
{{form_start(form)}}
<div class="row items-align-center">
<div class="col-md-2">{{form_row(form.choix)}}</div>
<div class="col-md-4">{{form_row(form.role)}}</div>
</div>
<button type="submit" class="btn btn-primary">Enregistrer</button>
{{form_end(form)}}
So the bug is only on radio buttons
Can someone help me please ?
I know this is a very old question, but I found that this solution works for me:
label.required:not(.form-check-label):after {
content: "\2000*";
color: red;
}
You set the css property on label and that's working. What you want for radios is to put it on legend. You have to adapt the css selector.
Does the radio group and the select label share one class attribute ?

Use FormTheme for CollectionType?

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

bootstrap cakephp help needed

i am creating a form with cakephp2 and bootstrap everything is working fine but im stuck in little confusion and i cant fix this kindly any bootstrap or cake expert help me thanks,
Check this envelop its stretching upwards
here is my code
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">
<span class="glyphicon glyphicon-envelope"></span>
</span>
<?php echo $this->Form->input('email',
array(
'class' => 'form-control',
'type' => 'email',
'placeholder' => 'Enter Email'
)
); ?>
</div>
<!--<input type="email" class="form-control" id="email" placeholder="Enter email" required="required" /></div>-->
</div>
Pass the argument 'label' => false will prevent to display the label
Use
<?php echo $this->Form->input('email',
array(
'class' => 'form-control',
'type' => 'email',
'placeholder' => 'Enter Email',
'label' => false
)
); ?>
You should use label false in your code.
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">
<span class="glyphicon glyphicon-envelope"></span>
</span>
<?php echo $this->Form->input('email',
array(
'class' => 'form-control',
'type' => 'email',
'label'=>false,
'placeholder' => 'Enter Email'
)
); ?>
</div>
<!--<input type="email" class="form-control" id="email" placeholder="Enter email" required="required" /></div>-->
</div>

Formbuilder Symfony2 - Searching by subcategory

I have tried this code in my twig : some stupid tests :/
<form class="form-horizontal" id="form-search" action="{{path('search_robe')}}" method="POST" enctype="multipart/form-data">
{% for robeoption in robeoptions %}
{% if robeoption.values|length>0 %}
<div class="col-md-3 col-sm-3 col-xs-3 ">
<div class="Robeoption-title">
{{robeoption.translate.title}}
</div>
<div class="Robeoption-value">
{% for robeoptionval in robeoption.values %}
<label>
<input type="checkbox" class="radio" value="1" name="{{robeoption.translate.title}}" />{{robeoptionval.translate.title}}</label>
{% endfor %}
</div>
</div>
{% endif %}
{% endfor %}
</form>
I have this code in my Form Type
class SearchrobeType extends AbstractType {
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('robeoptions', 'entity', array(
'required' => false,
'class' => 'BrainAdminBundle:RobeOption',
/** #Ignore */
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('s')
->Join('s.values', 'values')
->groupBy('s.id');
},
'label' => true,
'multiple' => true,
'expanded' => true,
))
->add('robeoptionvalue', 'entity', array(
'required' => false,
'class' => 'BrainAdminBundle:RobeOptionValue',
/** #Ignore */
'label' => false,
'multiple' => true,
'expanded' => true,
))
;
}
So, the point is that i Have Two entities : robeoption and robeoptionvalues which have a oneTomany relation , I'm new to Symfony and I want to use the formBuilder in my twig so it will be something like that instead :
<form class="form-horizontal" id="form-search" action="{{path('search_robe')}}" method="POST" enctype="multipart/form-data">
{{form_widget(form.robeoptions)}} //
{{form_widget(form.robeoptionvalue)}}
</form>
I want to show each Robeoption in a label and just under each one its Robeoptionsvalue with an input checkbox button, Any help plz?
There is only one way to do that : it's to do it dynamically from my twig `
<form class="form-horizontal col-md-12 col-sm-12 col-xs-12" id="form-search" action="{{path('searchrobe')}}" method="POST" enctype="multipart/form-data">
<div class="sh" style="display:none;">
{{form_row(form._token)}}
<div class="col-md-12 col-sm-6 search-slide">
{% for robeoption in robeoptions %}
{% if robeoption.values|length>0 %}
<div class="col-md-4 col-sm-4 col-xs-4 ">
<div class="Robeoption-title">
<label> {{robeoption.translate.title}} </label>
</div>
<div class="Robeoption-value">
{% for robeoptionval in robeoption.values %}
<input type="checkbox" name="robeoptionValues[{{robeoptionval.id}}]" />{{robeoptionval.translate.title}}<br>
{% endfor %}
</div>
</div>
{% endif %}
{% endfor %}
</div>
<div class="col-md-12 col-sm-12 search-slide">
<div class="col-md-12 col-sm-12 contour">
<button class="btn btn-default btn-slider">{{'search'|trans({})}}</button>
</div>
</div>
</div>
</form>
And then get the array of options from controller
if ($_request->getMethod() == 'POST') {
$optionsvalues=$_request->request->get('robeoptionValues');
$robe = $_em->getRepository('BrainAdminBundle:Robe')->searchforRobes($optionsvalues);
}
searchforRobes($optionsvalues); Is a function in the Robe Repository

Symfony (Silex) radio choice form builder does not render attr

I'm trying to add attributes to my rendered radios
$builder
->add('myRadios', 'choice', array(
'choices' => array(
'one' => 'uno',
'two' => 'due'),
'multiple' => false,
'attr' => array('class' => 'testClass'),
'expanded' => true
the output is:
<div class="control-group">
<label class="control-label required">Myradios</label>
<div class="controls">
<label for="form_one_0" class="required radio">
<input type="radio" id="form_one_0" name="form[one]" required="required" value="uno" />
<span>Uno</span>
</label>
<label for="form_two_1" class="required radio">
<input type="radio" id="form_two_1" name="form[two]" required="required" value="due" />
<span>Due</span>
</label>
</div>
</div>
no references to class='testClass'
I can't find any issue online
Try this way Adam,
$form = $app['form.factory']->createBuilder('form')
->add('myRadios', 'choice', array(
'choices' => array(
'one' => 'uno',
'two' => 'due'),
'multiple' => false,
'expanded' => true,
'attr' => array('class' => 'testClass'),
))
->getForm();
it works:
<div id="form_myRadios" class="testClass">
<input type="radio" id="form_myRadios_0" name="form[myRadios]" required="required" value="one">
<label for="form_myRadios_0" class="required">uno</label>
<input type="radio" id="form_myRadios_1" name="form[myRadios]" required="required" value="two">
<label for="form_myRadios_1" class="required">due</label>
</div>
How does your twig code look like? And which form template do you use?
You use a custom form template because
<div class="control-group">
<label class="control-label required">Myradios</label>
<div class="controls">
is definitely not standard Symfony!

Resources