Symfony 3 + FOS REST Bundle: Normalize values before validation

Dave Maple

I'm working on a FOS REST API. In the underlying models I'd like to be able to define Constraints representing the form appropriate for the datastore, for example, a US Phone Number should be exactly 10 digits.

/**
 * @var string
 *
 * @Assert\NotBlank(message="Phone is required.")
 * @Assert\Regex(message="Exactly 10 digits are required.",  pattern="/^\d{10}$/")
 */
private $phone;

On the other hand I'd like to be able to accept liberal values, for example a phone number formatted as:

{
    "phone": "603-988-6521"
}

The ideal way to implement this would be to have some type of "conversion" or "normalization" phase where select fields could be converted to all digits etc. prior to validation.

What would be the best way to accomplish this in the FOST REST paradigm and Symfony 3?

Dave Maple

It turns out that this is very simple. You can do any type of normalization needed in the actual setters of your model. You just need to configure JMS Serializer to use setters rather than using property reflection. Example with annotations:

/**
 * @var string
 *
 * @JMS\Accessor(getter="getPhone", setter="setPhone")
 * @Assert\Regex(message="Exactly 10 digits are required.",  pattern="/^\d{10}$/")
 */
private $phone;

/**
 * @param string
 */
public function setPhone($phone)
{
    if ($phone === null) {
        $this->phone = null;
        return;
    }

    $this->phone = preg_replace('/[^0-9]/', '', $phone);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Symfony2 FOS Rest bundle routing FileLoaderLoadException controller

From Dev

Symfony2 + FOS Rest Bundle - Regular route

From Dev

Symfony 3 overwrite FOS UserTemplate - but put the Views in my Bundle

From Dev

Symfony 3 - CollectionType of EntityType - Ajax submission (FOS REST)

From Dev

Symfony Password Reset without FOS Bundle

From Dev

Symfony, Fos User Bundle, link to auth/login

From Dev

FOS Rest Bundle not returning proper json

From Dev

FOS rest bundle: unable to find template

From Dev

FOS Rest Bundle not returning proper json

From Dev

Symfony 2 FOS User Bundle Bootstrap modal AJAX Login

From Dev

Symfony: FOS user Bundle promote user via controller

From Dev

Infinite loop repository symfony2 fos user bundle

From Dev

Symfony2 / FOS user bundle - Remember me after registration

From Dev

Symfony: FOS user Bundle promote user via controller

From Dev

Lexik Bundle Symfony 3

From Dev

Symfony2 - Removed FOS User Bundle manually and now cannot clear cache in production mode

From Dev

Symfony2 REST bundle : Filter Parameter

From Dev

Symfony 3 UniqueEntity validation on update

From Dev

Symfony 3: How to create a private bundle

From Dev

Unable to install bundle for Symfony 3 using Composer

From Dev

Sonata User Bundle + Symfony 3.x

From Dev

API Post to Sonata Media Bundle Symfony 3

From Dev

How to auto generate a bundle in Symfony3?

From Dev

Symfony 3 - Sonata Aplication Media Bundle error

From Dev

TYPO3/Extbase - How to trim values before validation/saving objects?

From Dev

Every parent controller must have `get{SINGULAR}Action($id)` method when i have multi level sub resource in FOS Rest Bundle

From Dev

Symfony multiple requests to FOS Routing

From Dev

Add ISBLANK validation before comparing values

From Dev

Symfony 2.7 / 3 - Doctrine: You have requested a non-existent service "fos_user.doctrine_registry"

Related Related

  1. 1

    Symfony2 FOS Rest bundle routing FileLoaderLoadException controller

  2. 2

    Symfony2 + FOS Rest Bundle - Regular route

  3. 3

    Symfony 3 overwrite FOS UserTemplate - but put the Views in my Bundle

  4. 4

    Symfony 3 - CollectionType of EntityType - Ajax submission (FOS REST)

  5. 5

    Symfony Password Reset without FOS Bundle

  6. 6

    Symfony, Fos User Bundle, link to auth/login

  7. 7

    FOS Rest Bundle not returning proper json

  8. 8

    FOS rest bundle: unable to find template

  9. 9

    FOS Rest Bundle not returning proper json

  10. 10

    Symfony 2 FOS User Bundle Bootstrap modal AJAX Login

  11. 11

    Symfony: FOS user Bundle promote user via controller

  12. 12

    Infinite loop repository symfony2 fos user bundle

  13. 13

    Symfony2 / FOS user bundle - Remember me after registration

  14. 14

    Symfony: FOS user Bundle promote user via controller

  15. 15

    Lexik Bundle Symfony 3

  16. 16

    Symfony2 - Removed FOS User Bundle manually and now cannot clear cache in production mode

  17. 17

    Symfony2 REST bundle : Filter Parameter

  18. 18

    Symfony 3 UniqueEntity validation on update

  19. 19

    Symfony 3: How to create a private bundle

  20. 20

    Unable to install bundle for Symfony 3 using Composer

  21. 21

    Sonata User Bundle + Symfony 3.x

  22. 22

    API Post to Sonata Media Bundle Symfony 3

  23. 23

    How to auto generate a bundle in Symfony3?

  24. 24

    Symfony 3 - Sonata Aplication Media Bundle error

  25. 25

    TYPO3/Extbase - How to trim values before validation/saving objects?

  26. 26

    Every parent controller must have `get{SINGULAR}Action($id)` method when i have multi level sub resource in FOS Rest Bundle

  27. 27

    Symfony multiple requests to FOS Routing

  28. 28

    Add ISBLANK validation before comparing values

  29. 29

    Symfony 2.7 / 3 - Doctrine: You have requested a non-existent service "fos_user.doctrine_registry"

HotTag

Archive