-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
samples/ValidationRulesTest/ValidationRulesTest/ViewModels/Example6ViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
samples/ValidationRulesTest/ValidationRulesTest/Views/Example6.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
38 changes: 38 additions & 0 deletions
38
samples/ValidationRulesTest/ValidationRulesTest/Views/Example6.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters