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...
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With