Forums

Custom Server Side Validation

elehost 13 Jun, 2011
Hello,

I am trying to convert a form from v3 to v4 that has some custom php server code at the validation level.

I have added the necessary element to the new form (Custom Server Side Validation) and I am having trouble getting the custom code validation working. The auto validation part (as a different element) is working fine.

Would you please post some sample code that should work within the "Custom Server Side Validation" section?

I have tried a number of things and it seems to not be working so I want to go back to the basics and then work from there.

Any tips you can provide would be much appreciated.

In the old version v3 version I was including files, had a number of functions and some analysis of the form submission as well as storing some post variables for later emailing.

Thanks,

Paul
GreyHead 14 Jun, 2011
Hi Paul,

Did you check the Help tab?

[list]

  • You should use PHP code with php tags.

  • Returning "boolean" false will fail, anything else or no return at all will lead to success.

  • Set fields errors by adding a new key => value entry to the $form->validation_errors array, where "key" is the "field name" and "value" is the "Error message", for example, if you want to set an error to the "email" field you should use this code
    $form->validation_errors['email'] = "Email error message is here.";
    .
  • [/list]



    In CFv4 the form data is stored in a $form->data array so you can either get values from this, or from the Joomla! JRequest method we used for CFv3 - but any changed values must be written back to the $form->data array.

    The validation needs to return 'false' to flag an error (in CFv4 they needed to return a message).
    <?php
    $error_count = 0;
    if ( $form->data['some_input_name'] == '' ) {
      $form->validation_errors['some_input_name'] = 'Some input name can\'t be empty';
      $error_count++;
    } elseif ( $form->data['some_input_name'] == 'bananas' ) {
      $form->validation_errors['some_input_name'] = 'Bananas is not a valid entry';
      $error_count++;
    }
    if ( $form->data['some_other_input_name'] < 1 || $form->data['some_other_input_name'] > 4) {
      $form->validation_errors['some_other_input_name'] = 'Some other input name must be between 1 and 4';
      $error_count++;
    }
    if ( $error_count ) {
      return false;
    }
    ?>

    Bob
    elehost 14 Jun, 2011
    Thanks Bob! That was exactly what I needed.

    I see that an event loop is required for the fail action to return false (On Load / Quit). This took a bit of tweaking to figure out but I now have gnuPGP "integrated" with Chronoforms v4.

    Thanks again for your help...

    Paul
    GreyHead 24 Jun, 2011
    Hi Paul,

    I just read this - a little late.

    Can you tell me a bit more about your gnuPGP integration? That might be the basis for a useful plugin.

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