即使 Entry 为空,Xamarin Community Toolkit 输入验证也是正确的

Xamarin Community Toolkit input validation is true even when Entry is empty

这是我的表单,其中包含邮件和 Phone 以及发送按钮两个条目

<StackLayout HorizontalOptions="FillAndExpand" Orientation="Vertical" Grid.Row="0"  Margin="0,5,0,5">
    <Label>
        <Label.FormattedText>
            <FormattedString>
                <Span  FontSize="16" Text="Phone" TextColor="#07987f"></Span>
                <Span  FontSize="16" Text=": " TextColor="#07987f"></Span>
            </FormattedString>
        </Label.FormattedText>
    </Label>
    <Entry x:Name="entryPhone" Placeholder="Phone" Text="{Binding Phone}">
        <Entry.Behaviors>
            <xct:TextValidationBehavior x:Name="phoneValidationRule" Flags="ValidateOnUnfocusing" RegexPattern="^07[0-9]{7}$"/>
        </Entry.Behaviors>
    </Entry>
</StackLayout>

<StackLayout HorizontalOptions="FillAndExpand" Orientation="Vertical" Grid.Row="1" Margin="0,5,0,5">
    <Label>
        <Label.FormattedText>
            <FormattedString>
                <Span  FontSize="16" Text="E-mail" TextColor="#07987f"></Span>
                <Span  FontSize="16" Text=": " TextColor="#07987f"></Span>
            </FormattedString>
        </Label.FormattedText>
    </Label>
    <Entry x:Name="entryEmail" Placeholder="Email" Text="{Binding Email}">
        <Entry.Behaviors>
            <xct:EmailValidationBehavior x:Name="emailValidationRule"  Flags="ValidateOnUnfocusing"/>
        </Entry.Behaviors>
    </Entry>
</StackLayout>

<Button Text="SEND" Clicked="ValidationFormForRegistraitonClicked" TextColor="#07987f"  BackgroundColor="#eeeeee"  Margin="0,10,0,10"></Button>
<StackLayout>
    <Label IsVisible="{Binding IsValidMail}" Text="ENTER VALID EMAIL"></Label>
    <Label  IsVisible="{Binding IsValidPhone}" Text="ENTER VALID PHONE"></Label>
</StackLayout>

这是我的验证函数:

       private void ValidationFormForRegistraitonClicked(object sender, EventArgs e)
        {

                if (!emailValidationRule.IsValid)
                    viewModel.IsValidMail = true;
                else
                    viewModel.IsValidMail = false;
                if (!phoneValidationRule.IsValid)
                    viewModel.IsValidPhone = true;
                else
                    viewModel.IsValidPhone = false;              
        }

当条目为空时,我遇到了问题,这是有效的声明,但这是错误的。我有一个简单的要求。如果输入有效地址和有效 phone 通过它。如何解决这个问题?

对于 phone,您可以通过将 Flags 属性 值更改为 "ValidateOnAttaching":

来更改应用行为的时间
<Entry x:Name="entryPhone" Placeholder="Phone" Text="{Binding Phone}">
      <Entry.Behaviors>
           <xct:TextValidationBehavior x:Name="phoneValidationRule" Flags="ValidateOnAttaching" RegexPattern="^07[0-9]{7}$"/>
      </Entry.Behaviors>
</Entry>

对于 phone 你可以使用 MinimumLength 正如 GeraldVersluis 在他的评论中提到的那样

<Entry x:Name="entryEmail" Placeholder="Email" Text="{Binding Email}">
       <Entry.Behaviors>
             <xct:EmailValidationBehavior x:Name="emailValidationRule"  Flags="ValidateOnAttaching" MinimumLength="1"/>
        </Entry.Behaviors>
</Entry>

如果在某些情况下这还不够,并且您希望更多地控制“何时”进行验证,您可以调用 ForceValidate() 方法,例如在您的 ValidationFormForRegistraitonClicked() 事件处理程序中强制 validation/re-evaluation,或任何您认为有必要的时候。

private void ValidationFormForRegistraitonClicked(object sender, EventArgs e)
{
            emailValidationRule.ForceValidate();
            phoneValidationRule.ForceValidate();

            if (!emailValidationRule.IsValid)
                 viewModel.IsValidMail = true;
            else
                viewModel.IsValidMail = false;
            if (!phoneValidationRule.IsValid)
                viewModel.IsValidPhone = true;
            else
                viewModel.IsValidPhone = false;              
}