I can´t get this validation to work [Solved]

JeLu 21 Dec, 2008
I don´t want to let any user to add same value twice in a field and tries to use this server validation:
<?php
global $mainframe;
$database =& JFactory::getDBO();
$sql = "SELECT * FROM jos_chronoforms_mantorp WHERE text_12='JRequest::getVar('text_12')'";
$database->setQuery( $sql );
$result = $database->loadObject();
if($result){
return "Laget är redan registrerat!!!!";
}
?>


But it does not work. The form submit and the record get stored in database. If I try ...WHERE text12='a value that is in the database' it works. So I figuered out that I don´t get the submitted value from my form.
GreyHead 21 Dec, 2008
Hi Jelu,

I suspect that the code you have still returns an empty object so doesn't behave as you expected. Try this version:
<?php
$db =& JFactory::getDBO();
$text =& JRequest::getString('text_12', '', 'post');
$sql = "
    SELECT COUNT(*)
        FROM #__chronoforms_mantorp
        WHERE coupon = ".$db->quote($text).";";
if ( $debug ) echo "sql: ".print_r($sql, true)."<br /><br />";
$db->setQuery( $sql );
if ( $database->loadResult() != 0 ) {
    return "Laget är redan registrerat!!!!";
}
?>
NB Not tested and may need debugging

Bob

Later: corrected the WHERE line and remove an uneccessary $sql= line
JeLu 21 Dec, 2008
I have tested it a little bit but it does not work. Should I have 2 sql-statemenets? And where does coupun come from? Perhaps I can change that to text_12?. It does Not work anyway I tested it. But when I use a named team eg eRace it works and I have an errormessage "Laget är redan registrerat!!!!". (Laget = The Team) Swedish.

I will test a little bit more but is there a way to see what value that the WHERE-claus use.

Sorry if this is newbie questions but I am still learning.
I found Debug feature and have this error:

Parse error: syntax error, unexpected T_STRING in /home/skrotbil/public_html/components/com_chronocontact/chronocontact.php(190) : eval()'d code on line 10
_POST: Array ( [text_4] => My Name[text_9] => Street 15 [text_10] => 11111 [text_7] => City [text_12] => eRace [text_0] => [email]my@online.com[/email] [chrono_verification] => mjYQe [hidden_0] => 0 [a634938e329905ee152959b08bf15458] => 1 )



Everything in Debug says it is OK otherwise.

I don´t know if this says anything for somebody, for me it does NOT.

Regards

JeLu

and P.S. Thanks for your Replay Geryhead D.S
GreyHead 21 Dec, 2008
Hi jelu,

Sorry, there were at least) two mistakes in there. I left in the second $sql = . . . and I missed some quotes off the end of the sql.

I've corrected it (I hope) and added a debug line to print out the sql for you.

Bob
JeLu 21 Dec, 2008
Now it works like a charm and I have learned a new thing today :mrgreen:

Thank you very much

And A Very Merry Christmas to You Greyhead and also to Max for exellent suport at this forum.

And A Happy New Year 😀
JeLu 21 Dec, 2008
I spook to soon. It works as long as you have a teamname without spaces. If I name it eRace it works but if I name it e Race it does not. I will try to test and will come back here if I find a sulotion.
GreyHead 21 Dec, 2008
Hi JeLu,

Name what 'e Race'? You can't have a space in a field-name??

Bob
JeLu 21 Dec, 2008
No it´s not the fieldname it is the value that is entered in the form.
JeLu 21 Dec, 2008
Don´t know what happend but now it seems to work.
GreyHead 22 Dec, 2008
Hi JeLu,

:-) sometime you get lucky!

There should be no problem with spaces in the name entered in the form.

Bob
GreyHead 24 Jan, 2009
Hi jacks88,

True - the alpha validation allows [a-zA-Z] no spaces, you'll need to hack the regexp in the validation file if you need to allow spaces (or skip the validation).

Bob
jacks88 25 Jan, 2009
Thank you. I'll work around it.
Great component, by the way.
This topic is locked and no more replies can be posted.