skonsoft skonsoft - 1 year ago 173
Twig Question

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?

Answer Source

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 %}{% endif %}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download