napster
napster

Reputation: 125

Doctrine: Validate Model Data using Annotations

In doctrine, is there a way that I could validate model data using annotations? Like the bellow example in c#

       public class ProductMD {
            [StringLength(50),Required]
            public object Name { get; set; }
            [StringLength(15)]
            public object Color { get; set; }
            [Range(0, 9999)]
            public object Weight { get; set; }
        }

So when the property Name is empty then it will give you error.

Upvotes: 1

Views: 159

Answers (1)

Andrea Sprega
Andrea Sprega

Reputation: 2271

Unfortunately, starting from Doctrine2 there is no validation component integrated into the ORM itself anymore.

If, for example, you're using Doctrine2 with Symfony2, you can take advantage of the validation framework component by using @Assert annotations in the Doctrine entities.

If you don't use any framework, or if the framework you use does not provide a validation component, you can always use Doctrine's lifecycle callbacks to provide custom validation in @PrePersist and @PreUpdate (for more information, take a look here). In this case, there's more manual work to be done but it still sounds like a reasonable solution.

Upvotes: 1

Related Questions