简体   繁体   中英

BootstrapValidator skip validation if empty

I have these rules here for example:

ducationStartYear: {
                validators: {
                    numeric: {
                        message: 'The year must be numeric'
                    },
                    stringLength: {
                        min: 4,
                        max: 4,
                        message: 'The year must be exact 4 characters long'
                    }
                }
            },
            EducationEndYear: {
                validators: {
                    numeric: {
                        message: 'The year must be numeric'
                    },
                    stringLength: {
                        min: 4,
                        max: 4,
                        message: 'The year must be exact 4 characters long'
                    }
                }
            },
            QualificationMonth: {
                validators: {
                    numeric: {
                        message: 'The month must be numeric'
                    },
                    stringLength: {
                        min: 2,
                        max: 2,
                        message: 'month year must be exact 2 numbers long'
                    }
                }
            },
            QualificationYear: {
                validators: {
                    numeric: {
                        message: 'The year must be numeric'
                    },
                    stringLength: {
                        min: 4,
                        max: 4,
                        message: 'The year must be exact 4 numbers long'
                    }
                }
            },
            JobStartDate: {
                validators: {
                    date: {
                        format: 'DD/MM/YYYY',
                        max: 'JobEndDate',
                        message: 'The date is not valid'
                    }
                }
            },
            JobEndDate: {
                validators: {
                    date: {
                        format: 'DD/MM/YYYY',
                        min: 'JobStartDate',
                        message: 'The date is not valid'
                    }
                }
            },

Fieldset:

 <fieldset class="form-group">
                <label for="Surname">Month / Year</label>
                <div class="input-group">
                    <?php echo "<input type='text' name='QualificationMonth' class='form-control' placeholder=''value=".$Submission['QualificationMonth'].">" ;?>
                    <span class="input-group-addon">-</span>
                    <?php echo "<input type='text' name='QualificationYear' class='form-control' placeholder=''value=".$Submission['QualificationYear'].">" ;?>
                </div>
            </fieldset>

As you can see, I have not set them to be required. But the validator alarms if left empty. I would like the validator to skip those field if they left empty. But if the user type something to have a look on the input. How's that possible?

EDIT: I got a bit deeper and the real issue is that, when I insert record to the database using the same form, if the textbox is empty it sends to database a value of 0. Im using the same form to fetch the details back to the form and allow modifications. When I fetch the inserted detail on the form, those specific textboxes appear with 0's. This is when the valitations alarm.

So the issue is in the creating the row in the database.

Solved: I just put an if statement to make the '' variables to NULL, so when i fetch the details back to the form, the field is empty and not 0.

解决:我只是放置了一个if语句,以使''变量为NULL,所以当我将详细信息取回表单时,该字段为空而不是0。

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