I want to check if a form is new in my template, something like $form->isNew() in symfony 1.4, but i did not find a solution.
Currently, i'm using this technique:
{% null != entity.getId %}
it works, but are there other solutions which are more clean?
I use this:
{% if form.vars.submitted == false %}DO WHATEVER HERE{% endif %}
More info in form functions and variables here:
https://symfony.com/doc/current/reference/forms/twig_reference.html
Symfony2 forms are less coupled to the model than symfony1 forms. Form in Symfony2 cannot be "new" or "old". It's the entity which is persisted, not the form. Forms job is to put submitted data to the model object.
Remember that model class doesn't have to be a doctrine entity (can be really an object of any class). It's up to you to define what does it mean that object is new or not (persisted or not). Again, it's not a form which is persisted.
Btw, you could check for id like this:
{% if entity.id %}{% endif %}
A better solution nowadays (SF3) will be:
{% if form.vars.data.id %}{% endif %}
Related
I have a custom twig template and within it I have a div that I only want to display if the value of another field on the page (weight) is -5.
I know how to show the field if there is a 'value' but im not sure how to write it for a specific value...?
What I have now:
{% if node.field_position_on_page.value %}
<div class="profile-pic">
{{ content.field_profile_picture }}
</div>
{% endif %}
I need it to be something like none.field_position_on_page[-5] but im not sure on the correct way to write that?
Using {% if node.field_position_on_page.value == -5 %} will compare the field to your specific value.
I am using FOSUserBundle with Symfony 3.4
I am trying to reset the user password and this works fine; only problem is that I can put ANY email and the status would be true saying that the email has been sent ??
How is it possible that FOS is finding a user that does not exist ? or did I miss something in my template ?
As far as I understand the status block is what should be displaying the error but it is always a success.
I have not overridden any of the default controller
{% extends 'UserBundle:Resetting:request.html.twig' %}
{% trans_default_domain 'FOSUserBundle' %}
{% block status %}
{{ 'resetting.check_email'|trans({'%tokenLifetime%': tokenLifetime})|nl2br }}
{% endblock %}
I think there is no problem and in addition it is a good behavior. Because otherwise, a person could easily devour a lot of users, which will cause a security problem. So if the user does not exist, not the pain to mention.
Again, if you look closely at this class (in fosuserbundle repository) via the link :
ResettingController, more precisely the method sendEmailAction, at the level of the 2nd control structure if
if (null !== $user && !$user->isPasswordRequestNonExpired($this->retryTtl))
you can notice that if the user does not exist then no mail is sent, the instructions inside the if are not executed and we go directly to a redirection instruction.
return new RedirectResponse($this->generateUrl('fos_user_resetting_check_email', array('username' => $username)));
That's why you see success all the time. Also this redirection above can be executed even in the case the user exists. That's how I tried to have an attempt to understand this process.
Is there a way to check if a current page belongs to a collection? If so, can I check what’s the label of the collection?
Background story:
What I’m trying to achieve is to display a different layout for all pages that belong to a collection. With this new layout I want to display a side navigation (some sort of „See also”) with listing of all pages in the collection that the current site belongs to.
In case my question turns out to be dumb – forgive me, I’m new to jekyll and I really tried to find out the answer by myself.
page.collection returns the label of the collection to which the document belongs. So I guess you want to do something like:
{% if page.collection == 'COLLECTION_LABEL' %}
NEW LAYOUT
{% else %}
OLD LAYOUT
{% endif %}
To access the pages in a collection, you can use the label with site.COLLECTION_LABEL or site[COLLECTION_LABEL] (you need [] if it is a variable). Something like:
{% for page in site[page.collection] %}
{{ page.title }}
{% endfor %}
Any collection document has a page.collection variable which is the collection label.
But, if you want to associate a specific layout to a collection, you can use default configuration :
defaults:
-
scope:
type: mycollection
values:
layout: a_custom_layout
anyvar: a_custom_var
I have setup the following:
{% include '_elements/page-heading.html' with {'shortTitle': 'entry.shortTitle','title':'entry.title'} %}
However, when the page renders this template it is actually outputting "entry.shortTitle" in the page when it should surely actually know what the entry.title is for this page.
e.g. it should say "Latest News" not "entry.title".
Just wondering why this would be?
Thanks!
Enclosing entry.shortTitle with (single) quotes tells Twig that this simply is a string. When you want to pass the value of a variable, you have to leave away the quotes:
{% include '_elements/page-heading.html' with { 'shortTitle': entry.shortTitle, 'title': entry.title } %}
I have an application with Frontend(sales) and Backend(administration), what I want to do is to set the flash messages separated by context, because When I'm login in both context I'm received in the Backend messages that belong to the Frontend, and I don't want it.
Sorry about my English but I'm not an English speaker.
King Regards
If you are using the sessions flash bag then you just need to use different names when adding a message. For example you could use the following for admin messages:
// in your controller
$this->get('session')->getFlashBag()
->add('admin_error','No user found with that email address');
.........................................................
{# in your template #}
{% for flashMessage in app.session.flashbag.get('admin_error') %}
<div class="error-message">{{ flashMessage }}</div>
{% endfor %}
Then do the same thing in your public controllers/templates but replace 'admin_error' with something like 'public_error'.