symfony2: how to make a form field not required on update

user3174311

I have a form with a file field, I'd like to have this field mandatory only when I create the record, not on update. in buildForm I only have this for the field:

->add('file', 'file', array(
            'required'    => false,
        ))

and in the controller I check for the id to decide if insert or update

thanks

Warzyw

In Your form class add:

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setRequired([
        'update',
    ]);
}

Then, when you are creating form use this:

$form = $this->createForm('formName', $object, array(
            'update' => $entity->getId==null?false:true,
       ));

And after that, in your form, in $options array you can use $options['update']. Eg.:

->add('file', 'file', array(
            'required'    => !$options['update'],
        ))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to make field required only if the form is new?

From Dev

Required field in Symfony2

From Dev

Symfony2: form field gets "required"even if is not set as it in form or Twig template

From Dev

How to check whether field exists in symfony2 form?

From Dev

Symfony2 : How to remove form field in listener, after submit

From Dev

How to check whether field exists in symfony2 form?

From Dev

symfony2: How to setting the value of field, outside the form

From Dev

Symfony 2 form field radio required=false?

From Dev

Symfony2 how to make Ajax Request with doctrine update

From Dev

Symfony2 how to make Ajax Request with doctrine update

From Dev

Symfony2 form not validating required fields

From Dev

How to make a field required in Rails?

From Dev

How to define a Symfony2 form (without entity) with at least 1 of 2 fields required?

From Dev

Override form field templates in Symfony2

From Dev

Symfony2 form with field not in entity

From Dev

Is there a way to rename a field in a form in Symfony2?

From Dev

Symfony2 errors on form field and parent

From Dev

Override form field templates in Symfony2

From Dev

Make a field required if another field is checked in Django form

From Dev

update field in DB symfony2

From Dev

Rails 4 How Do You Make 'date_select' Form Field Helper Required?

From Dev

How can I make a "*required field" error message appear for a drop down menu in a validating PHP form?

From Dev

Symfony Dependance required field's form

From Dev

Symfony 2 form conditional required field with fields not mapped to entity

From Dev

Django how to override form validation for a required field

From Dev

how to output field is required in the input form

From Dev

how to update the form field automatically

From Dev

Symfony2: How to filter the options of an entity-choice form field by a certain attribute?

From Dev

How to customize form field based on user roles in Symfony2/3?

Related Related

  1. 1

    How to make field required only if the form is new?

  2. 2

    Required field in Symfony2

  3. 3

    Symfony2: form field gets "required"even if is not set as it in form or Twig template

  4. 4

    How to check whether field exists in symfony2 form?

  5. 5

    Symfony2 : How to remove form field in listener, after submit

  6. 6

    How to check whether field exists in symfony2 form?

  7. 7

    symfony2: How to setting the value of field, outside the form

  8. 8

    Symfony 2 form field radio required=false?

  9. 9

    Symfony2 how to make Ajax Request with doctrine update

  10. 10

    Symfony2 how to make Ajax Request with doctrine update

  11. 11

    Symfony2 form not validating required fields

  12. 12

    How to make a field required in Rails?

  13. 13

    How to define a Symfony2 form (without entity) with at least 1 of 2 fields required?

  14. 14

    Override form field templates in Symfony2

  15. 15

    Symfony2 form with field not in entity

  16. 16

    Is there a way to rename a field in a form in Symfony2?

  17. 17

    Symfony2 errors on form field and parent

  18. 18

    Override form field templates in Symfony2

  19. 19

    Make a field required if another field is checked in Django form

  20. 20

    update field in DB symfony2

  21. 21

    Rails 4 How Do You Make 'date_select' Form Field Helper Required?

  22. 22

    How can I make a "*required field" error message appear for a drop down menu in a validating PHP form?

  23. 23

    Symfony Dependance required field's form

  24. 24

    Symfony 2 form conditional required field with fields not mapped to entity

  25. 25

    Django how to override form validation for a required field

  26. 26

    how to output field is required in the input form

  27. 27

    how to update the form field automatically

  28. 28

    Symfony2: How to filter the options of an entity-choice form field by a certain attribute?

  29. 29

    How to customize form field based on user roles in Symfony2/3?

HotTag

Archive