Skip to content

Commit

Permalink
Clean up form validation code.
Browse files Browse the repository at this point in the history
  • Loading branch information
bharat committed Jan 29, 2010
1 parent 0d73738 commit c214dfd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
11 changes: 1 addition & 10 deletions modules/akismet/controllers/admin_akismet.php
Expand Up @@ -25,17 +25,8 @@ public function index() {
// @todo move the "post" handler part of this code into a separate function
access::verify_csrf();

$valid = $form->validate();

if ($valid) {
if ($form->validate()) {
$new_key = $form->configure_akismet->api_key->value;
if ($new_key && !akismet::validate_key($new_key)) {
$form->configure_akismet->api_key->add_error("invalid", 1);
$valid = false;
}
}

if ($valid) {
$old_key = module::get_var("akismet", "api_key");
if ($old_key && !$new_key) {
message::success(t("Your Akismet key has been cleared."));
Expand Down
17 changes: 11 additions & 6 deletions modules/akismet/helpers/akismet.php
Expand Up @@ -23,8 +23,9 @@ class akismet_Core {
static function get_configure_form() {
$form = new Forge("admin/akismet", "", "post", array("id" => "g-configure-akismet-form"));
$group = $form->group("configure_akismet")->label(t("Configure Akismet"));
$group->input("api_key")->label(t("API Key"))->value(module::get_var("akismet", "api_key"));
$group->api_key->error_messages("invalid", t("The API key you provided is invalid."));
$group->input("api_key")->label(t("API Key"))->value(module::get_var("akismet", "api_key"))
->callback("akismet::validate_key")
->error_messages("invalid", t("The API key you provided is invalid."));
$group->submit("")->value(t("Save"));
return $form;
}
Expand Down Expand Up @@ -82,10 +83,14 @@ static function submit_ham($comment) {
* @param string $api_key the API key
* @return boolean
*/
static function validate_key($api_key) {
$request = self::_build_verify_request($api_key);
$response = self::_http_post($request, "rest.akismet.com");
return "valid" == $response->body[0];
static function validate_key($api_key_input) {
if ($api_key_input->value) {
$request = self::_build_verify_request($api_key_input->value);
$response = self::_http_post($request, "rest.akismet.com");
if ("valid" != $response->body[0]) {
$api_key_input->add_error("invalid", 1);
}
}
}


Expand Down

0 comments on commit c214dfd

Please sign in to comment.