On my site I have my register page, where a user inputs his information to register to my site.
When he does the I process the user inputted information. Now when processing it I'm using arrays to facilitate the process, and not have me write the same code over and over. For example, here is how I gather the user information and check it using a method in my validation class.
$data = array(
$_POST['firstname'] => "First Name",
$_POST['lastname'] => "Last Name"
);
foreach($data as $userInput => $fieldName) {
$validation = new Validation();
$result = $validation->checkEmpty($userInput, $fieldName);
}
Now this all works for me on my page, I'm able to check to see if the user left something empty on the register with the method "checkEmpty", which also returns an array of the fields left empty. I didn't show the method because it's not part of my problem. Now, here's my question, I want to also check the length of the fields. What would be a good way for me to do this without rewriting things over and over? Would I have to do another array?
I'm thinking something like this, maybe?
$data2 = array(
$_POST['firstname'] => 30,
$_POST['lastname'] => 35
),
foreach($data as $userInput => $limit) {
$result = $validation->checkLength($userInput, $limit);
}
But where I get stumped is, if one the inputted fields is too long, how do return which one it was if in the array I passed through I don't have the field name to which it belongs? I thoug开发者_如何学Goht of a multi-dimensional array, but I'm not sure if that will work.
I would structure the the array completely differently, something like:
$config = array(
'firstname' => array('notEmpty' = true,
'limit' => 30),
'lastname' => array('notEmpty' = true,
'limit' => 35)
);
Then I would create a method validate
that looks like this:
public function validate($config, $data) {
$error = array();
foreach($data as $field => $value) {
$c = $config[$field];
if(array_key_exists('notEmpty', $c)) {
if(!$this->checkEmpty($value)) {
$this->addError($error, $field, 'Must not be empty');
}
}
if(array_key_exists('limit', $c)) {
if(!$this->checkLength($value, $c['limit'])) {
$this->addError($error, $field, 'Must not be longer than' . $c['limit'] . ' characters');
}
}
/*...*/
}
return $error;
}
private function addError(&$error, $field, $msg) {
if(!array_key_exists($field, $error)) {
$error[$field] = array();
}
$error[$field][] = $msg;
}
Then you just call:
$validation = new Validation();
$errors = $validation->validate($config, $_POST);
and $errors
will contain all error messages per field. You just need to to loop over them and print them next to the field.
Of course this code can (and should be!) improved. E.g. dynamic lookup of validation methods).
That said, I highly recommend to have a look at and use a ready made validation classes such as Zend_Validate
I would think using the field names as keys for your array may be a better approach. Then yeah, use a multi-dimensional array something like this.
$data = array(
'First Name' => array($_POST['firstname'], 30),
'Last Name' => array($_POST['lastname'], 35)
)
You gloss over the validation class, but I think its important. For one it should be used statically if it doesn't have any state. Theres no reason to be constructing it every single loop iteration like that!
Validation::checkEmpty($x);
AS for the length checking, id suggest you keep fields in Validation something like
protected $lengths = array( 'firstname' => 35, 'lastname' => 30, 'default' => 100);
and call it with
Validation::checkLength($userInput, $fieldName);
with a body like
protected static function checkLength($subject, $fieldType = 'default')
{
return strlen($subject) < self::$lengthTypes($fieldType);
//or self::$fields['lengthLimit'] if you are going to structure it like Felix suggest
}
You don't need me to tell you that a function lengthChecker($subject, $limit)
is pretty pointless.
EDIT - restructuring like Felix suggested isn't a bad idea depending on how far you want tot ake this. His looks more extensible. If you are going to implement the addError
you probably don't want the static design.
精彩评论