Skip to content

Commit

Permalink
Example 6 added
Browse files Browse the repository at this point in the history
  • Loading branch information
luismts committed Aug 17, 2021
1 parent 5e33ec1 commit 6ca56ae
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Views\Example6.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Update="Views\Examples.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Plugin.ValidationRules;
using Plugin.ValidationRules.Extensions;
using Plugin.ValidationRules.Formatters;
using System;
using System.Collections.Generic;
using System.Text;

namespace ValidationRulesTest.ViewModels
{
public class Example6ViewModel
{
public Example6ViewModel()
{
Name = Validator.Build<string>().IsRequired("The name is required.");
Phone = Validator.Build<string>()
.When(x => !string.IsNullOrEmpty(x))
.Must(x => x.Length == 12, "Minimum lenght is 12.");

Phone.ValueFormatter = new MaskFormatter("XXX-XXX-XXXX");
}

public Validatable<string> Name { get; set; }
public Validatable<string> Phone { get; set; }

public bool Validate()
{
return Name.Validate() && Phone.Validate();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="ValidationRulesTest.Views.Example6"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:bx="clr-namespace:ValidationRulesTest.Behaviors">
<ContentPage.Content>
<StackLayout Padding="16">


<Entry
x:Name="nameEntry"
Placeholder="Name"
Text="{Binding Name.Value, Mode=TwoWay}">
<Entry.Behaviors>
<bx:EventToCommandBehavior Command="{Binding Name.ValidateCommand}" EventName="Unfocused" />
</Entry.Behaviors>
</Entry>
<Label
HorizontalTextAlignment="Center"
Text="{Binding Name.Error}"
TextColor="Red" />

<Entry
x:Name="phoneEntry"
Placeholder="Phone"
Text="{Binding Phone.Value, Mode=TwoWay}">
<Entry.Behaviors>
<bx:EventToCommandBehavior Command="{Binding Phone.ValidateCommand}" EventName="Unfocused" />
</Entry.Behaviors>
</Entry>
<Label
HorizontalTextAlignment="Center"
Text="{Binding Phone.Error}"
TextColor="Red" />

<Button Clicked="Button_Clicked" Text="Validate" />

</StackLayout>
</ContentPage.Content>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ValidationRulesTest.ViewModels;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace ValidationRulesTest.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Example6 : ContentPage
{
Example6ViewModel _context;
public Example6 ()
{
InitializeComponent ();

_context = new Example6ViewModel();
BindingContext = _context;
}

private void Button_Clicked(object sender, EventArgs e)
{
var isValid = _context.Validate();

if (isValid)
{
DisplayAlert(":)", "This form is valid", "OK");
}
else
{
DisplayAlert(":(", "This form is not valid", "OK");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
Clicked="example5_Clicked"
Text="Example 5" />

<Button
x:Name="example6"
Clicked="example6_Clicked"
Text="Example 6" />

</StackLayout>
</ContentPage.Content>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,10 @@ private void example5_Clicked(object sender, EventArgs e)
{
Navigation.PushAsync(new Example5());
}

private void example6_Clicked(object sender, EventArgs e)
{
Navigation.PushAsync(new Example6());
}
}
}

0 comments on commit 6ca56ae

Please sign in to comment.