闪电:输入日期字段验证

时间:2018-10-11 16:00:58

标签: salesforce salesforce-lightning lightning

我正在尝试通过以下组件使用标准日期选择器,但是当输入的日期不在指定的最小和最大范围内时,我想显示自定义错误消息。但是我无法实现它,因为可以看到标准错误消息。我通过参考:https://developer.salesforce.com/docs/component-library/bundle/lightning:input/example实现了相同的目标,在此链接中,我看到可以显示自定义错误消息。但是对我没有任何帮助的人可以帮忙。 https://developer.salesforce.com/docs/component-library/bundle/lightning:input/documentation

Period = 1
Sequence 0 - vptnvffuntshtarptymjwzirappljmhhqvsubwlzzygvtyitarptyiougxiuydtgzhhvvmum
shwkzgstfmekvmpkswdgbilvjljmglmjfqwioiivknulvvfemioiemojtywdsajtwmtcgluy
sdsumfbieugmvalvxkjduetukatymvkqzhvqvgvptytjwwldyeevquhlulwpkt

IOC of sequence 0 - 0.044944352356144926

IOC 1 = 0.044944352356144926

Period = 2

ssmbegvlxjutktmkzvvvttwlyeqhuwk <- Section of output missing

IOC of sequence 0 - 0.04492670854749667

ssmbegvlxjutktmkzvvvttwlyeqhuwk <- Section of output missing

IOC of sequence 1 - 0.046640015229392734

IOC 2 = 0.0457833618884447

Period = 3

hztemsgllgjwiklfiejwawcu <- Section of output missing

sfemlkuutvzqvywdeuup <- Section of output missing

IOC of sequence 0 - 0.036435469710272166

hztemsgllgjwiklfiejwawcu <- Section of output missing

sfemlkuutvzqvywdeuup <- Section of output missing

IOC of sequence 1 - 0.045609548167092936

hztemsgllgjwiklfiejwawcu <- Section of output missing

sfemlkuutvzqvywdeuup <- Section of output missing

IOC of sequence 2 - 0.04901960784313726

IOC 3 = 0.04368820857350079

Period = 4

wsepdljmwiufoowjtusmevxukmzvtwyquk <- Section of output missing

IOC of sequence 0 - 0.04072398190045249

wsepdljmwiufoowjtusmevxukmzvtwyquk <- Section of output missing

IOC of sequence 1 - 0.03846153846153847

wsepdljmwiufoowjtusmevxukmzvtwyquk <- Section of output missing

IOC of sequence 2 - 0.04862745098039216

wsepdljmwiufoowjtusmevxukmzvtwyquk <- Section of output missing

IOC of sequence 3 - 0.05568627450980392

IOC 4 = 0.04587481146304676

Period = 5

Sequence 0 - vfspwphuzypuyhuhsksijjonfitatysivktyzgtdql

IOC of sequence 0 - 0.04065040650406503

Sequence 0 - vfspwphuzypuyhuhsksijjonfitatysivktyzgtdql

IOC of sequence 1 - 0.04146341463414635

Sequence 0 - vfspwphuzypuyhuhsksijjonfitatysivktyzgtdql

IOC of sequence 2 - 0.048717948717948725

Sequence 0 - vfspwphuzypuyhuhsksijjonfitatysivktyzgtdql

IOC of sequence 3 - 0.0451219512195122

Sequence 0 - vfspwphuzypuyhuhsksijjonfitatysivktyzgtdql

IOC of sequence 4 - 0.04181184668989547

IOC 5 = 0.04355311355311356

Period = 6

zeslgwkfewwuselutzvweu

IOC of sequence 0 - 0.0427807486631016

zeslgwkfewwuselutzvweu

IOC of sequence 1 - 0.04991087344028521

zeslgwkfewwuselutzvweu

IOC of sequence 2 - 0.058823529411764705

zeslgwkfewwuselutzvweu

IOC of sequence 3 - 0.030303030303030297

zeslgwkfewwuselutzvweu

IOC of sequence 4 - 0.052100840336134456

zeslgwkfewwuselutzvweu

IOC of sequence 5 - 0.04369747899159665

IOC 6 = 0.04626941685765216

Period = 7

Sequence 0 - vurzjugrggukewjjivedt

bauyvyyl

IOC of sequence 0 - 0.04926108374384236

Sequence 0 - vurzjugrggukewjjivedt

bauyvyyl

IOC of sequence 1 - 0.0367816091954023

Sequence 0 - vurzjugrggukewjjivedt

bauyvyyl

IOC of sequence 2 - 0.07142857142857142

Sequence 0 - vurzjugrggukewjjivedt

bauyvyyl

IOC of sequence 3 - 0.04679802955665024

Sequence 0 - vurzjugrggukewjjivedt

bauyvyyl

IOC of sequence 4 - 0.06206896551724138

Sequence 0 - vurzjugrggukewjjivedt

bauyvyyl

IOC of sequence 5 - 0.041379310344827586

Sequence 0 - vurzjugrggukewjjivedt

bauyvyyl

IOC of sequence 6 - 0.059113300492610835

IOC 7 = 0.052404410039878016

1 个答案:

答案 0 :(得分:0)

这个问题在1个月前被问到,但是如果有人遇到这个问题,可以分享答案。使用闪电的 setCustomValidity() reportValidity()方法。

组件:

    <aura:component >
    <lightning:input aura:id="field"
                     type="date" 
                     name="MyDatefield" 
                     label="MyDateField" 
                     value="2017-09-07" 
                     min="2017-09-05" 
                     max="2017-09-22" 
                     onblur ="{!c.checkValidity }"
             />
</aura:component>

控制器:

({
    checkValidity : function(component, event, helper) {
        var inputCmp = component.find("field");
        inputCmp.setCustomValidity(""); //reset error
        var value = inputCmp.get("v.value");
        console.log('value: '+value);
        var lowerRange =  new Date("2017/09/05"); 
        var higherRange =  new Date("2017/09/22"); 
        if(!inputCmp.checkValidity()){
            if(Date.parse(value)){
                if (Date.parse(value) < lowerRange) {
                    inputCmp.setCustomValidity("Select correct date range1");
                }else if (Date.parse(value) > higherRange){
                    inputCmp.setCustomValidity("Select correct date range2"); 
                }
            }else{
                inputCmp.setCustomValidity("Invalid value");
            }
        }
        inputCmp.reportValidity();
    }
})