How to put my validation block of codes into Function PHP -
how can put validation codes function? how going return , call it? trying call put them in 1 code , call them in function forms. idea?
here's codes:
function validate(){ $errors = array(); //empty array collect errors //validation codes (need inside function) if(empty($_post['email']) , filter_var($email, filter_validate_email) != false) { $errors[] = "email cannot blank"; } if(empty($_post['first_name'])) { $errors[] = "first name cannot blank"; } if(empty($_post['last_name'])) { $errors[] = "last name cannot blank"; } if(empty($_post['password'])) { $errors[] = "password cannot blank"; } if(empty($_post['confirm_password']) , $_post['password'] == $_post['confirm_password']) { $errors[] = "please enter matching password"; } if(empty($_post['confirm_password']) , $_post['password'] == $_post['confirm_password']) { $errors[] = "please enter matching password"; } if(!isset($_post['date']) || strtotime($_post['date']) === false) { $errors[] = "birth date cannot blank"; } if(!empty($errors)) { //if there errors, assign session variable! $_session['errors'] = $errors; //redirect user using header('location: ') header('location: registration_page.php'); } else { $email = $_post['email']; $first_name = $_post['first_name']; $last_name = $_post['last_name']; $password = $_post['password']; $birth_date = $_post['date']; //redirect user next part of site! } } so when call this wont work:
echo validate(); hope can help. thanks!
so you're saying like:
class validation { public static function emailfilter($input) { global $_post; return empty($_post['email']) , filter_var($input, filter_validate_email) != false ? "email cannot blank" : false; } } or looking else?
edit 1
okay, how about:
function filter ($input, $type) { if (!$input or !$type) { switch ($type) { case "email": // check email if (empty($_post['email']) , filter_var($input, filter_validate_email)) { return "email cannot blank"; } break; case "first_name": if(empty($_post['first_name'])) { return "first name cannot blank"; } break; // , on. } } } you call by:
filter($_post['email'], 'email'); so then:
if (!filter($_post['email'], 'email')) { // email checks out. } else { $error[] = filter($_post['email'], 'email'); } there more elegant solutions available, based on think want.
Comments
Post a Comment