Jquery for php translator?

I have a set of form validation rules that I wrote using the jquery validator . Since I need to repeat the same check on the server side, I thought it would be nice not 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 depend on other fields or have calculated values.

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

An example of some 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... } 
+6
jquery php validation
source share
4 answers

I think you are on the right track, but I do not think that you will find it as easily as you expect. I do not know a pre-built function that could accurately analyze and reform the data set for this specific purpose.

If I was going to do this, I would form my test set in a multidimensional linked array that contained the necessary information in PHP.

After that, you will have to decide how to talk with JavaScript. If you load JS inline on the page, I would just use PHP to scroll through the PHP array and create your JS rules if necessary. If you use external function calls, then JSON encodes the array and passes it to the translator function, which parses the JSON array and reformatts it to what your jQuery expects.

EDIT TO COMPLETE:

You can go in another direction and move from JS to PHP, but by doing this, I offer you the ability to easily change other JS or JQuery libraries and just change your translator. If you switch from JS to PHP, you will have to change the BOTH side if you ever decide to change the JS validators. If you plan to make several websites or multi-program programming, it would be much better to just study the general configuration of the PHP array and just write translators for JS.

+3
source share

I would recommend doing the opposite: convert some PHP-based validation functions to Javascript. Automatically converted functions will not be as flexible and therefore as versatile or strong as manual encoding functions, and you really want everything to be better on the server side. In the worst case, if you fill out some PHP-> JS checks, it is that the user gets a return trip to the server before they are informed that their email address is incorrect.

+1
source share

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

How about this approach:

  • Create a configuration for which fields should be in the form, including validation.
  • Create a PHP function that generates a html + javascript form, including configuration-based validation.
  • Create a PHP function that validates form input based on configuration.
+1
source share

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

In your php code that receives the submit form, you just need to pass the field value of your validation function and check if they are true or false.

Internal validation is important, as javascript-based validation can be easily disabled.

0
source share

All Articles