Symfony2.7 form not valid - symfony

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)

Related

How to use template form when edit object

I have a form with an embedded form (collectionType). I made a template. It works when I add a new object. But how to use this template when I edit an object ? Below my code an 2 pictures to illustrate my problem
{% extends 'base.html.twig' %}
{% form_theme form _self %}
{% block _paris_rencontres_row %}
<div class="sports" id="renc-xx" style="background-color: rgba(182, 184, 185, 0.200);">
<div
class="row">
{# <ul class="sports" data-prototype="{{ form_widget(form.vars.prototype)|e('html_attr') }}"></ul> #}
<div class="col-md-3">
{{ form_row(form.vars.prototype.playedAt) }}
</div>
<div class="col-md-3">
{{ form_row(form.vars.prototype.sport) }}
</div>
</div>
<div class="row">
<div class="col-md-3">
{{ form_row(form.vars.prototype.player_1) }}
</div>
<div class="col-md-3">
{{ form_row(form.vars.prototype.player_2) }}
</div>
<div class="col-md-2">
{{ form_row(form.vars.prototype.cote) }}
</div>
<div class="col-md-2">
{{ form_row(form.vars.prototype.resultat) }}
</div>
</div>
<hr>
</div>
{% endblock %}
{% block body %}
{# {{ include('paris/_form.html.twig', {form:form, button: 'Ajouter'}) }} #}
<button type='button' id='btn-add' class='add_sport_link btn btn-primary'>Ajouter paris</button>
{{ form_start(form) }}
<div class="paris">
<div class="row">
<div class="col-md-4">
{{ form_row(form.mise) }}
</div>
<div class="col-md-4">
{{ form_row(form.type) }}
</div>
<div class="col-md-4">
{{ form_row(form.win) }}
</div>
</div>
<div class="col-md-4 matchs">
{% if form.rencontres|length > 0 %}
{% for index in 0..form.rencontres|length - 1 %}
{{ dump(index) }}
{{ form_row(form.rencontres[index]) }}
{% endfor %}
{% else %}
{{ form_row(form.rencontres) }}
{% endif %}
</div>
</div>
{{ form_end(form) }}
{% endblock %}
add (form.rencontres|length == 0):
enter image description here
edit (form.rencontres|length > 0):
enter image description here
Regards
PS: Sorry for my english
EDIT: My controller
#NicoHaase I dont think the problem is in the code.
I want just display my form template when I edit an object as when I add an object
`code
/**
* #Route("/paris/add", name="paris_add")
*/
public function add(Request $request)
{
$em = $this->getDoctrine()->getManager();
$paris = new paris();
$form = $this->createForm(ParisType::class, $paris);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em->persist($paris);
$em->flush();
return $this->redirectToRoute('paris');
}
return $this->render("paris/add.html.twig", [
'form' => $form->createView()
]);
}
/**
* #Route("/paris/{id}", name="paris_edit", requirements={"id":"\d+"})
* #param Paris $paris
* #param Request $request
*/
public function edit(Request $request, Paris $paris)
{
$form = $this->createForm(ParisType::class, $paris);
$form->handleRequest($request);
return $this->render("paris/add.html.twig", [
"paris" => $paris,
"form" => $form->createView(),
]);
}
`

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>

Customize symfony data-prototype for edit form

I know it's a common question but I cannot figure out how to achieve that.
I have a Course entity and a CourseDocument entity.
Course(id, documents, ...)
CourseDocument(id, file, course)
In my Course form:
class CourseType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class, [
'label' => 'course.title',
])
->add('documents', CollectionType::class, array(
'entry_type' => MediaType::class,
'label' => 'course.documents_list',
'entry_options' => array(
'label' => false,
'data_class' => CourseDocument::class,
),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
))
;
}
In the form I want to be able to add as many documents as I want. I set up everything, javascript included. My only issue is the data-prototype that is not what I want.
{% import "macros/prototype.html.twig" as prototype %}
{{ form_start(form) }}
<div class="row">
<div class="col-md-6">
<fieldset class="form-group">
{{ form_label(form.documents) }}
<div id="course_documents" class="collection_holder" data-prototype="{{ prototype.tagCollectionItem(form.documents.vars.prototype)|e }}">
{% for widget in form.documents %}
{{ prototype.tagCollectionItem(widget) }}
{% endfor %}
</div>
<button type="button" id="add-document-btn" data-target-collection="#{{ form.documents.vars.id }}" class="btn btn-sm btn-info"><i class="la la-plus"></i> {{ 'course.buttons.add_document' | trans({}, 'labels') }}</button>
</fieldset>
</div>
</div>
{{ form_end(form) }}
I'm generating my prototype with a macro:
{% macro tagCollectionItem(item) %}
<fieldset class="form-group">
<div id="{{ item.vars.id }}">
<div class="custom-file">
{{ form_widget(item.uploadedFile) }}
{{ form_label(item.uploadedFile, item.uploadedFile.vars.label, {'label_attr': {'class': 'custom-file-label'}}) }}
</div>
</div>
</fieldset>
{% endmacro %}
It's working pretty well except for editing. I don't want the input if I already selected a file. But I want the name of the file.
I finally used a macro.
{% macro tagCollectionItem(item) %}
<fieldset class="form-group">
<div id="{{ item.vars.id }}">
{% if item.uploadedFile.vars.file_url or item.uploadedFile.vars.image_url %}
{{ form_errors(item.uploadedFile) }}
{{ form_widget(item.uploadedFile, {'attr': {'hidden': true}}) }}
{% else %}
<div class="custom-file">
{{ form_widget(item.uploadedFile) }}
{{ form_label(item.uploadedFile, item.uploadedFile.vars.label, {'label_attr': {'class': 'custom-file-label'}}) }}
</div>
{% endif %}
</div>
</fieldset>
{% endmacro %}
The twig file for the form
<fieldset class="form-group">
{{ form_label(form.documents) }}
<div id="course_documents" class="collection_holder" data-prototype="{{ prototype.tagCollectionItem(form.documents.vars.prototype)|e }}">
{% for widget in form.documents.children %}
{{ prototype.tagCollectionItem(widget) }}
{% endfor %}
</div>
<button type="button" id="add-document-btn" data-target-collection="#{{ form.documents.vars.id }}" class="btn btn-sm btn-info"><i class="la la-plus"></i> {{ 'course.buttons.add_document' | trans({}, 'labels') }}</button>
</fieldset>

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

Resources