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:
How can I do the same in CF4?
//JeLu
<?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
hi,
Instead of returning a string, you should return "false" and add this line of code:
Regards,
Max
Instead of returning a string, you should return "false" and add this line of code:
$form->validation_errors[] = "Error here......";
Regards,
Max
Hi JeLu,
Here is Max's reply applied to your code example:
Bob
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
I get this error when copy and paste your code Greyhead!
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
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
Hi JeLu,
Sorry, I missed a ) on line 2 - fixed now.
This line is the Joomla! code to connect to the database.
Bob
Sorry, I missed a ) on line 2 - fixed now.
This line is the Joomla! code to connect to the database.
$db =& JFactory::getDBO();
Bob
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
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
I figured it out🙂
I put a "session to data" action after validation and it worked.
I love CF4🙂
JeLu
I put a "session to data" action after validation and it worked.
I love CF4🙂
JeLu
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
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
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
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
This topic is locked and no more replies can be posted.