简体   繁体   English

语义UI表单验证怪癖

[英]Semantic-UI Form Validation Quirk

I'm using Semantic-UI with React this time. 这次我将Semantic-UI与React一起使用。

I've used Semantic-UI's form and form validation in other products in the past and I've never had problems such as the one I am going to explain now. 过去,我在其他产品中使用过Semantic-UI的表单和表单验证,但是我从来没有遇到过诸如现在要解释的问题。

In the past when I used Semantic-UI's form validation, when I set the on property to change , the fields only show the inline error message if there is a change in that field. 过去,当我使用Semantic-UI的表单验证时,将on属性设置为change ,如果该字段中有change ,则这些字段仅显示内联错误消息。 However, now for some reason, editing on field could possibly trigger all fields to validate and show all of the inline errors if those fields are either empty or do not contain the correct value. 但是,由于某些原因,如果这些字段为空或不包含正确的值,则对字段进行编辑可能会触发所有字段进行验证并显示所有内联错误。 This is not so pleasing to the eye and I would prefer to not have the form function in this way. 这不是那么令人愉悦,我宁愿没有这种形式的功能。

What could possibly cause this? 可能是什么原因造成的? and how does one avoid this? 以及如何避免这种情况?

I've setup my form as usual... 我像往常一样设置了表格...

Here is an example: 这是一个例子:

$('.ui.form.inputform')
        .form({
            on: 'change',
            inline:true,
            fields: {
                name: {
                  identifier: 'name',
                  rules: [
                    {
                      type:'empty',
                      prompt: 'Name is required'
                    },
                    {
                      type: 'inputName',
                      prompt: 'Name already taken'
                    }
                  ]
                },
                month: {
                  identifier: 'month',
                  rules: [
                    {
                      type: 'empty',
                      prompt: 'Month required'
                    },
                    {
                      type: 'isInteger',
                      prompt: 'Must be a non-decimal number'
                    },
                    {
                      type: 'monthRange',
                      prompt: 'Must be between 1 to 12'
                    }
                  ]
                },
                day: {
                  identifier: 'day',
                  rules: [
                    {
                      type: 'empty',
                      prompt: 'Day required'
                    },
                    {
                      type: 'isInteger',
                      prompt: 'Must be a non-decimal number'
                    },
                    {
                      type: 'dayRange',
                      prompt: 'Must be between 1 to 31'
                    }
                  ]
                },
                year: {
                  identifier: 'year',
                  rules: [
                    {
                      type: 'empty',
                      prompt: 'Year required'
                    },
                    {
                      type: 'isInteger',
                      prompt: 'Must be a non-decimal number'
                    },
                    {
                      type: 'yearRange',
                      prompt: 'Must be between -3000 to +3000'
                    }
                  ]
                },
                hour: {
                  identifier: 'hour',
                  rules: [
                    {
                      type: 'empty',
                      prompt: 'Hour required'
                    },
                    {
                      type: 'isInteger',
                      prompt: 'Must be a non-decimal number'
                    },
                    {
                      type: 'hourRange',
                      prompt: 'Must be between 0 to 23'
                    }
                  ]
                },
                minute: {
                  identifier: 'minute',
                  rules: [
                    {
                      type: 'empty',
                      prompt: 'Minutes required'
                    },
                    {
                      type: 'isInteger',
                      prompt: 'Must be a non-decimal number'
                    },
                    {
                      type: 'minuteRange',
                      prompt: 'Must be between 0 to 59'
                    }
                  ]
                },
                second: {
                  identifier: 'second',
                  rules: [
                    {
                      type: 'empty',
                      prompt: 'Seconds required'
                    },
                    {
                      type: 'secondRange',
                      prompt: 'Must be between 0 to 59'
                    }
                  ]
                },
                timezone: {
                  identifier: 'timezone',
                  rules: [
                    {
                      type: 'empty',
                      prompt: 'Timezone required'
                    },
                    {
                      type: 'timezoneRange',
                      prompt: 'Must be between -16.00 to + 16.00'
                    }
                  ]
                },

                longitude_degrees: {
                  identifier: 'longdeg',
                  rules: [
                    {
                      type: 'empty',
                      prompt: 'Required'
                    },
                    {
                      type: 'isInteger',
                      prompt: 'Must be a number without decimal places'
                    },
                    {
                      type: 'longitudeRange',
                      prompt: 'Must be between -180 to +180'
                    },
                  ]
                },
                longitude_minutes: {
                  identifier: 'longmin',
                  rules: [
                    {
                      type: 'empty',
                      prompt: 'Required'
                    },
                    {
                      type: 'isInteger',
                      prompt: 'Must be a number without decimal places'
                    },
                    {
                      type: 'minRange',
                      prompt: 'Must be between 0 to 59'
                    }
                  ]
                },
                longitude_seconds: {
                  identifier: 'longsec',
                  rules: [
                    {
                      type: 'empty',
                      prompt: 'Required'
                    },
                    {
                      type: 'secRange',
                      prompt: 'Must be between 0 to any number less than 60'
                    }
                  ]
                },


                latitude_degrees: {
                  identifier: 'latdeg',
                  rules: [
                    {
                      type: 'empty',
                      prompt: 'Required'
                    },
                    {
                      type: 'isInteger',
                      prompt: 'Must be a number without decimal places'
                    },
                    {
                      type: 'latitudeRange',
                      prompt: 'Must be between -90 to +90'
                    }
                  ]
                },
                latitude_minutes: {
                  identifier: 'latmin',
                  rules: [
                    {
                      type: 'empty',
                      prompt: 'Required'
                    },
                    {
                      type: 'isInteger',
                      prompt: 'Must be a number without decimal places'
                    },
                    {
                      type: 'minRange',
                      prompt: 'Must be between 0 to 59'
                    }
                  ]
                },
                latitude_seconds: {
                  identifier: 'latsec',
                  rules: [
                    {
                      type: 'empty',
                      prompt: 'Required'
                    },
                    {
                      type: 'secRange',
                      prompt: 'Must be between 0 to any number less than 60'
                    }
                  ]
                },
            },


        });

