[development] decoupling form validation from error reporting and "actions" OR different use case for DANGEROUS_SKIP_CHECK

Ivan Sergio Borgonovo mail at webthatworks.it
Wed May 21 16:23:18 UTC 2008


On Wed, 21 May 2008 10:05:24 -0400
"Moshe Weitzman" <weitzman at tejasa.com> wrote:

> damn, thats sneaky. nice one.

It is. Just it "waste" FAPI validation and it's just a longer way to
write DANGEROUS_SKIP_CHECK.

What I actually did today for D5 was:
- set DANGEROUS_SKIP_CHECK
- really copy&paste the FAPI code into a _validate element hook
- set custom messages there

Still I've coupling and duplication of code.

Roberto's technique doesn't improve the coupling problem and it skips
the #required and #maxlength check too.
Somehow set DANGEROUS_SKIP_CHECK looks better even if Roberto's
solution becomes a smart *necessary workaround* in D6+.

What about changing:

function form_set_error($name = NULL, $message = '') {
  static $form = array();
  if (isset($name) && !isset($form[$name])) {
    $form[$name] = $message;
    if ($message) {
      drupal_set_message($message, 'error');
    }
  }
  return $form;
}

to

function form_set_error($name = NULL, $message = null, $type=null) {
  static $form = array();
  if (isset($name)) {
    if(isset($message)) {
      $form[$name] = Array('#type'=>$type, '#message'=>$message);
    } else {
      unset($form[$name]);
    }
  }
  return $form;
}

+

function report_errors() {
  $errors=form_get_error();
  if(is_array($errors)) {
    foreach($errors as $error) {
      if($error['message']) {
        drupal_set_message($error['message'],'error');
      }
    }
  }
}

sort of

then custom validate functions could access errors calling
form_get_error[s]() and change them with form_set_error()

After all form_set_error should be called during a form life cycle
so... sooner or later drupal_process_form is going to be called so it
could be possible to call the error reporting function just after 
drupal_validate_form get called without any breakage of existing code.

-- 
Ivan Sergio Borgonovo
http://www.webthatworks.it



More information about the development mailing list