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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:00:03+00:00 2026-05-30T22:00:03+00:00

I’m trying to create my own custom validator class that I can check the

  • 0

I’m trying to create my own custom validator class that I can check the length of a user supplied value. I know there are plenty of already created options out there, but I am trying to learn PHP and creating my own work is the approach I want to take.

I will try and explain what I am doing here and hopefully I don’t lose anyone. I will try and let the code talk for me as much as possible. I will start out by showing my multidimensional array that I will use to get the posted information:

$input_array = array('username' => array( 'value'=> $_POST['username'],
                                          'min'  => 3,
                                          'max'  => 20
                                         ),
                     'password' => array( 'value'=> $_POST['password'],
                                          'min'  => 8,
                                          'max'  => 16
                                         ),
                     'email'    => array( 'value'=> $_POST['email'],
                                          'min'  => 6,
                                          'max'  => 50
                                         )
                     );

create the object:

$obj = new Validate(array_map('mysql_real_escape_string',$input_array));
$vl  = $obj->ValidateLength();

So here I will show the class:

class Validate
{
    private $inputArray;
    public function __construct($inputAray)
    {
        $this->inputArray = $inputArray;
    }
    public function ValidateLength()
    {
        $larray = array('error'=>'','data'=> array());
        foreach($this->inputArray as $k => $i)
        {
            if(strlen($i['value']) < $i['min'])
            {
                $larray['error'] = true;
                $larray['data'][$k] = ' must be at least '.$i['min'].' characters long';
            }
        }
        return json_encode($larray);
    }
}

What the class does is checks the input value and sees if it fits within the min and max I put in the array. If it finds an error it changes the error value to true and puts all the errors in an array to return in JSON format.

Now I have this little guy:

$length = json_decode($obj->ValidateLength(),true);

which will convert the JSON into another multidimensional array like so:

var_dump($length)

array(2){
 ["error"]=>
 bool(true)
 ["data"]=>
 array(1){
 ["username"]=>
 string(35)" must be at least 3 charracters long"
 ["password"]=>
 string(35)" must be at least 8 characters long"
 }
}

So here is where the problem arises. I have this conditional that notices that there is an error and should print out the error message for username and password:

$key   = implode("\n",array_keys($length['data']));
$value = implode(", ",array_values($length['data']));
if($length['error'])
{
    print("Error :\n\n".strtoupper($key).$value);
}
else
{
    print('everything is within parameters');
}

What I get is this:

Error:

USERNAME
PASSWORD must be at least 3 characters long, must be at least 8 characters long

What I want is this:

Error:

USERNAME must be at least 3 characters long
PASSWORD must be at least 8 characters long

A few thing to note is, I am trying to avoid a foreach() if at all possble in the conditional to make it clean an easy. Also keeping modularization in mind, I want to be able to change username password email in the first initial array that gets the user input to work with other projects.

If did’t explain myself too well, and if you are lost at what I am trying to achieve please let me know and I will try better. Thanks in advance for having a looksie!

  • 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-30T22:00:05+00:00Added an answer on May 30, 2026 at 10:00 pm

    Could just add the name of the field to the error?

    $larray['data'][$k] = sprintf('%s must be at least %d characters long',$k,$i['min']);
    

    I don’t know if that helps at all?

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

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string

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.