skonsoft
skonsoft

Reputation: 1806

symfony2: how to check if form is new?

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?

Upvotes: 3

Views: 7680

Answers (3)

KungFuMonkey
KungFuMonkey

Reputation: 101

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

Upvotes: 6

Wesley Abbenhuis
Wesley Abbenhuis

Reputation: 687

A better solution nowadays (SF3) will be:

{% if form.vars.data.id %}{% endif %}

Upvotes: 3

Jakub Zalas
Jakub Zalas

Reputation: 36211

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

Upvotes: 3

Related Questions