toshy/validation-bundle

Additional Symfony validators.

Installs: 2 726

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 2

Open Issues: 0

Type:symfony-bundle

2.1.0 2023-12-23 18:12 UTC

This package is auto-updated.

Last update: 2024-08-23 19:44:40 UTC


README

Additional validators set for Symfony 6/7.

This is a fork from secit-pl/validation-bundle.

Installation

From the command line run

$ composer require toshy/validation-bundle

Validators

NotBlankIf

This validator checks if value is not blank like a standard NotBlank Symfony validator, but also allows define the condition when the NotBlank validation should be performed using Symfony Expression Language.

From Symfony 6.2 you can also use When validator.

https://symfony.com/blog/new-in-symfony-6-2-conditional-constraints

https://symfony.com/doc/6.2/reference/constraints/When.html

Example usage

use SecIT\ValidationBundle\Validator\Constraints as SecITAssert;

// ...

#[SecITAssert\NotBlankIf("this.isSuperUser")]
private ?string $email = null;

public function isSuperUser(): bool
{
    return true;
}

Parameters

FileExtension

This validator checks if file has valid file extension.

From Symfony 6.2 you can also use the "extensions" option in File validator.

https://symfony.com/blog/new-in-symfony-6-2-improved-file-validator

https://symfony.com/doc/6.2/reference/constraints/File.html#extensions

Example usage

use SecIT\ValidationBundle\Validator\Constraints as SecITAssert;

// ...

#[SecITAssert\FileExtension(["jpg", "jpeg", "png"])]
private $file;
use SecIT\ValidationBundle\Validator\Constraints as SecITAssert;

// ...

#[SecITAssert\FileExtension(disallowedExtensions: ["jpg", "jpeg", "png"])]
private $file;

Parameters

Caution! It's highly recommended to use this validator together with native Symfony File/Image validator.

use SecIT\ValidationBundle\Validator\Constraints as SecITAssert;
use Symfony\Component\Validator\Constraints as Assert;

// ...

#[Assert\Image(maxSize: '2M', mimeTypes: ["image/jpg", "image/jpeg", "image/png"])]
#[SecITAssert\FileExtension(validExtensions: ["jpg", "jpeg", "png"])]
private $file;

CollectionOfUniqueElements

Checks if collection contains only unique elements.

Parameters

use SecIT\ValidationBundle\Validator\Constraints as SecITAssert;

// ...

#[SecITAssert\CollectionOfUniqueElements()]
private $collection;

This validator can also be used to validate unique files upload.

<?php

declare(strict_types=1);

namespace App\Form;

use SecIT\ValidationBundle\Validator\Constraints\CollectionOfUniqueElements;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\FormBuilderInterface;

class ExampleType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('files', CollectionType::class, [
            'entry_type' => FileType::class,
            'allow_add' => true,
            'constraints' => [
                new CollectionOfUniqueElements(),
            ],
        ]);
    }
}

AntiXss

Checks if text contains XSS attack using voku\anti-xss library.

use SecIT\ValidationBundle\Validator\Constraints as SecITAssert;

// ...

#[SecITAssert\AntiXss()]
private $text;

NaiveNoHtml

Perform very naive check if text contains HTML.

use SecIT\ValidationBundle\Validator\Constraints as SecITAssert;

// ...

#[SecITAssert\NaiveNoHtml()]
private $text;

Parameters

BurnerEmail

Checks if email address is a throw away email addresses (burner email). This check is perform against the list provided by wesbos/burner-email-providers. You need to install this package manually (composer require wesbos/burner-email-providers) if you'd like to use this validator.

use SecIT\ValidationBundle\Validator\Constraints as SecITAssert;

// ...

#[SecITAssert\BurnerEmail()]
private $email;