symfony-validator

Why do I receive “This value should be of type string” when using a DateTime constraint on Symfony 5?

时光总嘲笑我的痴心妄想 提交于 2020-03-09 05:41:07
问题 I have the following entity (only attached the relevant parts): use ApiPlatform\Core\Annotation\ApiResource; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; /** * @ApiResource(mercure=true) * @ORM\Entity(repositoryClass="App\Repository\EventRepository") */ class Event { /** * @ORM\Column(type="datetime") * @Assert\DateTime * @Assert\NotNull */ private $createdAt; public function __construct() { $this->createdAt = new \DateTime(); } public function

Symfony2 - How to validate autocomplete entity form type?

早过忘川 提交于 2019-12-23 12:26:14
问题 I have a form with following fields: $builder ->add('title', 'text') ->add('body', 'textarea') ->add('tags', 'entity', [ 'class' => 'AppBundle\Entity\Tag', 'choice_label' => 'name', 'expanded' => false, 'multiple' => true, ]); User can select multiple tags. Everything works perfectly. But now when the numbers of tag become very large (over 20000 tags), page rendering become very slow because entity type loads all tag into selectbox. Therefore I implement a jQuery autocomplete selectbox to

Symfony Form Validation Constraint Expression

孤者浪人 提交于 2019-12-21 06:07:32
问题 I have form, and need to create inline validation: $builder ->add('Count1', 'integer', [ 'data' => 1, 'constraints' => [ new NotBlank(), new NotNull(), ], ]) ->add('Count2', 'integer', [ 'constraints' => [ new NotBlank(), new NotNull(), ], ]) ->add('Count3', 'integer', [ 'data' => 0, 'constraints' => [ new NotBlank(), new NotNull(), ], ]) How white inline validation Expression for rules Count2 >=Count1 Count3 <=Count2 Count2 >= $someVariable 回答1: Other solution by using Expression Constraint

Validate UniqueEntity for One-To-Many, Unidirectional with Join Table [Symfony2]

三世轮回 提交于 2019-12-12 18:14:00
问题 I have 2 mapped entities , Box class Box{ //[...] /** * @ORM\ManyToMany(targetEntity="Candy", cascade={"remove"}) * @ORM\OrderBy({"power" = "DESC"}) * @ORM\JoinTable(name="box_candies", * joinColumns={@ORM\JoinColumn(name="box_id", referencedColumnName="id")}, * inverseJoinColumns={@ORM\JoinColumn(name="candy_id", referencedColumnName="id", unique=true)} * ) */ private $candies; } And Candy class Candy { /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM

Symfony - Restrict registration from specific domain

北城以北 提交于 2019-12-12 10:19:50
问题 I am working on a registration form where I need to put a validation on email id, if the email domain does not belong to a specific domain then person should not be able to register so my question is does symfony by default has this validation option which I can turn on or i need to create a custom validation? For example I only want people to register if the email id has yahoo.com 回答1: No, there's not a build-in feature in symfony2 for check domain email. But you can add it. What you can do

Use Symfony2 Validator Annotations outside of Core

会有一股神秘感。 提交于 2019-12-12 09:18:47
问题 How do you configure Symfony2 Validator to use annotations outside of Core? In core you would do the following: $container->loadFromExtension('framework', array( 'validation' => array( 'enable_annotations' => true, ), )); Taken from: http://symfony.com/doc/2.0/book/validation.html#configuration For now to make validation work the rules are set within the method loadValidatorMetadata(ClassMetadata $metadata), it works but I prefer annotations. Example Entity with validation annotations and

Get validator constraints of an entity

你离开我真会死。 提交于 2019-12-12 03:28:46
问题 I managed to inject the validator service into mine. Now I can't figure out how to get the different constraints from an entity metadata . 回答1: // In your controller, get the validator: $validator = $this->get('validator'); // Get Metadata for Class. You can use 'App\Bundle\Entity\YourEntity' as well $meta = $validator->getMetadataFor(YourEntity::class); // Used 'Default' as default Validation Group. $constraints = $meta->findConstraints('Default'); http://symfony.com/doc/current/book

Symfony The annotation does not exist, or could not be auto-loaded - Symfony Validation with Doctrine

回眸只為那壹抹淺笑 提交于 2019-12-11 17:31:09
问题 We have a legacy app which is not based on symfony. Doctrine is in use and now we would like to add validation to the models. Seems that the Annotations never get autoloaded, even when "use" statements are in use. [Semantical Error] The annotation "@Symfony\Component\Validator\Constraints\NotBlank" in property Test\Stackoverflow\User::$Username does not exist, or could not be auto-loaded. Wrote a small demo application to showcase the problem and how we create the entity manager and

Class 'doctrine.orm.validator.unique' not found

吃可爱长大的小学妹 提交于 2019-12-02 06:26:17
问题 So I am not sure what the issue is here, or how this class even gets loaded. But my model (or as their actually called, entity) looks like this: <?php namespace ImageUploader\Models; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; /** * @ORM\Entity * @ORM\Table(name="users") * @UniqueEntity(fields="userName") * @UniqueEntity(fields="email") */ class User { /** * @ORM\Id * @ORM\Column(type=

Conditional validation of fields based on other field value in Symfony2

心不动则不痛 提交于 2019-12-01 00:05:25
问题 So here is the scenario: I have a radio button group. Based on their value, I should or shouldn't validate other three fields (are they blank, do they contain numbers, etc). Can I pass all these values to a constraint somehow, and compare them there? Or a callback directly in the controller is a better way to solve this? Generally, what is the best practice in this case? 回答1: I suggest you to use a callback validator. For example, in your entity class: <?php use Symfony\Component\Validator