C5 Form Validation

Permalink
With C5's built in Form block, it doesn't seem to validate the telephone for excluding letters. I can literally enter "abc" into the telephone field, and it doesn't validate.

Additionally, is there a QUICK way of adding a "name" type, or rather, a text input type that validates and checks that your name isn't including numbers etc?

MichaelG
 
jvansanten replied on at Permalink Reply
The validation helper might be of assistance here --http://www.concrete5.org/documentation/developers/forms/basic-valid...

It's not as sophisticated as other CMSes (whose validation routines are therefore heavier), but that may give you a framework to extend and add some regex validation.

Note that this is server-side validation.

Here's a link to a thread on jquery validation integrated into C5:http://www.concrete5.org/community/forums/customizing_c5/form_setup...
studio108 replied on at Permalink Reply
studio108
Thanks for the heads up on the validation issue MichealG. I foolishly assumed that if you have a telephone field it would have numeral only validation. I hope 5.7 might be an opportunity to address some basic validation options on the form block.
MichaelG replied on at Permalink Reply
MichaelG
I hacked away at this, and someone may be able to offer a better remedy, but here's what I did...

in file /concrete/core/controllers/blocks/form.php around line 288, I added the following (after the IF email statement):

if ($row['inputType'] == 'telephone') {
               if (!Loader::helper('validation/strings')->min($_POST['Question' . $row['msqID']], 7)) {
                  $errors['telephone'] = t('You must enter a valid phone number.');
               }
               elseif(!preg_match('(^((1)?[(-.\s]*\d{3}[)-.\s]*)?[-.\s]*\d{3}[-.\s]*\d{4}$)', $_POST['Question' . $row['msqID']])) {
                     $errors['telephone'] = t('You must enter a valid phone number.');                  
               }
            }


This checks that it's a minimum of 7 characters, then a regular expression to verify a phone number. The regex accepts: #'s, dashes, periods and parenthesis for the various number formats. You can change the regex as you wish if you need to account for country codes.
jasteele12 replied on at Permalink Reply 1 Attachment
jasteele12
You definitely don't want to overwrite the core files (unless you absolutely must), they will be lost if you upgrade concrete5.

Here's how I added a simple minimum number private function - you could easily add a reformatting function. It checks both use cases of required and not required, but something was entered.

First I copied concrete/blocks/form/controller.php to blocks/form/controller.php

Then extend the FormBlockController class by copying *just* the action_submit_form() from concrete/core/controllers/blocks/form.php

I've attached the file to this reply and you can see the code here (concrete5 telephone form validation): http://pastebin.com/4HDMDf0Q

Hope that helps,

John
Responsive replied on at Permalink Reply
Responsive
Hi John, Thanks for supplying this code as it solved the problem however it seems to create the below problem and Im not sure what is causing the blank fields in the email, are you free to check please
http://www.concrete5.org/community/forums/usage/critical-form-submi...

thanks
Rob