views:

123

answers:

4

I've got a set of form validation rules that I wrote with the jquery validator plugin. Since I have to repeat the same validation on the server side, I thought it would be nice not to have to rewrite my rules in PHP. If the rules were simple field=>rulename=>value I could just store them as JSON and decode them into PHP arrays. Unfortunately, some of the rules are dependent on other fields or have calculated values.

Is there a generic way to translate field=>rulename=>value=function{} from javascript/jquery to PHP? Or, is there a way to run the jquery on the server side too and then pass the results to PHP?

A sample of some of the rules:

        rules: {
            title: {required:true, minlength:5},
            description: {required:true, minlength:5},
            event_type_id: "required",
            ev_start: { dateCan: true, required: true},
            ev_end:{ dateCan: true,
                     minDate: "input[name='ev_start']"
            },
            ev_starttime:{
                required: 
                    function(element){
                        return $("input[name='allday']:unchecked")
                    },
                time: true,
                maxTime: {
                    depends: function(element) {
                                return $("input[name='ev_endtime']:filled")
                                       && $("input[name='ev_start']").valid()   
                                       && $("input[name='ev_end']").valid()
                                       && $("input[name='ev_start']").val()==$("input[name='ev_end']").val()
                            },
                    param: "input[name='ev_endtime']"
                }
            },
       //etc...
       }
+3  A: 

I think you are on the right path, but I don't think you are going to find it as easy as you expect. I am not aware of a pre-built function that could accurately parse and reform the data set for this specific purpose.

If I were going to do this I would form my validation set into a multidimensional associated array that held the necessary information in PHP.

After that you are then going to have to decide how to talk to JavaScript. If you are loading your JS inline in the page I would just use PHP to loop through the PHP array and construct your JS rules as necessary. If you are using external function calls then JSON encode the array and pass it to a translator function that parses the JSON array and reformats it to what your JQuery expects.

EDIT TO CLARIFY:

You could go the other direction and pass from JS to PHP, but doing it the way I suggest will give you the ability to swap out other JS or JQuery libraries easily and just change your translator. Going from JS to PHP you would have to change BOTH sides if you ever decide to change JS validators. If you plan on doing multi website or multi developer coding it would be much better to just learn a common PHP array configuration and just write translators for JS.

angryCodeMonkey
I suppose I could write the rules in PHP and then parse them into valid javascript, but I'd really rather go in the other direction.
dnagirl
@dnagirl - My only concern there would be that it is more difficult to pass information FROM JavaScript INTO PHP since it mostly uses a pull system, you will have to request the data with PHP. That may work in this instance but for a more universal approach I would personally do it the other direction. Imagine 6 months from now you decide to use a newer validator, you don't have to change the whole game you just change the JS translator... PHP isn't going to change that drastically =P
angryCodeMonkey
@Shane- good point about the validator being more likely to change than PHP.
dnagirl
A: 

Since the validation rules is quite simple, you can write your own and save it as a function. Just make it accept a value in the parameter then return true or false.

In your php code that receive form submit, you will just need to pass the fields value to your validation function, and check if it result true or false.

Validating in back-end side is important, since the javascript based validation can be turned off easily.

Donny Kurnia
+1  A: 

I would recommend doing the opposite: convert some PHP-based validation functions to Javascript ones. The automatically converted functions aren't going to be as flexible, and hence as versatile or strong as the hand-coded functions, and you really want the best to be on the server-side. The worst case scenario if you stuff up some PHP->JS validation is that the user gets a round trip to the server before they're told that their email address is wrong.

nickf
@nickf - While this is possible please check out the edit to my response for the reasoning why I would avoid doing so. It is very limiting and doesn't scale well.
angryCodeMonkey
A: 

I think that what you are trying to do (javascript to php code conversion) is not very systematic and I doubt you will find ready solution.

What about this approach:

  1. Create some configuration for what fields should be in the form including validation.
  2. Create a PHP function that generates html+javascript form including validation based on the configuration.
  3. Create a PHP function that validates form input based on the configuration.
Josef Sábl