How to check a form field with a table column(Validation)

hoomijoon 23 Jun, 2014
How can we check a form field with a database table column. For example I want to restrict registration of a person if he registered before. I want ChronoForm to check NationalID that person entered with all NationalIDs in database and show a mesaage.

Please Help
GreyHead 23 Jun, 2014
Hi hoomijoon,

You can do this after the form is submitted using custom Serverside Validation (you use the Event Switcher action for this in CFv5). I would add a simple MySQL query there to check for an existing record.

It's more complicated but you could also use JavaScript and AJAX to add a Client-side validation as well (note that this should be in addition to the server side check).

Bob
hoomijoon 24 Jun, 2014
Thanks Bob

I used this code in Event Switcher:
<?php
$db =& JFactory::getDBO();
$query = "
SELECT `id`
FROM `sm25o_chronoengine_chronoforms_datatable_register`
WHERE `nationalcode` = '0081501277' ;
";
$db->setQuery($query);
$options = $db->loadAssocList();
if ($options) {
echo 'fail';
}else{
echo 'success';
}
?>

But I get this ERROR:

Strict Standards: Only variables should be assigned by reference in /var/www/html/administrator/components/com_chronoforms5/chronoforms/actions/event_switcher/event_switcher.php(26) : eval()'d code on line 2 fail
GreyHead 24 Jun, 2014
Hi hoomijoon,

Please take the & out of this line in Joomla! 3 $db =& JFactory::getDBO();

And I wouldn't advise running any site with Strict Standards reporting on :-(

Bob
hoomijoon 24 Jun, 2014
Thank you so much...
How can I replace '0081501277' in this code with a form value that user entered?
GreyHead 24 Jun, 2014
Answer
Hi hoomijoon,

Yes, like this
WHERE `nationalcode` = '{$form->data['input_name']}' ;
You may also want to add some validation to make sure that the input has a value set before running the query.

Bob
hoomijoon 25 Jun, 2014
Thanks Bob,
but another problem: as you see in my code I used echo to determine which action should be performed after form submition. but program just echos: fail or success on the screen. I used a 'display message' block in fail event !
hoomijoon 25 Jun, 2014
I thnk I made it Bob. I changed 'echo' to 'return'.
Thanks a lot...
GreyHead 25 Jun, 2014
Hi hoomijoon,

Ah, that would help ;-) I didn't see that one.

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