newhck / php-form-builder-class

Automatically exported from code.google.com/p/php-form-builder-class
GNU General Public License v3.0
0 stars 0 forks source link

Allow the user to specify custom validation on fields #47

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
We should allow the user to validate fields for specific content, we could
have in the $additionalParams a new validate variable that can be set.

We could use the filter types that php make available:
http://www.php.net/manual/en/filter.filters.validate.php

Then the user could do something like:

$addTextbox("Validate me" , "validate" , "" , Array(
"validate" => Array( FILTER_VALIDATE_REGEXP , Array( "regexp" => "/^M(.*)/"
) ), "errorMsg" => 'failed to pass validation')
);

Although this is terribly ugly.

We could only allow for regex validation/callback function, and just have
more things like addEmail()

Original issue reported on code.google.com by moncojhr@gmail.com on 27 May 2010 at 6:17

GoogleCodeExporter commented 8 years ago
Thanks moncojhr, what I really did was to verify two form fields that must have 
equal value, not type validation.

Thanks

Original comment by geodavis...@gmail.com on 6 Aug 2010 at 8:09

GoogleCodeExporter commented 8 years ago
Is it already possible to use a regex? 
I could not find any documentation about it. 

Original comment by sven.lar...@gmail.com on 15 Nov 2010 at 3:09

GoogleCodeExporter commented 8 years ago
Maybe you could extend the field prototype to allow the user to pass a 
validation array after the options. Then you would create a simple validation 
script that would reference a custom library by looking up the "fieldType" and 
determining if it is in the acceptable format. If it is, it calls the success 
function, if not, it calls the error function. Timeout could also be added 
simply by adding the timeout time to the validate array. Then the user would be 
responsible for evaluating the errors and reformatting/rejecting the values. 
The validate function would be like a simple "filter" to determine if a second 
level of error handling was in order.

Presumably, you could validate automatically somehow using the syntax:
$form->addEmail("enter e-mail address",
                "emailField",
                "",
                 Array(),
                 Array("lib"=>rulesurl,
                       "fieldtype"=>"email",
                       "success"=>successfunc,
                       "error"=>errorfunc,
                       "altformat"=>"userNameOnly",
                       "timeout"=>30));

and the file located at rulesurl could contain something like

<fieldtype name="email">
     <validate><![CDATA[<script>
                       function (val) { //validates and returns value if valid
                                          or error reference if invalid
                                      }
                       </script>]]></validate></fieldtype>

If you were to be really crazy about it, you could have the library contain 
formatting rules as well so that when the field passed the validation function, 
it returned a correctly formatted value (that would be different from the input 
value, but more appropriate for the field (i.e. email addresses would be all 
lowercase, for example). 

Original comment by jasein...@gmail.com on 6 Jan 2011 at 2:27

GoogleCodeExporter commented 8 years ago
Regular expression validation is included in the 2.0 version release.  Also, 
it's much more feasible to extend the Validation abstract class now compared to 
the 1.x branch.

- Andrew

Original comment by ajporterfield@gmail.com on 14 May 2011 at 3:23

GoogleCodeExporter commented 8 years ago

Original comment by ajporterfield@gmail.com on 14 May 2011 at 3:36