Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating items inside an array - Symfony 2.1

I'm using the Symfony 2.1 validation module to validate data for my REST API, it works correctly but I'm wondering if there is any easy way to validate items inside an array. For example, one of my fields is an array of dates, I want to ensure that each item inside is correctly formatted date.

I am using YAML as follows to set constraints, array_of_dates is the field I'd like to be able to validate each item inside that array to be a valid date.

# src/Acme/DemoBundle/Resources/config/validation.yml
Acme\DemoBundle\Entity\Demo:
    properties:
        start:
            - NotBlank: ~
            - Date: ~
        end:
            - NotBlank: ~
            - Date: ~
        array_of_dates:
            - Type:
                type: array
like image 682
greg Avatar asked Dec 11 '12 20:12

greg


1 Answers

Apply All constraint validator.

This constraint allows you to apply a collection of constraints to each element of the array

like image 164
smoreno Avatar answered Sep 19 '22 18:09

smoreno