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 8790707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:43:31+00:00 2026-06-13T22:43:31+00:00

Should input validation be done with a separate class, rather than in a DB

  • 0

Should input validation be done with a separate class, rather than in a DB write class? Or should it be a method of the writing class?

Eg. (shorthand version)

class DBWrite {

   public function add($input) {
       $data = new Validator($input);
       if($data->isValid() == true) {
           // write to DB
       }
   }

}

or would it be better to have the validation as part of the DBWrite class? Or would it be in too much of violation in terms with separation of concerns (or something else)?

class DBWrite {

   public function add($input) {
       if($this->inputIsValid() == true) {
           // write to DB
       }
   }

   private function inputIsValid() {
       // Check that input is valid and return true if it is
   }
}

Any other thoughts will also be appreciated, thanks!

  • 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-06-13T22:43:32+00:00Added an answer on June 13, 2026 at 10:43 pm

    If the Validator can be used in other classes than DBWrite, or if later you may want to swap out validators later, I’d go the route of separate Validator class and subclasses. You might also want to add a method to set/get your validator class in your DBWrite so you can swap out different validators at runtime.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im writing a program that should read input via stdin, so I have the
I have been using the date_parse method for input validation, which seems to be
All, I'm attempting to write what should be simple validation for a Struts2 action.
A webservice should validate input data and write to the database. Later, other services
The user should input two strings and the script will performe a text search:
According to the W3 CSS spec, something like: input[type~=text password] should select input fields
Should I validate input parameters on all functions I create? Input isn't passed from
I have a number input that should trigger jQuery on every change. To do
I am using a <form:input> on my jsp. The input field should be editable
here is the input data: % @param Landmarks: % Landmarks should be 1*m struct.

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.