jeudi 26 novembre 2015

plan to implement server-side field validation modeling after jQuery .validate()

The objective of this question is to ask if something similar has been created, as well as get feedback on the approach.

I have used the jQuery .validate() plugin (http://ift.tt/18Dl45M) for some time and conceptually it's elegant and easily understandable. I like that you can declare the rules by css class or solely by the object you pass to the .validate( ) method.

One thing that our code base is weak on on the receiving end is validation. I.e., we are depending on JS to validate our forms in granular detail (date of enrollment, which degree you got in college), but may only check for key elements (name and email) on the server end.

This is problematic from the obvious standpoint that error checking and testing requires a physical person entering data and pushing a submit button.

But obviously keeping up with error checking on both the client and server code is an invitation to get out of sync!

My thought is, why not unify the two? The JSON arrays shown in the documentation could easily be expressed as a PHP array - so that all I'd need to do is something like this:

$validation=array(
    'rules'=>array(
        'name'=>'required',
        'email'=>array(
            'required'=>true,
            'email'=>true,
        ),
    ),
    'messages'=>array(
        'name'=>'Please specify your name',
        'email'=>array(
            'required'=>'We need your email address, dummy!',
            'email'=>'your email needs to actually be an email!',
        ),
    ),
);

and then on the client side, instead of this:

<script>$('#myform').validate({..array..})</script>

Just write this:

<script><?php
$validate=new Validate();
$validate->outputJSValidation('myform'); //where myform would be declared somewhere
?></script>

Where the second code would output exactly the same as the first snipped.

The real objective is to be able to do form testing and unit testing via an API or automated script, as well as double verify.

Have you seen this approach taken before? Also what are your thoughts on this idea?

Aucun commentaire:

Enregistrer un commentaire