Validators in Symfony

in #forms7 years ago

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

Coin Marketplace

STEEM 0.18
TRX 0.14
JST 0.029
BTC 57800.38
ETH 3127.30
USDT 1.00
SBD 2.40