feat: add validations

feat: add forms

feat: add validations

validations

InexController
This commit is contained in:
Ondrej Vlach 2024-01-16 23:00:30 +01:00
parent 248b25576b
commit 63783cb90f
Signed by: ovlach
GPG Key ID: 4FF1A23B4914DE70
6 changed files with 76 additions and 3 deletions

View File

@ -1,6 +1,8 @@
framework: framework:
validation: validation:
email_validation_mode: html5 email_validation_mode: html5
enabled: true
translation_domain: validation_errors
# Enables validator auto-mapping support. # Enables validator auto-mapping support.
# For instance, basic validation constraints will be inferred from Doctrine's metadata. # For instance, basic validation constraints will be inferred from Doctrine's metadata.

View File

@ -2,30 +2,47 @@
declare(strict_types=1); declare(strict_types=1);
namespace App\Entity\QRCode; namespace App\Entity\QRCode;
use Symfony\Component\Validator\Constraints as Assert;
/** /**
* QRCode description * QRCode description
*/ */
class QRCode class QRCode
{ {
#[Assert\NotBlank]
#[Assert\Iban(
message: 'messages.not_a_iban_number',
)]
private ?string $iban; private ?string $iban;
#[Assert\NotBlank(message: 'messages.fill_value')]
#[Assert\GreaterThanOrEqual(
'today', message: 'messages.invalid_date'
)]
private ?\DateTime $dueDate; private ?\DateTime $dueDate;
#[Assert\NotBlank(message: 'messages.fill_value')]
private ?string $message; private ?string $message;
#[Assert\Valid]
#[Assert\NotBlank]
private ?QRCodeMoney $money; private ?QRCodeMoney $money;
#[Assert\Valid]
#[Assert\NotBlank]
private ?QRCodePaymentIdentification $paymentIdentification; private ?QRCodePaymentIdentification $paymentIdentification;
#[Assert\Valid]
#[Assert\NotBlank]
private readonly QRCodeQROptions $codeQROptions; private readonly QRCodeQROptions $codeQROptions;
/** /**
* @param QRCodeQROptions $codeQROptions * @param QRCodeQROptions $codeQROptions
* @param string|null $iban * @param string|null $iban
* @param string|null $dueDate * @param \DateTime|null $dueDate
* @param string|null $message * @param string|null $message
* @param QRCodeMoney|null $money * @param QRCodeMoney|null $money
*/ */
public function __construct( public function __construct(
QRCodeQROptions $codeQROptions, QRCodeQROptions $codeQROptions,
?string $iban = null, ?string $iban = null,
?string $dueDate = null, ?\DateTime $dueDate = null,
?string $message = null, ?string $message = null,
?QRCodeMoney $money = null) ?QRCodeMoney $money = null)
{ {

View File

@ -3,13 +3,22 @@ declare(strict_types=1);
namespace App\Entity\QRCode; namespace App\Entity\QRCode;
use App\Service\CurrencyListerInterface;
use Symfony\Component\Validator\Constraints as Assert;
/** /**
* Money type for QR code * Money type for QR code
*/ */
class QRCodeMoney class QRCodeMoney
{ {
#[Assert\Positive(
message: 'messages.not_valid_amount',
)]
#[Assert\NotBlank(message: 'messages.fill_value')]
private ?string $amount; private ?string $amount;
// TODO: getCurrencies validation
#[Assert\NotBlank(message: 'messages.fill_value')]
private ?string $currency; private ?string $currency;
/** /**

View File

@ -3,12 +3,46 @@ declare(strict_types=1);
namespace App\Entity\QRCode; namespace App\Entity\QRCode;
use Symfony\Component\Validator\Constraints as Assert;
/** /**
* Payment identification for QR code * Payment identification for QR code
*/ */
class QRCodePaymentIdentification { class QRCodePaymentIdentification {
#[Assert\NotBlank(message: 'messages.fill_value')]
#[Assert\Length( // <<< Ano, mohl jsem si napsat vlastni validator ...
min: 1,
max: 10,
minMessage: 'messages.not_variable_symbol',
maxMessage: 'messages.not_variable_symbol',
)] // https://en.wikipedia.org/wiki/Variable_symbol
#[Assert\Positive(
message: 'messages.not_variable_symbol',
)]
private string $variableSymbol; private string $variableSymbol;
#[Assert\NotBlank(message: 'messages.fill_value')]
#[Assert\Length(
min: 1,
max: 10,
minMessage: 'messages.not_specific_symbol',
maxMessage: 'messages.not_specific_symbol',
)] // https://cs.wikipedia.org/wiki/Specifick%C3%BD_symbol
#[Assert\Positive(
message: 'The {{ value }} is not a variable symbol.',
)]
private string $specificSymbol; private string $specificSymbol;
#[Assert\NotBlank(message: 'messages.fill_value')]
#[Assert\Length(
min: 1,
max: 10,
minMessage: 'messages.not_constant_symbol',
maxMessage: 'messages.not_constant_symbol',
)] // https://www.hyponamiru.cz/en/glossary/constant-symbol/
#[Assert\Positive(
message: 'messages.not_constant_symbol',
)]
private string $constantSymbol; private string $constantSymbol;
public function getVariableSymbol(): string public function getVariableSymbol(): string

View File

@ -3,11 +3,22 @@ declare(strict_types=1);
namespace App\Entity\QRCode; namespace App\Entity\QRCode;
use Symfony\Component\Validator\Constraints as Assert;
/** /**
* Options for generating QR code * Options for generating QR code
*/ */
readonly class QRCodeQROptions { readonly class QRCodeQROptions {
#[Assert\Positive(
message: 'messages.scale_must_be_positive',
)]
#[Assert\NotBlank(message: 'messages.fill_value')]
private int $scale; private int $scale;
#[Assert\PositiveOrZero(
message: 'messages.margin_must_be_positive',
)]
#[Assert\NotBlank(message: 'messages.fill_value')]
private int $margin; private int $margin;
/** /**

View File

@ -22,7 +22,7 @@ class QRCodeType extends AbstractType
->add('message', TextType::class) ->add('message', TextType::class)
->add('money', QRCodeMoneyType::class) ->add('money', QRCodeMoneyType::class)
->add('paymentIdentification', QRCodePaymentIdentificationType::class) ->add('paymentIdentification', QRCodePaymentIdentificationType::class)
->add('save', SubmitType::class); ->add('generate_qr_code', SubmitType::class);
} }
public function configureOptions(OptionsResolver $resolver): void public function configureOptions(OptionsResolver $resolver): void