Forums

Server side validation?

JeLu 27 Apr, 2012
I have an old form that I want to upgrade to CF4. In the old form there is a server side validation on one field and the code is like this:
<?php 
$pnr  = JRequest::getString('pnr', '', 'post'); 
//if (!$pnr) { 
if ( !preg_match("/^\d{6}\-\d{4}$/", $pnr) ) {
return "Ett personnummer saknas eller är inte inskrivet på rätt sätt. Använd formen XXXXXX-XXXX"; 
} 
$db =& JFactory::getDBO(); 
$query = " 
SELECT count(*) 
FROM `#__elevfiler` 
WHERE `pnr` = ".$db->quote($pnr)."; 
"; 
$db->setQuery($query); 
$in_use = $db->loadResult(); 
if ( $in_use) { 
return "Personnumret används redan! Använd \"Ändra Elevfil\" istället om det är din elev. I annat fall tar du kontakt med Global Education på elevfil@arbetamedtruck.se"; 
} 
?>

How can I do the same in CF4?

//JeLu
Max_admin 27 Apr, 2012
hi,

Instead of returning a string, you should return "false" and add this line of code:

$form->validation_errors[] = "Error here......";


Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
GreyHead 28 Apr, 2012
Hi JeLu,

Here is Max's reply applied to your code example:
<?php
$pnr = '';
if ( isset($form->data['pnr']) ) {
  $pnr = $form->data['pnr'];
}
if ( !preg_match("/^\d{6}\-\d{4}$/", $pnr) ) {
  $form->validation_errors[] = "Ett personnummer saknas eller är inte inskrivet på rätt sätt. Använd formen XXXXXX-XXXX";
  return false;
}
$db =& JFactory::getDBO();
$query = "
  SELECT count(*)
    FROM `#__elevfiler`
    WHERE `pnr` = ".$db->quote($pnr).";
";
$db->setQuery($query);
$in_use = $db->loadResult();
if ( $in_use ) {
  $form->validation_errors[] = "Personnumret används redan! Använd \"Ändra Elevfil\" istället om det är din elev. I annat fall tar du kontakt med Global Education på elevfil@arbetamedtruck.se";
  return false;
}
?>

Bob
JeLu 29 Apr, 2012
I get this error when copy and paste your code Greyhead!
in /my_server_path/administrator/components/com_chronoforms/form_actions/custom_serverside_validation/custom_serverside_validation.php(18) : eval()'d code on line 3
 


Do I need to connect to table in DB first or wouldn´t be abke to run thru any table in the DB?

Regards

JeL
GreyHead 29 Apr, 2012
Hi JeLu,

Sorry, I missed a ) on line 2 - fixed now.

This line is the Joomla! code to connect to the database.
$db =& JFactory::getDBO();


Bob
JeLu 30 Apr, 2012
Hi!

I noticed the missing ) and fixed that. I have used "Action" Custom Server Side Validation and put the code in it. The Error messages that is in the Code will not show but if I put something in "OnFail" or in "OnSuccess" I can see that the validation seems to work.

In my Custom code I have actually 2 validations. First is it in format xxxxxx-xxxx and not empty. For the second Validation it checks the table for already entered value. I have for this two different errormessages.

How do I do that and use "OnFail" fauction?

JeLu
JeLu 30 Apr, 2012
I figured it out🙂

I put a "session to data" action after validation and it worked.

I love CF4🙂

JeLu
JeLu 30 Apr, 2012
And a show HTML action in on fail
GreyHead 30 Apr, 2012
Hi JeLu,

Assuming that you want to re-load the form when there is an error you need to drag an Event Loop action into the pink On Fail event. The default settings are OK.

Without an added action the On Fail event does nothing.

Bob
JeLu 04 May, 2012
EvventLoop action worked fine. Another question is if I can use Auto Server Validation together with above used Custom server Validation?

I now that i can use validation for the fields by check e.g. required but it would be nice to do all validation Server Side.

//JeLu
GreyHead 04 May, 2012
Hi JeLU,

Yes, you can use both together. At the moment I can't see a simple way of running both at the same time. If the first one fails then the form will re-load without checking the second. That's a nuisance rather than a big problem.

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