Uncategorized

Custom Validators on Validation Groups in Symfony2.

If you are using validation groups and have created a custom validator, you will want to use it in your validation group, however its a bit different from how you would apply validators in the entity, and quite similar in-fact to how you define services.

Its similar to how you define services because you have 2 root nodes, first somewhat like services.yml’s parameters line, in this instance called ‘namespaces’, the second part matches the entities name which the validator will be applied to.

Starting out, your validator.yml file probably looks like this:

CCDNComponent\AttachmentBundle\Entity\Attachment:
    properties:
        description: 
            - Type: { type: string, groups: [upload] }
            - MaxLength: { limit: 1000, groups: [upload] }
        attachment:
            - NotNull: { groups: [upload] }
            - File: { groups: [upload] }

This is an example taken from my AttachmentBundle, which you can find here.

Now, when we have a custom validator, in this case, some validators to check a users quota on file uploads etc, we need to set a namespace for our custom validators, with the namespace parameter, and then reference that namespace below followed by our custom validator.

namespaces:
    CCDNComponentAttachmentBundle: CCDNComponent\AttachmentBundle\Form\Validator\Constraints\

CCDNComponent\AttachmentBundle\Entity\Attachment:
    properties:
        description: 
            - Type: { type: string, groups: [upload] }
            - MaxLength: { limit: 1000, groups: [upload] }
        attachment:
            - NotNull: { groups: [upload] }
            - File: { groups: [upload] }
            - "CCDNComponentAttachmentBundle:UploadQuotaFileSize": { groups: [upload] }
            - "CCDNComponentAttachmentBundle:UploadQuotaDiskSpace": { groups: [upload] }
            - "CCDNComponentAttachmentBundle:UploadQuotaFileQuantity": { groups: [upload] }

The name of the validator must be wrapped in double quotes so that Symfony2 is aware that this is using a custom validator, inside of which we first start the namespace, and secondly the validator alias.

We define the alias of our validator in our services.yml like so:

Parameters:
    ccdn_component_attachment.validator.upload_quota_disk_space.class:      CCDNComponent\AttachmentBundle\Form\Validator\UploadQuotaDiskSpaceValidator
    ccdn_component_attachment.validator.upload_quota_file_quantity.class:   CCDNComponent\AttachmentBundle\Form\Validator\UploadQuotaFileQuantityValidator
    ccdn_component_attachment.validator.upload_quota_file_size.class:       CCDNComponent\AttachmentBundle\Form\Validator\UploadQuotaFileSizeValidator

Services:
    ccdn_component_attachment.validator.upload_quota_disk_space:
        class: %ccdn_component_attachment.validator.upload_quota_disk_space.class%
        arguments: [@doctrine, @service_container]
        tags:
            - { name: validator.constraint_validator, alias: upload_quota_disk_space }
    ccdn_component_attachment.validator.upload_quota_file_quantity:
        class: %ccdn_component_attachment.validator.upload_quota_file_quantity.class%
        arguments: [@doctrine, @service_container]
        tags:
            - { name: validator.constraint_validator, alias: upload_quota_file_quantity }
    ccdn_component_attachment.validator.upload_quota_file_size:
        class: %ccdn_component_attachment.validator.upload_quota_file_size.class%
        arguments: [@doctrine, @service_container]
        tags:
            - { name: validator.constraint_validator, alias: upload_quota_file_size }