Page 1 of 1

FormBuilder UDT validation boilerplate code

Posted: Mon Jan 27, 2014 3:09 pm
by boonier
Hi

Is there a starting block UDT that validates submitted form elements that I can use as a basis for mine? I'm having a bit of of trouble getting my UDT going. I'm getting the $params in ok - do I just reference all the fields individually and return whether this field validates? Or better, I read that there is a validate() function - can I call this and extend with my own bits?

The reason I'm using the UDT is a) I'm implementing the honeypot anti spam technique and b) checking that an 'Other info' textfield has a value based on whether an 'Other' checkbox is checked or not (it becomes required if checked). Obviously all other fields need validating at th same time.

I want to get the basic server side validation right first before using client side JS validation plugins

any help much appreciated

thanks

Re: FormBuilder UDT validation boilerplate code

Posted: Mon Jan 27, 2014 9:44 pm
by velden
It's explained in the help of FormBuilder:
Validation UDT. Set this for a form, and the UDT will receive all of the form's human-readable results. The UDT should do whatever validation it wants, and return an array with the first value being true or false (indication whether the form validates), and the second value being error messages (if any).
So I think it's an error state for the whole form and it won't be easy to highlight the specific field(s).
Maybe you can add a small css block in the error message field and use that to highlight the specific fields. Never used it though.

Not tested example:

Code: Select all

// do your checking here and make sure you set $errors to true or false

if ($errors) {
  return Array(false, '<strong>You need to fill in field X</strong><style type="text/css">.conditional input {border:1px solid red;}</style>');
} else {return Array(true,'');}
Then give the specific fields the CSS class 'conditional' from the field's Advanced Settings in FormBuilder.

Re: FormBuilder UDT validation boilerplate code

Posted: Mon Jan 27, 2014 11:22 pm
by boonier
Thank you for the response Velden. Yes I read that entry in the docs and it never did quite sink in. I was getting the values in and running conditional checks on them but getting unstuck at sending them back.

As I understand it, if the form is passed to the UDT, then the default validation can never occur and one has to implement their own in place of it, right? It's not clear what the return array consists of and whether it can hold data for each field. More digging around I guess...

I think for the time being I'm going to have to reply on the front end JS validation as I'm running out of time on this...Not ideal, but I'll revisit it I think.

thanks again