You're going to want to use the "blur" event rather than "change". 您将要使用“模糊”事件而不是“更改”。

I put together a quick demo demonstrating this: 我整理了一个快速演示,演示了这一点:

<form>
    <div class="field">
        <label>Field 1</label>
        <input name="empty" type="text" placeholder="My first field...">
    </div>
    <div class="field">
        <label>Field 2</label>
        <input name="empty" type="text" placeholder="My second field...">
    </div>
    <div class="field">
        <label>Dropdown</label>
        <select class="ui dropdown" name="dropdown">
            <option value="">Select</option>
            <option value="male">Choice 1</option>
            <option value="female">Choice 2</option>
        </select>
    </div>
    <div class="inline field">
        <div class="ui checkbox">
            <input type="checkbox" name="checkbox">
            <label>Checkbox</label>
        </div>
    </div>
    <div class="ui submit button">Submit</div>
    <div class="ui error message"></div>
</form>

Compare to your JavaScript: 与您的JavaScript比较:

$('form').form({
    on: 'blur',
    fields: {
        empty: {
            identifier: 'empty',
            rules: [{
                type: 'empty',
                prompt: 'Please enter a value'
            }]
        },
        dropdown: {
            identifier: 'dropdown',
            rules: [{
                type: 'empty',
                prompt: 'Please select a dropdown value'
            }]
        },
        checkbox: {
            identifier: 'checkbox',
            rules: [{
                type: 'checked',
                prompt: 'Please check the checkbox'
            }]
        }
    }
});

( http://codepen.io/appsoa/pen/BoxQeL ) http://codepen.io/appsoa/pen/BoxQeL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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