Symfony: Printing Multiple Forms Per View - symfony

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>

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) }}

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

how to override ProfileFormType to edit users? symfony2

I'm working with symfony2.I'm using FOSUserBundle, so i want to override FormType of edit user to add name to builder. so how i should do it?
what i've did :
ProfileFormType :
<?php
namespace UserBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use FOS\UserBundle\Form\Type\ProfileFormType as BaseType;
class ProfileFormType extends BaseType {
public function buildForm(FormBuilderInterface $builder, array $options) {
parent::buildForm($builder, $options);
// add your custom field
$builder->add('name')
->add('roles', 'collection', array(
'type' => 'choice',
'options' => array(
'choices' => array(
'ROLE_ADMIN' => 'Admin'))))
->add('image', new ImageType())
;
}
public function getName() {
return 'user_edit_profile';
}
}
services.yml
user.edit.form.type:
class: UserBundle\Form\Type\ProfileFormType
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: user_edit_profile }
edit.html.twig
{% extends "UserBundle::layout.html.twig" %}
{% block body %}
<center> <h1> Modification de profile </h1> </center>
<aside class="col-sm-3">
<div class="panel panel-default">
<div class="panel-heading">Modification</div>
<div class="panel-body">
Veuillez remplir les champs
</div>
</div>
</aside>
<!--timeline-->
<section class="timeline col-sm-9">
<!--post Timeline-->
<div class="thumbnail thumbnail-post">
<!--caption-->
<div class="caption">
<form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="form-horizontal">
<div class="form-group">
{{ form_errors(form.name) }}
<div class="col-sm-9">
Name {{ form_widget(form.name, { 'attr': {'class': 'form-control', 'placeholder': 'form.name'|trans } }) }}
</div>
</div>
<div class="form-group">
{{ form_errors(form.email) }}
<div class="col-sm-9">
Email {{ form_widget(form.email, { 'attr': {'class': 'form-control', 'placeholder': 'form.email'|trans } }) }}
</div>
</div>
<div class="form-group">
{{ form_errors(form.username) }}
<div class="col-sm-9">
Pseudo {{ form_widget(form.username, { 'attr': {'class': 'form-control', 'placeholder': 'form.username'|trans } }) }}
</div>
</div>
<div class="form-group">
{{ form_errors(form.current_password) }}
<div class="col-sm-9">
Mot de passe actuelle {{ form_widget(form.current_password, { 'attr': {'class': 'form-control', 'placeholder': 'form.current_password'|trans } }) }}
</div>
</div>
</br>
{{ form_rest(form) }}
<div class="form-group">
<div class="col-md-4 col-sm-4 col-xs-12 col-md-offset-3">
<input class="btn btn-default submit" type="submit" value="{{ 'registration.submit'|trans }}">
</div>
</div>
</form>
</div> <!--#caption-->
<!--#post timeline-->
</div>
<!--#timeline-->
</section>
{% endblock %}
{% block js %}
<script>
$(document).ready(function () {
$('#roles').hide();
});
</script>
{% endblock %}
i'm getting this error :
Neither the property "name" nor one of the methods "name()",
"getname()"/"isname()" or "__call()" exist and have public access in
class "Symfony\Component\Form\FormView" in
FOSUserBundle:Profile:edit.html.twig at line 28.
How can i resolve that.
profile:
form:
type: fos_user_profile
It needs that at config.yml of fos
Update your service file:
user.edit.form.type:
class: UserBundle\Form\Type\ProfileFormType
arguments: [%fos_user.model.user.class%]
decorates: fos_user.profile.form.type
tags:
- { name: form.type, alias: user_edit_profile }
fos_user.profile.form.type is mean our name of service which will be replace our service that name user.edit.form.type
Add use Symfony\Component\Form\FormBuilderInterface;
You got error:
Neither the property "name" nor one of the methods "name()", "getname()"/"isname()" or "__call()" exist and have public access in class "Symfony\Component\Form\FormView" in FOSUserBundle:Profile:edit.html.twig at line 28.
due to fos_user_user table has not name field. If you want to add field whitch not locate in table you'll should add field those fields in entity generate getters and setters and finaly create new migration and migrate up.

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

Symfony2.7 form not valid

I'm developping a blog using Symfony 2.7. I've an ArticleBundle and a CommentBundle. My entities Article and Comment are linked by a ManyToOne relation.
When I try to send a comment, it fails. The $form->isValid() method returns false.
Here, my add method to send a comment :
public function addAction(Article $article, Request $request){
$comment = new Comment();
$form = $this->createForm(new CommentType(), $comment);
$form->handleRequest($request);
if($form->isValid()){
$comment->setArticle($article);
$em = $this->getDoctrine()->getManager();
$em->persist($comment);
$em->flush();
return $this->redirect($this->generateUrl('esgi_article_view', array(
'id' => $article->getId()
)));
} else{
return $this->render('ESGICommentBundle:Comment:add.html.twig', array(
'form' => $form->createView(),
'article' => $article
));
}
}
My comment form is inclued into article's view like this :
{{ render(controller("ESGICommentBundle:Comment:add", { 'id' : article.id })) }}
And here my form :
{{ form_start(form) }}
<div class="form-group">
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-8">
{{ form_label(form.author, "Auteur") }}
{{ form_widget(form.author, { 'attr' : { 'class' : 'form-control' } }) }}
{{ form_errors(form.author) }}
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-8">
{{ form_label(form.email, "Email (optionnel)") }}
{{ form_widget(form.email, { 'attr' : { 'class' : 'form-control' } }) }}
{{ form_errors(form.email) }}
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-8">
{{ form_label(form.content, "Commentaire") }}
{{ form_widget(form.content, { 'attr' : { 'class' : 'form-control' } }) }}
{{ form_errors(form.content) }}
</div>
</div>
</div>
<button class="btn btn-success" type="submit">Poster</button>
{{ form_end(form) }}
Thanks, have a nice day !
IF you submit the form, you should have all the errors in the profiler bar(there is a form icon which you can click to see errors in the submitted form)

Resources