Skip to content

Commit

Permalink
Validate inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
ousiax committed Dec 12, 2023
1 parent 6d63660 commit cdf43ea
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 22 deletions.
52 changes: 35 additions & 17 deletions src/Leo.Wpf.App/ViewModels/NewCustomerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,52 @@
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Leo.Data.Domain.Dtos;
using Leo.Data.Domain.Entities;
using Leo.UI;
using Leo.Wpf.App.Messages;
using System.ComponentModel.DataAnnotations;

namespace Leo.Wpf.App.ViewModels
{
public partial class NewCustomerViewModel : ObservableObject
public partial class NewCustomerViewModel(
ICustomerService _customerService,
IMapper _mapper,
IMessenger _messenger) : ObservableValidator
{
[Required(AllowEmptyStrings = false)]
[NotifyDataErrorInfo]
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(SaveCommand))]
private string? _name;

[Phone]
[NotifyDataErrorInfo]
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(SaveCommand))]
private string? _phone;

[EnumDataType(typeof(Gender))]
[NotifyDataErrorInfo]
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(SaveCommand))]
private string? _gender;

[DataType(DataType.Date)]
[NotifyDataErrorInfo]
[ObservableProperty]
private DateTime? _birthday;
[NotifyCanExecuteChangedFor(nameof(SaveCommand))]
private DateOnly? _birthday;

[Required(AllowEmptyStrings = false)]
[NotifyDataErrorInfo]
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(SaveCommand))]
private string? _cardNo;

private readonly ICustomerService _customerService;
private readonly IMapper _mapper;
private readonly IMessenger _messenger;

public NewCustomerViewModel(
ICustomerService customerService,
IMapper mapper,
IMessenger messenger)
{
_customerService = customerService;
_mapper = mapper;
_messenger = messenger;
}

[RelayCommand]
private void Close() => CloseAction?.Invoke();

[RelayCommand]
[RelayCommand(CanExecute = nameof(CanSave))]
private async Task SaveAsync()
{
var dto = _mapper.Map<CustomerDto>(this);
Expand All @@ -52,5 +58,17 @@ private async Task SaveAsync()
}

public event Action? CloseAction;

private bool CanSave()
{
return !HasErrors && Validate();
}

private bool Validate()
{
var context = new System.ComponentModel.DataAnnotations.ValidationContext(this);
var results = new List<ValidationResult>();
return Validator.TryValidateObject(this, context, results, true);
}
}
}
40 changes: 35 additions & 5 deletions src/Leo.Wpf.App/Views/NewCustomerWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@
<Setter Property="Control.Margin" Value="2,0,5,0" />
<Setter Property="Control.VerticalContentAlignment" Value="Center" />
<Setter Property="Control.Margin" Value="0,1,0,1" />
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<Border BorderBrush="Red" BorderThickness="1">
<AdornedElementPlaceholder/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="Control.ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors)[0].ErrorContent}" />
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="Label">
<Setter Property="MinWidth" Value="80"/>
Expand All @@ -37,6 +53,7 @@
<Setter Property="MinWidth" Value="80" />
<Setter Property="MinHeight" Value="30" />
</Style>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</Window.Resources>
<Grid Margin="5,5,5,5">
<Grid.RowDefinitions>
Expand All @@ -49,29 +66,42 @@
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<Label Content="{StaticResource NewCustomerWindow.Label.Name.Content}" Target="{Binding ElementName=txtName}" />
<TextBox x:Name="txtName" Text="{Binding Name}" />
<TextBox x:Name="txtName" Text="{Binding Name, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}">
<TextBox.ToolTip>
<ToolTip Visibility="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget.(Validation.HasError), Converter={StaticResource BooleanToVisibilityConverter}}"
Content="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget.(Validation.Errors)[0].ErrorContent}">
</ToolTip>
</TextBox.ToolTip>
<Validation.ErrorTemplate>
<ControlTemplate>
<Border BorderBrush="Red" BorderThickness="1" ToolTip="{Binding Path=ErrorContent}">
<AdornedElementPlaceholder/>
</Border>
</ControlTemplate>
</Validation.ErrorTemplate>
</TextBox>
</StackPanel>
<StackPanel Grid.Row="1" Orientation="Horizontal">
<Label Content="{StaticResource NewCustomerWindow.Label.Birthday.Content}" />
<DatePickerTextBox
x:Name="txtBirthday"
Text="{Binding Birthday, StringFormat={StaticResource NewCustomerWindow.Label.Birthday.StringFormat}}"/>
Text="{Binding Birthday, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, StringFormat={StaticResource NewCustomerWindow.Label.Birthday.StringFormat}}"/>
</StackPanel>
<StackPanel Grid.Row="2" Orientation="Horizontal">
<Label Content="{StaticResource NewCustomerWindow.Label.Gender.Content}" />
<ComboBox x:Name="txtGender"
ItemsSource="{x:Static infra:Constants.Genders}"
SelectedValuePath="Key"
DisplayMemberPath="Value"
SelectedValue="{Binding Gender}"/>
SelectedValue="{Binding Gender, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"/>
</StackPanel>
<StackPanel Grid.Row="3" Orientation="Horizontal">
<Label Content="{StaticResource NewCustomerWindow.Label.Phone.Content}" />
<TextBox x:Name="txtPhone" Text="{Binding Phone}" />
<TextBox x:Name="txtPhone" Text="{Binding Phone, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
<StackPanel Grid.Row="4" Orientation="Horizontal">
<Label Content="{StaticResource NewCustomerWindow.Label.CardNo.Content}" />
<TextBox x:Name="txtCarNo" Text="{Binding CardNo}" />
<TextBox x:Name="txtCarNo" Text="{Binding CardNo, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
<StackPanel Grid.Row="5" Orientation="Horizontal" FlowDirection="RightToLeft">
<Button x:Name="btnOK"
Expand Down

0 comments on commit cdf43ea

Please sign in to comment.