Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7659801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:22:51+00:00 2026-05-31T13:22:51+00:00

When validating a domain entity, is it better to validate the values as they

  • 0

When validating a domain entity, is it better to validate the values as they are set, or all at once with a validator (such as in Symfony2) later on?

For example:

Option 1. Validate while being set

public function setEmail($email)
{
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        throw new EntityException('The specified email address ' . $email . ' is invalid.');
    }

    $this->_email = $email;
    return $this;
}

Option 2. Validate later…

$user = new UserEntity();
$user->setEmail('johnnie@duh.com');
$validator = new Validator();
$validator->validate($user);

Option 3. Both of the above (although seems a little redundant and not worth the overhead).

I know that the first one probably makes for more airtight entities, but the second one makes for more user friendly error handling.

Also, the second option seems easier to configure and maintain…as I don’t have to adjust the setters, and I can centralize my validation logic to a single class.

What it boils down to…
So basically, it sounds like Option 2 is what I WANT to do, but feel like sacrificing the airtight-ness of the entity (for example, if I forget to run the entity through the validator), might be stupid.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:22:52+00:00Added an answer on May 31, 2026 at 1:22 pm

    Single Responsibility Principle

    The best is to have all necessary validations in a separated layer. With that will be easier to maintain and test the validators. Also easier to validate data across your application.

    Don’t Repeat Yourself

    You don’t have to call validate() for each entity.

    All you have to do is to implement the validation on your repository layer or service layer if you have one.

    $user = new User();
    // [...]
    $user->setEmail('myinvalidemail#blah,com');
    $repository->save($user);
    

    So in your user’s repository

    UserRepository extends AbstractRepository {}
    

    And the common validation for all entities:

    abstract class AbstractRepository {
        public function save($entity) {
            $validator = // Get your validator based on the entity's name or something else
            $validator->validate($entity); // Throws exceptions or flag the  fields for future use
    
             // Now save it...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am using hibernate validation annotation framework to validate my domain classes and validating
Where would you place validation code for a domain entity? Inside the class itself
While validating my CSS on http://jigsaw.w3.org/css-validator/ I am getting following errors: 1.Property zoom doesn't
Cannot validate this method because the domain mapping for the return type (XXXProxy) could
I have a RegEx for validating email addresses, but I'm really looking to validate
i have a generated domain model using LLBLGEN, and im validating those validation rules
I have a standard Domain Layer entity: public class Product { public int Id
We have recently adopted the specification patterns for validating domain objects and now want
How to save hibernate entity and ignore (not correct)transient pattern. for example: @Entity @Table(name=`REQUEST`)
I want to validate a domain object without automatic parameter binding to restrict the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.