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!
Could just add the name of the field to the error?
I don’t know if that helps at all?