On Jun 14, 12:27 am, Jörn Zaefferer <[EMAIL PROTECTED]> wrote:
> R.RajeshJebaAnbiahwrote:
> > On Jun 13, 9:45 pm, Jörn Zaefferer <[EMAIL PROTECTED]> wrote:
> >> R.RajeshJebaAnbiahwrote:>    FWIW, it's no way related to the plugin, but 
> >> I have patched CakePHP
>
> >>> to dump the validation rules in JSON 
> >>> <https://trac.cakephp.org/ticket/2359>;
> >>> I think, this will help to have only one rule.
>
> >> Thanks. With that it shouldn't be difficult to dump rules that work with
> >> the validation plugin.
>
> >    The idea was impressed by validation plugin. But, I found that the
> > validation rules of CakePHP differs to the one of validation plugin;
> > with the JSON dumping, it would be easier to achieve the same result
> > in jQuery.
>
> I guess that scenario can be found at every serverside validation
> framework. Considering that it is impossible to provide a common
> denominator for all of those, an adapter seems to be the better idea.
> There should be always some kind of required-method.
   <snip>

    To be honest, having inspired by validation plugin, I thought of
hacking an adopter for it. But, later I found that it's too un-
intuitive to try to bend CakePHP rules for validation plugins; it will
be just easy to dump CakePHP rules in JSON and write a simple jQuery
code to do the validation. This way, one can just follow the CakePHP
validation code and translate it to jQuery easily. The simple code, I
came up with for CakePHP rules (not complete) is:
//Logic: Extract validation rule which is in JSON from class attribute
// on blur of input elements, validate--on error append error div
$('div.input').each(function () {
    var m = /validation:{(.*)}/.exec($(this).attr('class'));
    if (m[1]) {
        $('input', $(this)).blur(function () {
            var validation = eval('({' + m[1] + '})');
            $(this).siblings('div.error-message').remove();
            if (!validation['allowEmpty'] && !$(this).val()) {
                $(this).parent().append('<div class="error-message">'
+ validation['message'] + '</div>').fadeIn();
            }
        });
    }
});

--
  <?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com    Blog: http://rajeshanbiah.blogspot.com/

Reply via email to