开发者

jquery to php translator?

开发者 https://www.devze.com 2022-12-19 17:51 出处:网络
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

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']"
            },
            开发者_如何学Pythonev_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...
       }


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号