Forums

examples serverside validation

pino 29 Oct, 2009
hi
are there some easy examples for serverside validation?
some code examples?

i'm using a very easy form with couple of "required" fields...

thank you for your help
pino
GreyHead 29 Oct, 2009
Hi pino,

There's a simple example on the Validation tab
  <?php
if($_POST['accept_terms'] != 'yes')
return 'Sorry, but you need to accept our terms to proceed';
?>

Bob
pino 29 Oct, 2009
sorry for the basic questions but what do i need instead of "yes", that the user can put what ever he wants? field is required, can not be empty!!!

in your example the user has to write "yes"...
pino 29 Oct, 2009
how can i manage, that the user has not to reenter all the previous data?
nml375 29 Oct, 2009
Hi pino,
To validate that an input is not empty, use something like this:
<?
if (!isset($_POST['accept_terms']) || empty($_POST['accept_terms']))
  return 'Sorry, but you must enter _something_ into the accept terms field!';
?>

Or in J!1.5 style:
<?
if (!empty(JRequest::getString('accept_terms')))
  return 'Sorry, but you must enter _something_ into the accept terms field!';
?>


As for restoring the entered data, try enabling "Republish fields if error occured" on the General tab.

Edit: fixed a logic error. Should report a validation error when the user did _not_ submit any data..
/Fredrik
pino 29 Oct, 2009
Hi Fredrik
thanks a lot for your help! sorry i'm not a programmer...

i tried this for my 3 fields:
<?
if (!empty(JRequest::getString('Name','Email','Telefon')))
return 'Error Message!';
?>

sending the for gives me a blank (white) page?!
it's not going to de redirect adress?
nml375 29 Oct, 2009
Hi pino,
First off, there's a logic error in my previous post, there should be no exclamation mark (1) in front of empty(). I'll update the post once I'm done with this one..

The JRequest::getString() method is used to retrieve one field value, not multiple. In order to test multiple fields, you'll have to test each field at a time. A very simple version would be something like this, reporting the very first error it finds:
<?
if (empty(JRequest::getString('text_1')))
  return "text_1 may not be empty!";
if (empty(JRequest::getString('text_2')))
  return "text_2 may not be empty!";
if (empty(JRequest::getString('text_3')))
  return "text_3 may not be empty!";
?>

If you really don't care to inform the user which field was empty, you could also do it like this:
<?
if (empty(JRequest::getString('text_1')) || empty(JRequest::getString('text_2')) || empty(JRequest::getString('text_3')))
  return "There was an error validating the submitted data, please check your data and try again!";
?>


To do it a little more complex, you could try something like this, which will report a list of all failed tests:
<?
$validate_errors = "";
if (empty(JRequest::getString('text_1')))
  $validate_errors .= "text_1 may not be empty!<br />";
if (empty(JRequest::getString('text_2')))
  $validate_errors .= "text_2 may not be empty!<br />";
if (empty(JRequest::getString('text_3')))
  $validate_errors .= "text_3 may not be empty!<br />";
if (!empty($validate_errors))
  return "There were errors while validating the form:<br />" . $validate_errors;
?>


/Fredrik
GreyHead 29 Oct, 2009
Hi pino,

[[>> I see that Fredrick has posted more or less the same while I was writing this.<<]]

I think you get a php error if you use JRequest inside the if () and you also can't just pack all three field names in there at random Try this
<?
$name = JRequest::getString('Name','', 'post');
if ( !$name ) {
  return 'Error Message!';
}
$email = JRequest::getString('Email','', 'post');
if ( !$email ) {
  return 'Error Message!';
}
$phone = JRequest::getString('Phone','', 'post');
if ( !$phone ) {
  return 'Error Message!';
}
?>

Bob

PS It's a really bad idea to try to do this is you don't have at least basic skills in PHP. Please find someone to help out.
pino 29 Oct, 2009
Thank you Fredrik, Thank you Bob for your help!
It's working perfectly now!

pino
sinemac 23 Nov, 2009
I've decided to try serverside validation to get around an error on IE that appears to be caused by the validation process...

So, I've tried your example, Bob - and while it does work, it only reports the first error that it comes across. Is there a way to modify this so that it will go through the whole validation script and return a list of all found errors at once?

Thanks,
Scott
GreyHead 23 Nov, 2009
Hi Scott,

It woudl be something like this
<?php
$errors = array();
$name = JRequest::getString('Name','', 'post');
if ( !$name ) {
  $errors[] = 'Error Message!';
}
$email = JRequest::getString('Email','', 'post');
if ( !$email ) {
  $errors[] = 'Error Message!';
}
$phone = JRequest::getString('Phone','', 'post');
if ( !$phone ) {
  $errors[] = Error Message!';
}
if ( count($errors) ) {
  $message = "";
  foreach ($errors as $error ) {
    $message .= "<div class='error' >$error</div>";
  }
  return $message;
}
?>

Bob
This topic is locked and no more replies can be posted.