server side validation for v4 not working

squawk 04 Nov, 2011
Hi

Can anyone tell me when if the server side validation method have changed since v3.
I'm using the tutiorial 'Build an extended User information form' as a base and within v4 the basic method doesn't run. It processes the onSuccess action regardless of the server side validation code?

I'm using.....

<?php
// get the user id
$cf_user_id = JRequest::getInt('cf_user_id', '', 'post');
if ( !$cf_user_id ) {
return "Sorry, we couldn't identify the user";
}
// initialise variables
$messages = array();
$input_array = array('firstname', 'username', 'email');

$db =& JFactory::getDBO();


// check the firstname, username & email
foreach ( $input_array as $v ) {
$temp = JRequest::getString($v, '', 'post');
if ( !$temp ) {
$messages[] = "You must include a value for the '$v' input";
}
if ( $v != 'firstname' ) {
$query = "
SELECT COUNT(*)
FROM `ie2sg_users`
WHERE `$v` = ".$db->quote($temp)." AND `id` != $cf_user_id;
";
$db->setQuery($query);
$count = $db->loadResult();
if ( $count ) {
$messages[] = "That $v is already in use";
}
}
}

// Check the passwords
$password = JRequest::getString('password', '', 'post');
$password_confirm = JRequest::getString('password_confirm', '', 'post');
if ( $password && $password != $password_confirm ) {
$messages[] = "The Password and Verify Password entries don't match";
}
if ( $messages ) {
return implode('<br />', $messages);
}
?>

Does this look roughly OK?

Many Thanks in advance. 😀
GreyHead 04 Nov, 2011
Hi squawk,

If in doubt check the help tab

Returning "boolean" false will fail, anything else or no return at all will lead to success.
Set fields errors by adding a new key => value entry to the $form->validation_errors array, where "key" is the "field name" and "value" is the "Error message", for example, if you want to set an error to the "email" field you should use this code
$form->validation_errors['email'] = "Email error message is here.";.


So your code becomes
<?php
$return = true;
// get the user id
$cf_user_id = JRequest::getInt('cf_user_id', '', 'post');
if ( !$cf_user_id ) {
  $form->validation_errors['cf_user_id'] "Sorry, we couldn't identify the user";
  $return = false;
}
// initialise variables
$messages = array();
$input_array = array('firstname', 'username', 'email');

$db =& JFactory::getDBO();
// check the firstname, username & email
foreach ( $input_array as $v ) {
  $temp = JRequest::getString($v, '', 'post');
  if ( !$temp ) {
    $form->validation_errors[$v] = "You must include a value for the '$v' input";
    $return = false;
  }
  if ( $v != 'firstname' ) {
    $query = "
      SELECT COUNT(*)
        FROM `#__users`
        WHERE `$v` = ".$db->quote($temp)." AND `id` != $cf_user_id;
    ";
    $db->setQuery($query);
    $count = $db->loadResult();
    if ( $count ) {
      $form->validation_errors[$v] = "That $v is already in use";
      $return = false;
    }
  }
}

// Check the passwords
$password = JRequest::getString('password', '', 'post');
$password_confirm = JRequest::getString('password_confirm', '', 'post');
if ( $password && $password != $password_confirm ) {
  $form->validation_errors['password'] = "The Password and Verify Password entries don't match";
  $return = false;
}
return $return;
?>

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