Search code examples
c#xamlxamarin.formsxamarin-community-toolkit

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


This is my form with two entries for Mail and Phone and button Send

<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>

And this is my validation function:

       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;              
        }

I have problem when the Entry is empty that is Valid Statement and that is wrong. I have a simple requirement. If enter valid address and valid phone pass it. How to fix this?


Solution

  • For phone you can just change when the behavior is applied by changing the Flags property value to "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>
    

    For phone you can use MinimumLength as GeraldVersluis mentioned in his comment

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

    If this is not enough in some cases and you want more control on "when" to validate, you can call ForceValidate() method, for example inside your ValidationFormForRegistraitonClicked() event handler to force validation/re-evaluation, or whenever you think it is necessary.

    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;              
    }