Conditional validation of fields based on other field value in Symfony2

spiridon

So here is the scenario: I have a radio button group. Based on their value, I should or shouldn't validate other three fields (are they blank, do they contain numbers, etc).

Can I pass all these values to a constraint somehow, and compare them there?

Or a callback directly in the controller is a better way to solve this?

Generally, what is the best practice in this case?

Matteo

I suggest you to use a callback validator.

For example, in your entity class:

<?php

use Symfony\Component\Validator\Constraints as Assert;

/**
 * @Assert\Callback(methods={"myValidation"})
 */
class Setting {
    public function myValidation(ExecutionContextInterface $context)
    {
        if (
                $this->getRadioSelection() == '1' // RADIO SELECT EXAMPLE
                &&
                ( // CHECK OTHER PARAMS
                 $this->getFiled1() == null
                )
            )
        {
            $context->addViolation('mandatory params');
        }
       // put some other validation rule here
    }
}

Otherwise you can build your own custom validator as described here.

Let me know you need more info.

Hope this helps.

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 fill in a field based on conditional subsetting from 2 other fields

From Dev

set field value based on other fields values

From Dev

set field value based on other fields values

From Dev

How to dynamically modify a form field based on 2 other fields in Symfony?

From Dev

Symfony2 validation.yml for value of Array field type

From Dev

Model field based on other fields?

From Java

Conditional field validation based on boolean prop

From Dev

How to do conditional form validation which is based on user submitted data in symfony2?

From Dev

Symfony2 conditional form validation

From Dev

MySQL select calculate 2 fields based on other field

From Dev

How to add conditional schema based on other field?

From Dev

Symfony2 form datetime fields validation

From Dev

Symfony2 Validation against multiple fields

From Dev

Mysql Update field with max value between 2 other fields

From Dev

Conditional Json Schema validation based on property value

From Dev

Validation Symfony2 Entity Choice Field

From Dev

Model - field based on values in other fields

From Dev

Updating a field based on other fields being changed

From Dev

rails presence conditional validation between 2 fields

From Dev

rails presence conditional validation between 2 fields

From Dev

Conditional Formatting on column based on other column value

From Dev

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

From Dev

Jackson Conditional Validation of a field

From Dev

Symfony2 choice field : empty value

From Dev

Symfony2: Conditional bundle configuration sections with required fields

From Dev

JSON Schema conditional: field is required based on value of another field

From Dev

Conditional field requirement based on another field value in Jackson?

From Dev

Conditional field requirement based on another field value in Jackson?

From Dev

SQL: Conditional Field output based on another field's value

Related Related

  1. 1

    How to fill in a field based on conditional subsetting from 2 other fields

  2. 2

    set field value based on other fields values

  3. 3

    set field value based on other fields values

  4. 4

    How to dynamically modify a form field based on 2 other fields in Symfony?

  5. 5

    Symfony2 validation.yml for value of Array field type

  6. 6

    Model field based on other fields?

  7. 7

    Conditional field validation based on boolean prop

  8. 8

    How to do conditional form validation which is based on user submitted data in symfony2?

  9. 9

    Symfony2 conditional form validation

  10. 10

    MySQL select calculate 2 fields based on other field

  11. 11

    How to add conditional schema based on other field?

  12. 12

    Symfony2 form datetime fields validation

  13. 13

    Symfony2 Validation against multiple fields

  14. 14

    Mysql Update field with max value between 2 other fields

  15. 15

    Conditional Json Schema validation based on property value

  16. 16

    Validation Symfony2 Entity Choice Field

  17. 17

    Model - field based on values in other fields

  18. 18

    Updating a field based on other fields being changed

  19. 19

    rails presence conditional validation between 2 fields

  20. 20

    rails presence conditional validation between 2 fields

  21. 21

    Conditional Formatting on column based on other column value

  22. 22

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

  23. 23

    Jackson Conditional Validation of a field

  24. 24

    Symfony2 choice field : empty value

  25. 25

    Symfony2: Conditional bundle configuration sections with required fields

  26. 26

    JSON Schema conditional: field is required based on value of another field

  27. 27

    Conditional field requirement based on another field value in Jackson?

  28. 28

    Conditional field requirement based on another field value in Jackson?

  29. 29

    SQL: Conditional Field output based on another field's value

HotTag

Archive