Skip to content

Instantly share code, notes, and snippets.

@vudaltsov
Created December 12, 2018 21:26
Show Gist options
  • Save vudaltsov/e83853cc00e2ff1d2d8090b5441fecee to your computer and use it in GitHub Desktop.
Save vudaltsov/e83853cc00e2ff1d2d8090b5441fecee to your computer and use it in GitHub Desktop.
Domain constraints for Symfony Validator: ArticleDto
<?php
use Symfony\Component\Validator\Constraints as Assert;
final class ArticleDto
{
/**
* @Assert\NotNull()
* @Assert\Type("string")
* @Assert\Length(min=5, max=50)
* @Assert\Regex("/^[a-z0-9-]$/", message="Slug must be composed of lowercase latin letters, digits and dashes.")
*
* @var null|string
*/
public $slug;
}
@Pochwar
Copy link

Pochwar commented Apr 20, 2023

I think the regex needs a * so that it can match more than 1 character: "/^[a-z0-9-]*$/"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment