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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:46:35+00:00 2026-05-25T19:46:35+00:00

I need help understanding the logic to deal with validating user inputs. my current

  • 0

I need help understanding the logic to deal with validating user inputs.

my current state of validating user data is at worst, i feel pretty awkward using these messy lines of codes, have a look at my typical function which i uses it to get input from the user and process it to database.

public function saveUser($arguments = array()) {
    //Check if $arguments have all the required values
    if($this->isRequired(array('name','email','password','pPhone','gender','roleId'))) {
        //$name should could minimum of 5 and maximum of 25 chars, and is a strict character.
        $name = $this->isString(5, 25, $this->data['name'], 'STRICT_CHAR');
        $email = $this->isEmail($this->data['email']);
        $pPhone = $this->isString(5, 12, $this->data['pPhone'], 'STRICT_NUMBER');
        $sPhone = (!empty($this->data['sPhone'])) ? $this->isString(5, 12, $this->data['sPhone'], 'STRICT_NUMBER') : 0;
        //Check For Duplicate Email Value
        $this->duplicate('user_details','email',$email);
        //If Static Variable $error is not empty return false
        if(!empty(Validation::$error)) { return false; }
        //After Validation Insert the value into the database.
        $sth = $this->dbh->prepare('INSERT QUERY');
        $sth->execute();
    }
}

Now is the time i focus on improving my validation code. i would like all my class methods to validate the user inputs before inserting into the database. basically a class methods which takes user input should be able to perform the following.

  1. If class method accepts user input as an array then check for all required inputs from within the array.

  2. Check the Minimum and Maximum Length of the input.

  3. Check for any illegal character.

etc.

I would like to know how to deal with this, and also if there is any PHP Independent Validation Component which can come to my rescue. it would be of great help. if i am doing it wrong please suggest me on improving my code, i won’t mind going to any extent as long as it guarantees that my code follows the coding standard.

I will also appreciate if someone could explain me on dealing with validation logic for validating user input for a class method of an object.

Thank you..

  • 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-25T19:46:36+00:00Added an answer on May 25, 2026 at 7:46 pm

    PHP 5.2 has a new core extension called “filter functions”. You can use this extension to sanitize and validate user data.

    For example, to validate an email address:

    if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
        echo "This (email) email address is considered valid.";
    }
    

    As for dealing with validation in general, you want to decouple the validation process from the incoming data and the objects themselves. I use the Lithium PHP framework, and their data validation class is implemented as a nearly independant utility class. Check it out for ideas on how to roll your own: http://li3.me/docs/lithium/util/Validator

    Using their class, you get something like this:

    // Input data. This can be an $object->data() or $_POST or whatever
    $data = array(
        'email' => 'someinvalidemailaddress';
    );
    
    // Validation rules
    $rules = array(
        'email' => array(
            array('notEmpty', 'message' => 'email is empty'),
            array('email', 'message' => 'email is not valid')
        )
    );
    
    // Perform validation
    Validator::check($data, $rules);
    
    // If this were in your object
    public validate($data = array(), $rules = array()) {
        $data = !empty($data) ? $data : $this->data; // Use whatever data is available
        $rules = $this->rules + $rules; // Merge $this's own rules with any passed rules
    
        return Validator::check($data, $rules));
    }
    
    // You can have a save method like
    public save() {
        if ($this->validates()) {
            // insert or update
        }
    }
    
    // And your object would 
    $user = new User();
    $user->data = array('email' => 'whatever');
    $user->save();
    

    And there’s always Zend Validate. You can look it up at http://framework.zend.com/manual/en/zend.validate.set.html

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

Sidebar

Related Questions

I need help understanding the best way to manipulate a JavaScript data structure (if
Hi need a little help understanding the following statement and the logic of what
I need help understanding some C++ operator overload statements. The class is declared like
I need some help understanding this bit of code pre { white-space: pre; white-space:
I'm a SQL noob, and I need a little bit of help understanding the
Need help writing a script downloads data from google insight using c# this is
I need help getting my head around the difference between my current OOP notion
I am new to c# and need help understanding what going on in the
Need some help understanding why my concat() is failing and how to fix it.
I need some help understanding some of the points from Paul Graham’s What Made

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.