Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsley JS 2.x - disabling validation for fields that are not visible

I want to have Parsley not validate an input if it is not visible. I have a large multi-step survey that reveals new sections as you complete it. Each section is in its own tag, and within each form I can have multiple sections that reveal as you go through it. I am currently using 1.x, and this is how I do it now:

$('#' + formID).parsley({
            errors : {
                container: function(element) {
                    var $container = element.parent().find('parsley-container');
                    if($container.length === 0)
                    {
                        if($(element).hasClass('text-hide') && !$(element).hasClass('not-select'))
                        {
                            $(element).prev().addClass('is-error'); 
                        }
                        else
                        {
                            $container = $('<div class="parsley-container"></div>').insertAfter(element);
                        }
                    }
                    return $container;
                }
            },
            listeners: {
                onFieldValidate: function(elem) {
                    if(!$(elem).is(':visible'))
                    {
                        return true;
                    }
                    return false;
                }
            }

It is in the listeners section that I only validate fields that are visible. How can I do this in Parsley 2.x? I've been going through the documentation and I can't find an equivalent way of doing it. I know there are validation groups in 2.x, but is there a way to just do it like I did in 1.x?

like image 808
k_mada Avatar asked Jul 02 '14 18:07

k_mada


People also ask

How do I validate my form with parsley?

No need to write even a single JavaScript line for simple form validation. Parsley is now smarter, it automatically detects your forms' modifications and adapts its validation accordingly. Simply add, remove or edit fields, Parsley validation will follow! Parsley is shipped with more than a dozen useful validators.

What are the common errors in parsley?

One error at the time: constraints are prioritized in Parsley, and if several of them are not met for a field on validation, only show the most important one. Quick error removal: when a field is detected and shown as invalid, further checks are done on each keypress to try to quickly remove error messages once the field is ok.

What's wrong with Min Char validation in parsley?

Min char validation: Parsley by default does not proceed with field validation when less than 3 chars have been input. Do not assault your users with error messages too soon! One error at the time: constraints are prioritized in Parsley, and if several of them are not met for a field on validation, only show the most important one.

Do I need to write a JavaScript line for form validation?

No need to write even a single JavaScript line for simple form validation. Parsley is now smarter, it automatically detects your forms' modifications and adapts its validation accordingly. Simply add, remove or edit fields, Parsley validation will follow!


1 Answers

The easiest solution is to exclude all the hidden inputs, like this:

$('#' + formID).parsley({
    excluded: "input[type=button], input[type=submit], input[type=reset], input[type=hidden], input:hidden"
});

This way the validation will only bind the visible inputs. However, this forces you to destroy and apply Parsley each time a input changes its visibility.

To avoid this you can use the following «not so elegant» solution. This code would be better in the 'parsley:field:validate' event, but I couldn't make it work.

Using the 'parsley:field:validated', the validation was already performed and now we change the validation result to true and hide the error container.

$('#' + formID).parsley();

$.listen('parsley:field:validated', function(fieldInstance){
    if (fieldInstance.$element.is(":hidden")) {
        // hide the message wrapper
        fieldInstance._ui.$errorsWrapper.css('display', 'none');
        // set validation result to true
        fieldInstance.validationResult = true;
        return true;
    }
});

As of Parsley 2.1.* the event parsley:field:validated throws the following message

Parsley's pubsub module is deprecated; use the corresponding jQuery event method instead

Instead of parsley:field:validated you should use the event field:validated


The latest versions have $.listen() deprecated. You should use Parsley.on() instead. Example:

Parsley.on('field:validated', function(fieldInstance){
    if (fieldInstance.$element.is(":hidden")) {
        // hide the message wrapper
        fieldInstance._ui.$errorsWrapper.css('display', 'none');
        // set validation result to true
        fieldInstance.validationResult = true;
        return true;
    }
});
like image 199
Luís Cruz Avatar answered Sep 25 '22 20:09

Luís Cruz