Skip to content

Commit 6ca56ae

Browse files
committed
Example 6 added
1 parent 5e33ec1 commit 6ca56ae

File tree

6 files changed

+122
-0
lines changed

6 files changed

+122
-0
lines changed

samples/ValidationRulesTest/ValidationRulesTest/ValidationRulesTest.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
</ItemGroup>
3737

3838
<ItemGroup>
39+
<EmbeddedResource Update="Views\Example6.xaml">
40+
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
41+
</EmbeddedResource>
3942
<EmbeddedResource Update="Views\Examples.xaml">
4043
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
4144
</EmbeddedResource>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Plugin.ValidationRules;
2+
using Plugin.ValidationRules.Extensions;
3+
using Plugin.ValidationRules.Formatters;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
8+
namespace ValidationRulesTest.ViewModels
9+
{
10+
public class Example6ViewModel
11+
{
12+
public Example6ViewModel()
13+
{
14+
Name = Validator.Build<string>().IsRequired("The name is required.");
15+
Phone = Validator.Build<string>()
16+
.When(x => !string.IsNullOrEmpty(x))
17+
.Must(x => x.Length == 12, "Minimum lenght is 12.");
18+
19+
Phone.ValueFormatter = new MaskFormatter("XXX-XXX-XXXX");
20+
}
21+
22+
public Validatable<string> Name { get; set; }
23+
public Validatable<string> Phone { get; set; }
24+
25+
public bool Validate()
26+
{
27+
return Name.Validate() && Phone.Validate();
28+
}
29+
}
30+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage
3+
x:Class="ValidationRulesTest.Views.Example6"
4+
xmlns="http://xamarin.com/schemas/2014/forms"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
xmlns:bx="clr-namespace:ValidationRulesTest.Behaviors">
7+
<ContentPage.Content>
8+
<StackLayout Padding="16">
9+
10+
11+
<Entry
12+
x:Name="nameEntry"
13+
Placeholder="Name"
14+
Text="{Binding Name.Value, Mode=TwoWay}">
15+
<Entry.Behaviors>
16+
<bx:EventToCommandBehavior Command="{Binding Name.ValidateCommand}" EventName="Unfocused" />
17+
</Entry.Behaviors>
18+
</Entry>
19+
<Label
20+
HorizontalTextAlignment="Center"
21+
Text="{Binding Name.Error}"
22+
TextColor="Red" />
23+
24+
<Entry
25+
x:Name="phoneEntry"
26+
Placeholder="Phone"
27+
Text="{Binding Phone.Value, Mode=TwoWay}">
28+
<Entry.Behaviors>
29+
<bx:EventToCommandBehavior Command="{Binding Phone.ValidateCommand}" EventName="Unfocused" />
30+
</Entry.Behaviors>
31+
</Entry>
32+
<Label
33+
HorizontalTextAlignment="Center"
34+
Text="{Binding Phone.Error}"
35+
TextColor="Red" />
36+
37+
<Button Clicked="Button_Clicked" Text="Validate" />
38+
39+
</StackLayout>
40+
</ContentPage.Content>
41+
</ContentPage>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using ValidationRulesTest.ViewModels;
7+
using Xamarin.Forms;
8+
using Xamarin.Forms.Xaml;
9+
10+
namespace ValidationRulesTest.Views
11+
{
12+
[XamlCompilation(XamlCompilationOptions.Compile)]
13+
public partial class Example6 : ContentPage
14+
{
15+
Example6ViewModel _context;
16+
public Example6 ()
17+
{
18+
InitializeComponent ();
19+
20+
_context = new Example6ViewModel();
21+
BindingContext = _context;
22+
}
23+
24+
private void Button_Clicked(object sender, EventArgs e)
25+
{
26+
var isValid = _context.Validate();
27+
28+
if (isValid)
29+
{
30+
DisplayAlert(":)", "This form is valid", "OK");
31+
}
32+
else
33+
{
34+
DisplayAlert(":(", "This form is not valid", "OK");
35+
}
36+
}
37+
}
38+
}

samples/ValidationRulesTest/ValidationRulesTest/Views/Examples.xaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
Clicked="example5_Clicked"
3838
Text="Example 5" />
3939

40+
<Button
41+
x:Name="example6"
42+
Clicked="example6_Clicked"
43+
Text="Example 6" />
44+
4045
</StackLayout>
4146
</ContentPage.Content>
4247

samples/ValidationRulesTest/ValidationRulesTest/Views/Examples.xaml.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,10 @@ private void example5_Clicked(object sender, EventArgs e)
4141
{
4242
Navigation.PushAsync(new Example5());
4343
}
44+
45+
private void example6_Clicked(object sender, EventArgs e)
46+
{
47+
Navigation.PushAsync(new Example6());
48+
}
4449
}
4550
}

0 commit comments

Comments
 (0)