symfonydev avatar

Validators in Symfony

symfonydev

Published: 02 Jan 2018 › Updated: 02 Jan 2018Validators in Symfony

Validators in Symfony

http://symfony.com/doc/3.2/components/validator/resources.html

Example of usage of @Assert\File

/**
 * @var \SplFileInfo
 *
 * @Assert\File(
 *     mimeTypes = {"application/x-bittorrent"},
 *     mimeTypesMessage = "Your file was not recognize as a .torrent file.",
 *     maxSize = "100k",
 *     maxSizeMessage = "Your .torrent file was rejected because it is too large.",
 *     disallowEmptyMessage = "The uploaded torrent file seems completely empty.",
 *     uploadIniSizeErrorMessage = "Your torrent file was rejected because it is too big (limited by server). {{ limit }}{{ suffix }} max allowed.",
 *     groups={"default", "torrent_menu"}
 * )
 */
protected $uploadedFileByUrl = null;

source: https://github.com/Juakstlomr/astha/blob/44f330ad5d0ea662cb2a494e6749787cde244727/src/TorrentBundle/Entity/CanBeUploadedTrait.php

and official docs: https://symfony.com/doc/3.2/reference/constraints/File.html#maxsize

validation groups example:
https://symfony.com/doc/3.2/form/data_based_validation.html

When one want a different validation for the same form (different for when the "Product" is first created, and different when it is edited, but we're using here the same form)
"Suppose now, that you don’t want the user to be able to change the name value once the object has been created. To do this, you can rely on Symfony’s Event Dispatcher system to analyze the data on the object and modify the form based on the Product object’s data. In this entry, you’ll learn how to add this level of flexibility to your forms."
http://symfony2-document.readthedocs.io/en/latest/cookbook/form/dynamic_form_generation.html

Leave Validators in Symfony to:

Written by

Read more #forms posts


Best Posts From symfonydev

We have not curated any of symfonydev's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From symfonydev