Forums

[solved] CF V3.1 RC5.1 Server side Validation failed

Torty 09 Sep, 2009
Hello,
I use the Mootools validation for my form, but some user has JS disabled. For this case I want add a server-side validation to my form.
But it doesn't work.

My form has fields like 'street', 'phone', 'city' a.s.o.
My server validate-script looks like this:
<?php
    $errors = array();
    
    if(empty(JRequest::getVar('phone', null))) {
        $errors[] = 'error-msg for the field.';
    }    
    if(empty(JRequest::getVar('street', null))) {
        $errors[] = 'error-msg for the field.';
    }    
    if(empty(JRequest::getVar('city', null))) {
        $errors[] = 'error-msg for the field.';
    }
    if (sizeof($errors)) {
        $errors = implode('<br />', $errors);
        return $errors;
    }
?>

If I disable my JS and try to send the form, the follwing error is show'n:

Fatal error: Can't use function return value in write context in D:\xampp\htdocs\vv2k9\components\com_chronocontact\libraries\chronoform.php(244) : eval()'d code on line 4



Why this error comes and what can I do to fix this problem?

Regards,
Torty
GreyHead 09 Sep, 2009
Hi Toryt,

Line 4 is
if(empty(JRequest::getVar('phone', null))) {
So I guess that you can't use the JRequest expression inside the if clause, try
$phone = JRequest::getVar('phone', null);
if(!$phone) {

Bob
Torty 11 Sep, 2009
Hello GreyHead,

thx thats was the solution for *this* problem. But the server side validation (ssv) still works not right, because no errors will be shown if fields are empty.

i.e.: 'city' is empty, then the ssv have to return the error message, but the form was sended.

Where is the problem? Is ssv ignored if the MooTool-validation is aktive?
GreyHead 11 Sep, 2009
Hi torty,

It should stop processing if any value is returned. What is your server-side script now? And what is the value of $errors for an empty field?

Bob
Torty 11 Sep, 2009
My ssv-script is:
<?php
    $errors = array();
    
    $fname = JRequest::getVar('fname', null);
    if(empty($fname)) {
        $errors[] = 'Bitte geben Sie Ihren Namen an.';
    }
    
    $telefon = JRequest::getVar('telefon', null);
    if(empty($telefon)) {
        $errors[] = 'Bitte geben Sie eine Rückrufnummer an.';
    }
    
    $strasse = JRequest::getVar('strasse', null);
    if(empty($strasse)) {
        $errors[] = 'Bitte geben Sie Ihre Strasse an.';
    }
    
    $plz = JRequest::getVar('plz', null);
    $ort = JRequest::getVar('ort', null)
    if(empty($plz) or empty($ort)) {
        $errors[] =  'Bitte geben Sie Ihre PLZ + Wohnort an.';
    }
    
    $betreff = JRequest::getVar('betreff', null); 
    if(empty($betreff)) {
        $errors[] =  'Bitte geben Sie einen Betreff an.';
    }
    
    $nachricht = JRequest::getVar('nachricht', null);
    if(empty($nachricht)) {
        $errors[] =  'Bitte geben Sie eine Nachricht ein.';
    }
    
    if (sizeof($errors)) {
        $errors = implode('<br />', $errors);
        return $errors;
    }
?>


This code I have paste into the field "Server Side validation Code" on "Validation" tab. The field "On Submit code - before sending email:" on "FormCode"-tab is empty.

Thx 4 helping me.
GreyHead 11 Sep, 2009
Hi Torty,

That all looks good to me, I'll run a test later.

Bob
Torty 11 Sep, 2009
Hmmm ... I have used other validation in other forms.
After I change my validation script - it works.

The script is:
<?php
  // delete all not allowed parameters
  $allowed_vars = array('anrede', 'fname', 'telefon', 'strasse', 'plz', 'ort', 'betreff', 'nachricht');
  foreach (array_keys($_POST) as $var) {
      if (! in_array($var, $allowed_vars)) {
          unset($_POST[$var]);
      } 
  }
  
  // validate: not empty
  foreach ($allowed_vars as $var) {
      if (empty($_POST[$var])) {
          return 'Alle Pflichtfelder müssen ausgefüllt sein.';
      } 
  }
  
  // return nothing to mark submited values as valid
?>


Surprisingly this statement supply true then the field specified by $var is empty:
if (! $_POST[$var])
after I change it to
if (empty($_POST[$var]))
it works.

THX for your help.
Torty
This topic is locked and no more replies can be posted.