validation_errors['age'] = "I'm sorry you must be over 64 to submit this form."; $valid = false;}return $valid;?>Thanks for any help. Mike"> Validation Alternative - Forums

Forums

Validation Alternative

nassausky 13 Feb, 2012
Hi Bob,

I see there are posts about validation but I couldn't find one that went into maybe a basic question.

Let's say I want to get the value submitted from a form textbox and if the value doesn't equal something of my choice, have it bounce back. I just couldn't get a grasp on anything anyone submitted as questions in the forum.

Let's say a textbox fieldname is age and I want someone to only respond in a certain range for example over 64. My guess is that I would probably put the code in the block "Custom Code - Before Email(s)" something like this but how would I make it return with the validation error and not submit it until corrections are made.

<?php
$valid = true;
$owner_age=JRequest::getString('age', '', 'post');
if ( $owner_age>64 ) {
  $form->validation_errors['age'] = "I'm sorry you must be over 64 to submit this form.";
  $valid = false;
}
return $valid;
?>



Thanks for any help.
Mike
nassausky 13 Feb, 2012
OK I think I got it after going over this post http://www.chronoengine.com/forums.html?cont=posts&f=26&t=66257 a few times.

Dragging the custom server side validation as the first event after submit (as displayed in the attached image) [attachment=0]Chrono4EventCheckerCustomValid.jpg[/attachment]and then posting the code:

<?php
$valid = true;
$owner_age=trim(JRequest::getString('age', '', 'post'));
if ( $owner_age <65 ) {
  $form->validation_errors['age'] = "I'm sorry we only accept users over 64 in age.";
  $form->data['age'] = '';
  $valid = false;
}
return $valid;
?>

I hope this helps someone else.

Thanks Bob!
GreyHead 13 Feb, 2012
Hi Mike,

This looks good to me.

Bob

One tiny comment. In CFv4 it's better to get into the habit of accessing the form data from the $form->data array rather than using JRequest. In this case it makes no difference but in more complex processes where the data is being manipulated the updated results are only found in $form->data.
nassausky 13 Feb, 2012
I could have sworn I tried that last night and for some reason it didn't work. I will try that out a little later again with modifying those references. Thanks again for the tip. They definitely help out!
This topic is locked and no more replies can be posted.