Call JavaScript function from Twig (symfony 5) - symfony

I'm working on a Symfony5 project and I want to call a JavaScript function and pass an Ajax Request (which I get from a controller) from a Twig.
i create a function phoneverif() in order to do some verification before the validation of form ..like doing a test if the fields are blanks then an alert will appears also the verification of the existence of data in db.
But a first, very simple test already failed, and an error appear :
Uncaught ReferenceError: phoneverif is not defined
onclick http://localhost:8000/super_admin/telephone/nouveau:1
here is my twig code :
{% extends 'superadmin.html.twig'%}
{% block body %}
<div class="container-fluid">
<!-- Page Heading -->
<h1 class="h3 mb-4 text-gray-800">Nouveau téléphone</h1>
</div>
<div class="col-md-12">
{% for flashMessage in app.session.flashbag.get('notice') %}
<div class="alert alert-success alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa fa-check"></i>{{ flashMessage }}</h4>
</div>
{% endfor %}
<!-- general form elements -->
<div>
<!-- form start -->
{{ form_start(form)}}
<div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>{{ form_label(form.userGroup) }}</label>
{{ form_widget(form.userGroup) }}
<span class="text-danger">{{ form_errors(form.userGroup) }}</span>
</div>
<div class="form-group">
<label>{{ form_label(form.protocol) }}</label>
{{ form_widget(form.protocol) }}
<span class="text-danger">{{ form_errors(form.protocol) }}</span>
</div>
<div class="form-group">
<label>{{ form_label(form.extension) }}</label>
{{ form_widget(form.extension) }}
<span class="text-danger">{{ form_errors(form.extension) }}</span>
</div>
<div class="form-group">
<label>{{ form_label(form.dialplanNumber) }}</label>
{{ form_widget(form.dialplanNumber) }}
<span class="text-danger">{{ form_errors(form.dialplanNumber) }}</span>
</div>
<div class="form-group">
<label>{{ form_label(form.voicemailId) }}</label>
{{ form_widget(form.voicemailId) }}
<span class="text-danger">{{ form_errors(form.voicemailId) }}</span>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>{{ form_label(form.login) }}</label>
{{ form_widget(form.login) }}
<span class="text-danger">{{ form_errors(form.login) }}</span>
</div>
<div class="form-group">
<label>{{ form_label(form.pass) }}</label>
{{ form_widget(form.pass) }}
<span class="text-danger">{{ form_errors(form.pass) }}</span>
</div>
<div class="form-group">
<label>{{ form_label(form.confSecret) }}</label>
{{ form_widget(form.confSecret) }}
<span class="text-danger">{{ form_errors(form.confSecret) }}</span>
</div>
<div class="form-group">
<label>{{ form_label(form.serverIp) }}</label>
{{ form_widget(form.serverIp) }}
<span class="text-danger">{{ form_errors(form.serverIp) }}</span>
</div>
</div>
</div>
</div>
<div>
<button type="button" class="btn btn-primary" onclick="phoneverif();">Ajouter</button>
<a class="btn btn-warning" href="{{ path('Les_telephones') }}">Retour</a>
</div>
{{ form_end(form)}}
</div>
</div>
</div>
{% endblock %}
{% block javascripts %}
<script src="{{ asset('bundles/fosjsrouting/js/router.js') }}"></script>
<script src="{{ path('fos_js_routing_js', { callback: 'fos.Router.setData' }) }}"></script>
<script type="text/javascript">
function phoneverif() {
if ($("#telephone_extension").val() == '') {
alert('Vous devez saisir une extension');
} else {
if ($("#telephone_dialplanNumber").val() == '') {
alert('Vous devez saisir le dialplan du téléphone');
} else {
if ($("#telephone_login").val() == '') {
alert('Vous devez saisir le login du téléphone');
} else {
if ($("#telephone_pass").val() == '') {
alert('Vous devez saisir le mot de passe du téléphone');
} else {
$.ajax({
type: 'get',
url: Routing.generate('phone_verif', {extension: $("#telephone_extension").val()}),
success: function (data) {
if (data.existe == 1) {
alert('Téléphone non valide');
} else {
$("form").submit();
}
},
error: function() { alert('Erreur lors de l'appel AJAX'); }
});
}
}
}
}
}
</script>
{% endblock %}
function verifphone :
public function Phoneverif1(Request $request , $extension){
if ($request->isXmlHttpRequest()) {
$em = $this->getDoctrine()->getManager();
$phone = $em->getRepository(Phones::class)->findOneByExtension($extension);
if ($phone) {
$existe = 1;
} else {
$existe = 0;
}
$response = new JsonResponse();
return $response->setData(array('existe' => $existe));
} else {
throw new \Exception('Erreur');
}
}
my fosjsrouting configuration :
config/packages/routing.yaml:
framework:
router:
utf8: true
# Configure how to generate URLs in non-HTTP contexts, such as CLI commands.
# See https://symfony.com/doc/current/routing.html#generating-urls-in-commands
#default_uri: http://localhost
fos_js_routing:
routes_to_expose: [ phone_verif ]
config/routes/fos_js_routing.yaml:
fos_js_routing:
resource: "#FOSJsRoutingBundle/Resources/config/routing/routing.xml"
routes.yaml :
verification_telephone:
path: /verification/phone/{extension}
controller: App\Controller\TelephoneController::Phoneverif1
options:
expose: true

Rename the function and try again
I solved this problem by renaming the function. You try too.
Phoneverif1()
{% extends 'superadmin.html.twig'%}
{% block body %}
<div class="container-fluid">
<!-- Page Heading -->
<h1 class="h3 mb-4 text-gray-800">Nouveau téléphone</h1>
</div>
<div class="col-md-12">
{% for flashMessage in app.session.flashbag.get('notice') %}
<div class="alert alert-success alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa fa-check"></i>{{ flashMessage }}</h4>
</div>
{% endfor %}
<!-- general form elements -->
<div>
<!-- form start -->
{{ form_start(form)}}
<div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>{{ form_label(form.userGroup) }}</label>
{{ form_widget(form.userGroup) }}
<span class="text-danger">{{ form_errors(form.userGroup) }}</span>
</div>
<div class="form-group">
<label>{{ form_label(form.protocol) }}</label>
{{ form_widget(form.protocol) }}
<span class="text-danger">{{ form_errors(form.protocol) }}</span>
</div>
<div class="form-group">
<label>{{ form_label(form.extension) }}</label>
{{ form_widget(form.extension) }}
<span class="text-danger">{{ form_errors(form.extension) }}</span>
</div>
<div class="form-group">
<label>{{ form_label(form.dialplanNumber) }}</label>
{{ form_widget(form.dialplanNumber) }}
<span class="text-danger">{{ form_errors(form.dialplanNumber) }}</span>
</div>
<div class="form-group">
<label>{{ form_label(form.voicemailId) }}</label>
{{ form_widget(form.voicemailId) }}
<span class="text-danger">{{ form_errors(form.voicemailId) }}</span>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>{{ form_label(form.login) }}</label>
{{ form_widget(form.login) }}
<span class="text-danger">{{ form_errors(form.login) }}</span>
</div>
<div class="form-group">
<label>{{ form_label(form.pass) }}</label>
{{ form_widget(form.pass) }}
<span class="text-danger">{{ form_errors(form.pass) }}</span>
</div>
<div class="form-group">
<label>{{ form_label(form.confSecret) }}</label>
{{ form_widget(form.confSecret) }}
<span class="text-danger">{{ form_errors(form.confSecret) }}</span>
</div>
<div class="form-group">
<label>{{ form_label(form.serverIp) }}</label>
{{ form_widget(form.serverIp) }}
<span class="text-danger">{{ form_errors(form.serverIp) }}</span>
</div>
</div>
</div>
</div>
<div>
<button type="button" class="btn btn-primary" onclick="Phoneverif1()">Ajouter</button>
<a class="btn btn-warning" href="{{ path('Les_telephones') }}">Retour</a>
</div>
{{ form_end(form)}}
</div>
</div>
</div>
{% endblock %}
{% block javascripts %}
<script src="{{ asset('bundles/fosjsrouting/js/router.js') }}"></script>
<script src="{{ path('fos_js_routing_js', { callback: 'fos.Router.setData' }) }}"></script>
<script type="text/javascript">
function Phoneverif1() {
if ($("#telephone_extension").val() == '') {
alert('Vous devez saisir une extension');
} else {
if ($("#telephone_dialplanNumber").val() == '') {
alert('Vous devez saisir le dialplan du téléphone');
} else {
if ($("#telephone_login").val() == '') {
alert('Vous devez saisir le login du téléphone');
} else {
if ($("#telephone_pass").val() == '') {
alert('Vous devez saisir le mot de passe du téléphone');
} else {
$.ajax({
type: 'get',
url: Routing.generate('phone_verif', {extension: $("#telephone_extension").val()}),
success: function (data) {
if (data.existe == 1) {
alert('Téléphone non valide');
} else {
$("form").submit();
}
},
error: function() { alert('Erreur lors de l'appel AJAX'); }
});
}
}
}
}
}
</script>
{% endblock %}

Related

Arranging photos in a vertical position in CommerceHQ platform

I'm having some problem with my theme for my e-commerce store. Currently, the product images are aligned in a carousel form. (One large main image, 3-5 thumbnails images below in carousel form)
I'd like to change this to show all photos in same sizes but vertically (top to bottom)
This is my code, how do I change it? I've read stuff online and tried making changes but nothing is happening. Please help.
{{ set(this, 'title', product.seo_title) }}
{% set mainStyles = load.get( loadQuery.cms_items("main-styles", {limit: 50}) ) %}
{% set buttonText = mainStyles[0].settings["Add to cart button text"]|default('Add to Cart') %}
{% set buttonColor = mainStyles[0].settings["Add to cart button color"]|default('inherit') %}
{% set maxRelatedProducts = mainStyles[0].settings["Number of rows for related items"]|default(1) * 3 %}
{% set quanSelector = mainStyles[0].settings["Quantity selector on product page"] %}
{% set imageCrop = mainStyles[0].settings["Product thumbnail aspect ratio"]|default('Smart crop') %}
{% if imageCrop == 'Wide' %}
{% set imageCrop = 'wide' %}
{% elseif imageCrop == 'Tall' %}
{% set imageCrop = 'tall' %}
{% endif %}
<div class="page product" data-productid="{{ product.id }}">
<div class="container">
<div class="col-sm-7">
<div class="fotorama"
data-width="100%"
data-arrows="false"
data-loop="true"
data-autoplay="false"
data-thumbwidth="113"
data-thumbheight="113"
data-thumbborderwidth="4"
data-thumbmargin="5">
{% if product.mainImage %}
<a href="{{ product.mainImage.getImageSize("jumbo") }}">
<img src="{{ product.mainImage.getImageSize("jumbo") }}">
</a>
{% endif %}
</div>
<div class="product-no-image{% if product.mainImage %} hidden{% endif %}">
<img src="/img/icons/image.svg">
</div>
<div class="hidden-xs">
<hr>
{% set relatedProducts = campaign.recommendations({ "product_id": product.id, "limit": maxRelatedProducts }) %}
{% if relatedProducts|length %}
<div class="row related items-grid">
<div class="col-sm-12">
<div class="block-title">
<h3>Related products</h3>
</div>
<div class="items">
{% for product in relatedProducts %}
<div class="col-sm-4 col-xs-6 item {{ imageCrop }}">
<a href="{{ site_path(product.url) }}">
{% if product.mainImage %}
<div class="image"
style="background-image: url('{{ product.mainImage.getImageSize("medium") }}');"></div>
{% else %}
<div class="image empty"></div>
{% endif %}
<p class="title">{{ product.title }}</p>
<p class="price">
{{ product.defaultPrice | currency }}
</p>
</a>
</div>
{% endfor %}
</div>
</div>
</div>
{% endif %}
</div>
</div>
<div class="col-sm-5">
<div class="product-head">
<h2>{{ product.title }}</h2>
<p class="price">
<span id="price">{{ product.price | currency }}</span>
<span class="compare">
<span id="compare-price">{{ product.compare_price | currency(NULL, {"force_prefix_symbol": true}) }}</span>
</span>
</p>
{% if product.variants %}
<div class="variants">
{% for variant in product.allVariants %}
<div class="item">
<span class="label-heading">{{ variant.variant }}</span>
{% if variant.selectorType == "dropdown" %}
<div class="select2">
<select data-variant="{{ loop.index0 }}"
class="product-{{ variant.variant|preg_replace('/[^a-zA-Z0-9]/')|lower }}-select product-select"
title="">
</select>
</div>
{% else %}
<div class="product-colors product-{{ variant.variant|preg_replace('/[^a-zA-Z0-9]/')|lower }}-round">
{% for option in variant.variantOptions %}
<button type="button"
data-variant="{{ loop.parent.loop.index0 }}"
data-option="{{ option.option|preg_replace('/[^a-zA-Z0-9]/')|lower }}"
class="btn-rounded product-color"
style="{% if option.thumbnail.image %}background-image: url('{{ option.thumbnail.imageUrl }}');{% else %}background-color: {{ option.thumbnail.color }}{% endif %}"></button>
{% endfor %}
</div>
{% endif %}
</div>
{% endfor %}
</div>
{% endif %}
{% if quanSelector %}
<div id="quantity-selector" class="quantity">
<span class="label-heading">Quantity</span>
<input type="number">
<button class="down"></button>
<button class="up"></button>
</div>
{% endif %}
{{ system.getApp('ProductCustomizer', {'product_id': product.id} ) }}
<button class="btn" id="buy-it-now"
style="background-color: {{ buttonColor }}">{{ buttonText }}</button>
{{ system.getApp('SecurityBadge', {} ) }}
{{ system.getApp('QuantityLeft', {'product_id': product.id} ) }}
{{ system.getApp('Timer', {'product_id': product.id}) }}
</div>
{% if product.textareaModels %}
<div class="panel-group descriptions" id="accordion" role="tablist" aria-multiselectable="true">
{% for textarea in product.textareaModels %}
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="section-heading{{ loop.index }}">
<h4 class="panel-title">
<a role="button" data-toggle="collapse" data-parent="#accordion"
href="#section{{ loop.index }}"
aria-expanded="{% if loop.index == 1 %}true{% else %}false{% endif %}"
aria-controls="section{{ loop.index }}">
{{ textarea.name|raw }}
<span class="icon__state"></span>
</a>
</h4>
</div>
<div id="section{{ loop.index }}"
class="panel-collapse collapse{% if loop.index == 1 %} in{% endif %}" role="tabpanel"
aria-labelledby="section-heading{{ loop.index }}">
<div class="panel-body">
<div class="typography">{{ textarea.text | raw }}</div>
</div>
</div>
</div>
{% endfor %}
</div>
{% endif %}
</div>
<div class="visible-xs">
{% set relatedProducts = campaign.recommendations({ "product_id": product.id, "limit": maxRelatedProducts }) %}
{% if relatedProducts|length %}
<div class="row related items-grid">
<div class="col-sm-12">
<div class="block-title">
<h3>Related products</h3>
</div>
<div class="items">
{% for product in relatedProducts %}
<div class="col-sm-4 col-xs-6 item {{ imageCrop }}">
<a href="{{ site_path(product.url) }}">
{% if product.mainImage %}
<div class="image"
style="background-image: url('{{ product.mainImage.getImageSize("medium") }}');"></div>
{% else %}
<div class="image empty"></div>
{% endif %}
<p class="title">{{ product.title }}</p>
<p class="price">
{{ product.defaultPrice | currency }}
</p>
</a>
</div>
{% endfor %}
</div>
</div>
</div>
{% endif %}
</div>
<div class="row">
<div class="col-sm-12">
{{ system.getApp('Reviews', {'product_id': product.id} ) }}
</div>
</div>
</div>
</div>

Adding class to images and videos with Summernote widget in Django 2

I'm looking for a way to add a class, which I could modify in css, to images and videos in both editor forms and html templates displaying the form. I'm using the SummernoteInplaceWidget() from django-summernote package and it's working fine except this feature.
I use bootstrap 4 with it.
Any ideas?
Thanks in advance.
forms.py:
from django.forms import ModelForm
from django_summernote.widgets import SummernoteInplaceWidget
from .models import Post
class PostFormModel(ModelForm):
class Meta:
model = Post
fields = ['title', 'content', 'image', ]
widgets = {
'content': SummernoteInplaceWidget(attrs={'class': 'summer-img-class'}),
}
post_form.html:
{% extends "blog/base.html" %}
{% load crispy_forms_tags %}
{% block mt %} mt-2 {% endblock mt %}
{% block content %}
<div class="content-section">
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Blog Post</legend>
{{ form|crispy }}
{% if form.image.errors %}
<div class="alert alert-danger">
{{ form.image.errors }}
</div>
{% endif %}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Post</button>
</div>
</form>
{% block jquery %}
{{ form.media }}
{% endblock %}
</div>
{% endblock content %}
post_detail.html:
{% extends "blog/base.html" %}
{% block mt %} mt-2 {% endblock mt %}
{% block content %}
<article class="media content-section">
<div class="shadow-sm rounded row media-body">
<div class="col-md-3 col-sm-3 col-3 align-self-center">
<img class="rounded-circle article-img align-middle mx-auto d-none d-sm-block " src="{{ object.author.profile.image.url }}">
<img class="rounded-circle article-img-sm align-midle mx-auto d-block d-sm-none " src="{{ object.author.profile.image.url }}"">
</div>
<div class="col-md-9 col-sm-9 col-9">
<div class="article-metadata">
<a class="mr-2" href="{% url 'user-posts' post.author.username %}">{{ post.author }}</a>
<small class="text-muted">{{ object.date_posted|date:"F d, Y" }}</small>
{% if object.author == user %}
<div>
<a class="btn btn-secondary btn-sm mt-1 mb-1" href="{% url 'post-update' object.id %}">Update</a>
<a class="btn btn-danger btn-sm mt-1 mb-1" href="{% url 'post-delete' object.id %}">Delete</a>
</div>
{% endif %}
</div>
<h2><a class="article-title" href="{% url 'post-detail' post.id %}">{{ object.title }}</a></h2>
</div>
<div class="row media-body pt-3">
<div class="col-md-12">
{% if post.image %}
<img alt="" src="{{ object.image.large.url }}" class="post_image rounded mx-auto mb-2 d-block"/>
{% endif %}
<p class="article-content ml-3">{{ object.content|safe }}</p>
</div>
</div>
</div>
</article>
{% endblock content %}
I tried to add some styling to .article-content, it didn't have any effect, the same for this class in css (I imagine it can be more effectively):
.summer-img-class * {
max-width: 100%;
max-height: 400px;
object-fit: cover;
margin: auto;
}
Maybe it should be done with Jquery somehow, I don't know.

Edit Path in my twig file

I have a variable extracted in my Twig file which is the result of a selected :
<div class="col-sm-7">
<div class="product-information"><!--/product-information-->
<img src="images/product-details/new.jpg" class="newarrival" alt="" />
<h2>{{ produit.nom }}</h2>
<p>{{ produit.categorie.nomcat }}</p>
<p>{{ produit.description }}</p>
<span>
<span>€ {{ produit.prix}}</span>
</span>
<p>
<script>
function myFunction() {
var x = document.getElementById("co").value;
document.getElementById("demo").innerHTML = x;
}
</script>
<b>Couleur :</b>
<select id="co" onchange="myFunction()">
{% for coul in produit.couleur %}
<option value="{{ coul.nomc }}">{{ coul.nomc }}</option>
{% endfor %}
</select>
</p>
<p><b>Marque :</b> {{ produit.marque}}</p>
<p>
<a href="{{ path('ajouter', { 'id' : produit.id }) }}">
<button type="button" class="btn btn-fefault cart" >
<i class="fa fa-shopping-cart"></i>
Ajouter au Panier
</button>
</a>
</p>
</div><!--/product-information-->
i try to extract the selected item from the select by put it in a variable,
somthing like that : {% set var %} <p id=demo> </p>
when i put the variable {{ var }} i get the selected item in my screen
but when i put it in the path i get the problem :
the path is somthing like that : ../../../id/ /
he put the balise p not the value of my variable
I have tried : <a href="{{ path('ajouter', { 'id' : produit.id, 'coul' : {{ var }} }) }}">
you can use FOSJsRoutingBundle to generate routes on client side (right in JS)

Twig {% if statement %} using Symfony 2

How can I access an entity attribute in Twig (using the Winzou Symfony2 tutorial on OpenClassroom)?
Category is an attribute of my class Advert which contains all my adverts. I just want to show an error message if there are no Advert entities.
Here is my accordion that lists "adverts" from my Advert entity. One accordion is for the category incident and the one other is for the category general.
<div class="well">
<div class="accordion" id="accordion2">
<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseGen">
<h3><i class="icon-chevron-right"></i> Incidents</h3>
</a>
</div>
<div id="collapseGen" class="accordion-body collapse">
<div class="accordion-inner">
<div class="accordion" id="accordion4">
{% for advert in listAdverts %}
{% if advert.category == "incident" %}
<div class="accordion-group">
<div class="accordion-heading decalage">
<a href="{{ path('info_view', {'id': advert.id}) }}">
<h3 class="{{ advert.category }}">{{ advert.title }}</h3>
</a>
<div>
<span>{{ advert.content|truncate(100, true, '...')|raw }}</span>
</div>
<div>
{% if (advert.UpdatedAt is empty) %}
<i>Créé par {{ advert.author }}, le {{ advert.date|date('d/m/Y') }}
à {{ advert.date|date('H:i') }}</i>
{% else %}
<i>Modifié par {{ advert.author }},
le {{ advert.updatedAt|date('d/m/Y') }}
à {{ advert.updatedAt|date('H:i') }}</i>
{% endif %}
</div>
<div>{% if advert.readers is not empty %}
<i>Vu par :
{% for reader in advert.readers %}
{{ reader.username }},
{% endfor %}
{% endif %}</i>
</div>
<br>
<div>
{% if is_granted("IS_AUTHENTICATED_FULLY") %}
<p>
<a href="{{ path('info_edit', {'id': advert.id}) }}"
class="btn btn-default">
<i class="fa fa-pencil-square-o" aria-hidden="true"></i>
Modifier
</a>
<a href="#myModal{{ advert.id }}" role="button" class="btn btn-danger"
data-toggle="modal">
<i class="fa fa-trash-o" aria-hidden="true"></i>
Supprimer
</a>
</p>
<div id="myModal{{ advert.id }}" class="modal hide fade">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"
aria-hidden="true">×</button>
<h3>Suppression annonce "{{ advert.title }}"</h3>
</div>
<div class="modal-body">
<p>Voulez-vous vraiment supprimer l'annonce : "{{ advert.title }}" ? </p>
</div>
<div class="modal-footer parente">
<div>Annuler</div>
<div>
<form class="nomargin"
action="{{ path('info_delete', {'id': advert.id}) }}"
method="post">
<input type="submit" value="Supprimer"
class="btn btn-danger"/>{{ form_rest(form) }}
</form>
</div>
</div>
</div>
{% endif %}
</div>
</div>
</div>
{# HERE I DONT KNOW HOW TO DO IT #}
{% else %} No Advert !
{% endif %}
{% endfor %}
</div>
</div>
</div>
</div>
<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseCo">
<h3><i class="icon-chevron-right"></i> Général</h3>
</a>
</div>
<div id="collapseCo" class="accordion-body collapse">
<div class="accordion-inner">
<div class="accordion" id="accordion4">
{% for advert in listAdverts %}
{% if advert.category == "general" %}
<div class="accordion-group">
<div class="accordion-heading decalage">
<div class="accordion-heading">
<a href="{{ path('info_view', {'id': advert.id}) }}">
<h3 class="{{ advert.category }}">{{ advert.title }}</h3>
</a>
<span>{{ advert.content|truncate(100, true, '...')|raw }}</span>
<div>
{% if (advert.UpdatedAt is empty) %}
<i>Créé par {{ advert.author }}, le {{ advert.date|date('d/m/Y') }}
à {{ advert.date|date('H:i') }}</i>
{% else %}
<i>Modifié par {{ advert.author }},
le {{ advert.updatedAt|date('d/m/Y') }}
à {{ advert.updatedAt|date('H:i') }}</i>
{% endif %}
</div>
<div>{% if advert.readers is not empty %}
<i>Vu par :
{% for reader in advert.readers %}
{{ reader.username }},
{% endfor %}
{% endif %}</i>
</div>
<br>
<div>
{% if is_granted("IS_AUTHENTICATED_FULLY") %}
<p>
<a href="{{ path('info_edit', {'id': advert.id}) }}"
class="btn btn-default">
<i class="fa fa-pencil-square-o" aria-hidden="true"></i>
Modifier
</a>
<a href="#myModal{{ advert.id }}" role="button" class="btn btn-danger"
data-toggle="modal">
<i class="fa fa-trash-o" aria-hidden="true"></i>
Supprimer
</a>
</p>
<div id="myModal{{ advert.id }}" class="modal hide fade">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"
aria-hidden="true">×</button>
<h3>Suppression annonce "{{ advert.title }}"</h3>
</div>
<div class="modal-body">
<p>Voulez-vous vraiment supprimer l'annonce : "{{ advert.title }}" ? </p>
</div>
<div class="modal-footer parente">
<div>Annuler</div>
<div>
<form class="nomargin"
action="{{ path('info_delete', {'id': advert.id}) }}"
method="post">
<input type="submit" value="Supprimer"
class="btn btn-danger"/>{{ form_rest(form) }}
</form>
</div>
</div>
</div>
{% endif %}
</div>
</div>
</div>
</div>
{% endif %}
{% endfor %}
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
My error message " No Adverts !" doesnt appears..
EDIT : According to Mitchel's answer, i've tried this :
{% endif %}
{% else %}<li>No Adverts</li>
{% endfor %}
I don't know why it doesn't work when one advert is in the other category...
{% for advert in listAdverts if advert.category == "incident" %}
{% if listAdverts is empty%}
<li>No Adverts</li>
{% endif %}
{% endfor %}
That code does not work.
I'm not sure to understand your question, but if so, you can maybe try a thing like this according to the documentation:
{% for user in users %}
<li>{{ user.username|e }}</li>
{% else %}
<li><em>no user found</em></li>
{% endfor %}
In Twig a for statement can have an associated else. It basically means that if the for has no results, the else is used. You should put the {% endif %} before the {% else %} and it should work.

Unable to render the form because the block names array contains duplicates

i'm using Symfony 3.3.9 and when I'm trying to render a form I have the following error:
An exception has been thrown during the rendering of a template
("Unable to render the form because the block names array contains
duplicates: "_fos_user_registration_form_errors", "user_errors",
"user_errors", "fos_user_registration_errors", "form_errors".").
Thank you in advance for your help !
EDIT 17/09/2017
Here you go:
public function indexAction()
{
/** #var $formFactory FactoryInterface */
$formFactory = $this->get('fos_user.registration.form.factory');
$form = $formFactory->createForm();
return $this->render('AppTMMainBundle:Default:index.html.twig', array(
'form' => $form->createView(),
));
}
My Twig:
{{ form_start(form, {'method': 'post', 'action': path('fos_user_registration_register')}) }}
<div class="card-content">
<h3 class="text-center title" style="color: #3C4858;margin:10px 0;">Inscription</h3>
<div class="social text-center">
<a class="btn btn-just-icon btn-round btn-facebook" href="{{ path('hwi_oauth_service_redirect',{'service': 'facebook'}) }}">
<i class="fa fa-facebook"> </i>
</a>
<a class="btn btn-just-icon btn-round btn-twitter" href="{{ path('hwi_oauth_service_redirect',{'service': 'twitter'}) }}">
<i class="fa fa-twitter"></i>
</a>
<a class="btn btn-just-icon btn-round btn-google" href="{{ path('hwi_oauth_service_redirect',{'service': 'google'}) }}">
<i class="fa fa-google-plus"></i>
</a>
</div>
<p class="description text-center">Ou de façon plus classique</p>
<div class="row">
<div class="col-xs-6">
{{ form_label(form.firstname) }}
{{ form_widget(form.firstname) }}
</div>
<div class="col-xs-6">
{{ form_label(form.lastname) }}
{{ form_widget(form.lastname) }}
</div>
</div>
<div class="row">
<div class="col-xs-12">
{{ form_label(form.email) }}
{{ form_widget(form.email) }}
</div>
</div>
<div class="row">
<div class="col-xs-6">
{{ form_widget(form.plainPassword.first, {'attr': {'class': 'form-control', 'placeholder': 'form.password'}}) }}
</div>
<div class="col-xs-6">
{{ form_widget(form.plainPassword.second, {'attr': {'class': 'form-control', 'placeholder': 'form.password_confirmation'}}) }}
</div>
</div>
<!-- If you want to add a checkbox to this form, uncomment this code -->
<div class="input-group">
<div class="checkbox">
<label>
<input type="checkbox" name="optionsCheckboxes" checked="">
{{ 'index.proceed.agree'|trans }} {{ 'index.proceed.cgu'|trans }}.
</label>
</div>
</div>
<div class="footer text-center">
<input type="submit" class="btn btn-primary btn-round" value="{{ 'index.action.subscribe'|trans }}">
</div>
</div>
{{ form_end(form) }}
for anyone having such an exception:
You probably shouldn't extend a form type, but instead create your own and use getParent to inherit the behaviour.
And if your class name is the same as an other type (excluding namespaces)
you must override the getBlockPrefix from AbstractType.
The getBlockPrefix method from AbstractType uses the last part of fqcn (the class name) and you could have a duplicate block name.
I found the answer to my question.
That was from my RegistrationType class where I was trying to extends with another Type Class. getParent not working as well expected...
RegistrationType > UserType > FOS\UserBundle\Form\Type\RegistrationFormType
I move all fields from UserType in RegistrationType and it's working :)

Resources