proengsoft / laravel-jsvalidation

Laravel Javascript Validation
MIT License
1.13k stars 175 forks source link

How to exclude a rule? #168

Closed Lescazel closed 8 years ago

Lescazel commented 8 years ago

Actualy I use FormRequest to validate my whole form. For my Captcha-field I use this: 'captcha' => 'required|captcha'

Unfortunately it seems not to be possible to check a Captcha via JQuery. So I want to exclude the field from the JSValidation. As I read in the docs, I added no_jsvalidation to my rule: 'captcha' => 'required|captcha|no_jsvalidation'.

But now I get the following error: BadMethodCallException in Validator.php line 2694: Method [validateNoJsValidation] does not exist.

Has anyone an idea?

mikifus commented 8 years ago

Same problem here. I found this thread saying that upgrading was enough, but I didn't manage to make it work: https://github.com/proengsoft/laravel-jsvalidation/issues/149

I have a rule for a file which is already being validated by another library so I want to exclude its rule.

I made a workaround by adding this to the rules() method in my Request. Hope it helps!

Validator::extend('no_js_validation', function($attribute, $value, $parameters, $validator) {
            return true;
});
...
return [
            'file'         => 'required|...|no_js_validation'
];
...
antonkomarev commented 8 years ago

You can define special attribute in js which will tell frontend validator to not validate a field. And it will be easily configurable using HTML then.

resources/views/vendor/jsvalidation/bootstrap.php

<?php if (isset($validator['ignore']) && is_string($validator['ignore'])): ?>
    ignore: "<?= $validator['ignore'] ?>",
<?php else : ?>
    ignore: '.no-validate',
<?php endif; ?>

This works for me.

Lescazel commented 8 years ago

Thank you very much for your answers.

At the time I solved my problem via a custom validation rule, similar to the solution of mikifus.

For other reasons I use the master branch of JSValidation now, so I've just tried to remove the custom validator and it still works! Thank you!