简体   繁体   中英

how to get parsley.js to validate if one of the fields has number with a with a minimum value equal to 1

I have Adults, Children and Babies fields on my form. All these 3 fields have a default value which is 0. I am using parsley to validate the form. I need parsley to validate if at least one of the fields has a value greater than 0. It's should validate the form if one of the fields is bigger then 0. If not it should give an error when trying to submit. I used this example from the parsley offical website.

This is what I have so far:

    <input  type="text" name="nAdults" id="nAdults" value="0" data-parsley-group="block1" class="form-control " />

    <input  type="text" name="nChildren" id="nChildren" value="0" data-parsley-group="block2" class="form-control "/> 

    <input  type="text" name="nBabies" id="nBabies" value="0" data-parsley-group="block3" class="form-control " /> 

<script type="text/javascript">
            //parsley validate
            var form = $('#{{ $pageModule }}FormAjax'); 
            form.parsley().on('form:validate', function (formInstance) {
            var ok = formInstance.isValid({group: 'block1', force: true}) || formInstance.isValid({group: 'block2', force: true}) || formInstance.isValid({group: 'block3', force: true});
            $('.invalid-form-error-message').html(ok ? '' : 'You must fill at least one of the Adult, Child or FOC Fields!').toggleClass('filled', !ok);
            if (!ok)
            formInstance.validationResult = false;
            });
            //form submit
            form.on('submit', function(e) {
                var f = $(this);
                f.parsley().validate();
                if (f.parsley().isValid()) {
                    var options = { 
                        dataType:      'json', 
                        beforeSubmit :  showRequest,
                        success:       showResponse  
                    }  
                    $(this).ajaxSubmit(options); 
                    return false;
                }
                else    { 
                    return false; }
                    e.preventDefault();
            }); 
        </script>

I would appreciate if you can show me how to validate these 3 fields. When I write

data-parsley-min="1" it expects all the field to have a minimum value. But I need only one field to have minimum value "1".

you have to write custom validator. Here can you find a good example, how to do this.

my own working example (see console)

// bind event after form validation
$.listen('parsley:form:validated', function(ParsleyForm) {
// We only do this for specific forms
if (ParsleyForm.$element.attr('id') == 'myForm') {
    var combinations = ParsleyForm.$element.find('input')
    ,counter=0;
    $.each($('input'),function(){
        counter+=($(this).val());
    });
    if(counter>0) 
        ParsleyForm.validationResult = true;
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM