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

  • Home
  • SEARCH
  • 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 4615672
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:52:24+00:00 2026-05-22T01:52:24+00:00

i have a data validation class method where i check the user input before

  • 0

i have a data validation class method where i check the user input before inserting the records into the database. i would like to apply many rules per field method for example

field email will have to validate the
following

a) validate email using
FILTER_VALIDATE_EMAIL

b) check if duplicate mail exist in
database(this does not apply
everytime)

field name should have to validate the following.

a) only a-z or A-Z with space are
allowed

b) it should be minimum 5 and maximum
40 characters

and so on i would want to make sure that i should be able to apply many rules per field and it should be optional as well.

here is the original code i was using.

public function validate() {
    if(!empty($this->name)) {
        if(!preg_match('/^[a-zA-z ]{3,50}$/',$this->name)) {
            $this->error['name'] = 'Name should be valid letters and should be between 3 and 25 characters';
        }
    }
    if(!empty($this->email)) {
        if(!filter_var($this->email,FILTER_VALIDATE_EMAIL)) {
            $this->error['invalidEmail'] = 'Invalid email address';
        }
        if($this->emailCount($this->email)) {
            $this->error['emailExist'] = 'Email already exist';
        }
    }
    if(!empty($this->password)) {
        $this->password = trim($this->password);
        if(strlen($this->password) < 5 || strlen($this->password > 40)) {
            $this->error['password'] = 'Password length should be between 5 and 40 characters';
        }
    }
    if(!empty($this->pPhone)) {
        if(!preg_match('/^[0-9]{5,10}$/',$this->pPhone)) {
            $this->error['invalidpPhone'] = 'Invalid primary phone number';
        }
    }
    if(!empty($this->sPhone)) {
        if(!preg_match('/^[0-9]{5,10}$/',$this->sPhone)) {
            $this->error['invalidsPhone'] = 'Invalid secondary phone number';
        }
    }
    return (empty($this->error)) ? true : false;    
}

instead of using lots of if condition i want to use switch case with multi dimensional array with something like this.

var $validate = array(
    'name' => array(
        'notEmpty'=> array(
            'rule' => 'notEmpty',
            'message' => 'Name can not be blank.'
        ),
        'allowedCharacters'=> array(
            'rule' => '|^[a-zA-Z ]*$|',
            'message' => 'Name can only be letters.'
        ),
        'minLength'=> array(
            'rule' => array('minLength', 3),
            'message' => 'Name must be at least 3 characters long.'
        ),
        'maxLength'=> array(
            'rule' => array('maxLength', 255),
            'message' => 'Name can not be longer that 255 characters.'
        )
    ),
    'email' => array(
        'email' => array(
            'rule' => 'email',
            'message' => 'Please provide a valid email address.'
        ),
        'isUnique' => array(
            'rule' => 'isUnique',
            'message' => 'This E-mail used by another user.'
        )
    )        
); 

i am getting confused on how to implement mt code to be compatible with the later one. with regard to my original i will be thankful if someone demonstrate me with an example about implementing validation with the later one.

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-22T01:52:24+00:00Added an answer on May 22, 2026 at 1:52 am

    I would put this into some functions:

    // Validation methods
    function checkMail($mail) {
       // perform mail checks
       if(!valid)
          throw Exception("not valid mail");
    }
    
    function checkMinLength($string) {
       // perform length
       if(!valid)
          throw Exception("not valid length");
    }
    
    // mapping fields - methods
    $mappingArray = array('mailfield' => array('checkMail'), 'lengthfield' => array('checkMinLength');
    
    // perform checking
    try {
       foreach($arrayContaintingYourFields as $field) {
          foreach($mappingArray[$field['name']] as $check) {
             call_user_func($check, $field['value']);
          }
       }
    } catch (Exception $e) {
       echo $e->getMessage();
    }
    

    You can influence the error handling by defining own exception types and reacting in different ways.

    } catch (MailException $e) {
       echo $e->getMessage();
    } catch (LengthException $e) {
       // do some other stuff
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a user class setup with validation like this: [MetadataType(typeof(ssUserMetaData))] public
I have data in a MySQL database. I am sending the user a URL
I have a data validation class that checks whether the start date of a
I have a situation where I am using wpf data binding and validation using
I have data coming from the database in the form of a DataSet .
I have data that looks like CUSTOMER, CUSTOMER_ID, PRODUCT ABC INC 1 XYX ABC
I have data from a table in a database (string) that contain text and
I have data that looks like this: entities id name 1 Apple 2 Orange
If I have data like this: Key Name 1 Dan 2 Tom 3 Jon
I have a situation where a user can click a check box field, field_enable,

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.