public function save() { GUMP::add_validator("unique", function ($field, $input, $param = NULL) { $checkExistingUser = R::findOne('user', 'user=?', array($input)); if ($checkExistingUser == NULL) { return FALSE; } else { return TRUE; } }); GUMP::add_validator("strong", function ($field, $input, $param = NULL) { return checkPasswordStrength($input); }); $rules = array('reseller_username' => 'required|alpha_numeric|max_len,10|min_len,6|unique', 'reseller_password' => 'required|max_len,10|min_len,7|strong'); $filters = array('reseller_username' => 'trim|sanitize_string', 'reseller_password' => 'trim|sanitize_string|md5'); $app = Slim::getInstance(); $post = $app->request()->post(); // $app - Slim main app instance $postValues = $gump->filter($post, $filters); $validated = $gump->validate($gump->filter($postValues, $filters), $rules); if ($validated === TRUE) { $createUser = R::dispense('user'); $createUser->user = $postValues['reseller_username']; $createUser->user = $postValues['reseller_password']; } else { $this->setError($gump->get_readable_errors(true)); } if ($this->getError() == "") { $this->fails = FALSE; } else { $this->fails = TRUE; } }
#!/usr/bin/php -q <?php require "../gump.class.php"; // Add the custom validator GUMP::add_validator("is_object", function ($field, $input, $param = NULL) { return is_object($input[$field]); }); // Generic test data $input_data = array('not_object' => 5, 'valid_object' => new stdClass()); $rules = array('not_object' => "required|is_object", 'valid_object' => "required|is_object"); // METHOD 1 (Long): $validator = new GUMP(); $validated = $validator->validate($input_data, $rules); if ($validated === true) { echo "Validation passed!"; } else { echo $validator->get_readable_errors(true); } // METHOD 2 (Short): $is_valid = GUMP::is_valid($input_data, $rules); if ($is_valid === true) { echo "Validation passed!"; } else { print_r($is_valid); }
}); $app->map('/signup', function () use($app) { if ($app->request->post()) { $post['username'] = $app->request->post('username'); $post['email'] = $app->request->post('email'); # https://github.com/Wixel/GUMP $gump = new GUMP(); GUMP::add_validator('istaken', function ($field, $input, $param = NULL) { if (!empty($input[$field])) { $app = \Slim\Slim::getInstance(); # http://stackoverflow.com/questions/4364686/how-do-i-sanitize-input-with-pdo # https://youtu.be/sRfYgco3xo4?t=1758 $sql = 'SELECT name FROM users WHERE name=:name OR email=:name'; $user = $app->db->prepare($sql); /*** bind the paramaters ***/ $user->bindParam(':name', $input[$field], PDO::PARAM_STR); /*** execute the prepared statement ***/ $user->execute(); $user = $user->fetch(PDO::FETCH_ASSOC); if (is_array($user)) { return false; } } }); $validation_rules_1 = array('username' => 'required|min_len,6|alpha_space|istaken', 'email' => 'required|valid_email|istaken', 'password' => 'required', 'password_confirm' => 'required'); $validation_rules_2 = array('username' => 'required|min_len,6|alpha_space', 'email' => 'required|valid_email', 'password' => 'required', 'password_confirm' => 'required'); $gump->validation_rules($validation_rules_1); $filter_array = array('username' => 'trim|sanitize_string|rmpunctuation', 'email' => 'trim|sanitize_string|sanitize_email', 'password' => 'trim', 'password_confirm' => 'trim'); $gump->filter_rules($filter_array); $validated_data = $gump->run($app->request->post()); if ($validated_data !== false) {