Validating External Forms

Permalink
Hi,

Anyone got an example of this?

for example in my form

<div class="form-field">
<?php echo $form->label('email', 'Email address');
echo $form->text('email'); ?>
</div>

How would I validate this? Something like

echo $form->text('email', required); ?>

 
hutman replied on at Permalink Reply
hutman
If you are using an External Form block you can validate on the server side. So the action in your form would be something like

$this->action('do_contact');


then in your controller you would have the action_do_contact function where you could do your validation

$e = Loader::helper('validation/error');
if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email'])) {
   $email = $_POST['email'];
} else {
   $e->add("Please include a valid email address.");
}
if (!$e->has()) {
   //there are no errors
} else {
   $this->set('error', $e);
}


and then back on your form page you could handle the errors with something like this

if (isset($error) && $error != '') {
   if ($error instanceof Exception) {
      $_error[] = $error->getMessage();
   } else if ($error instanceof ValidationErrorHelper) {
      $_error = $error->getList();
   } else if (is_array($error)) {
      $_error = $error;
   } else if (is_string($error)) {
      $_error[] = $error;
   }
   echo '<ul style="color: red;">';
   foreach($_error as $e) 
      echo '<li>'.$e.'</li>';
   echo '</ul>';
}
leblaireau replied on at Permalink Reply
That looks great have you validated the captcha?

<?php $captcha = Loader::helper('validation/captcha');?>
<?php $captcha->display(); ?>
<?php $captcha->showInput(); ?>