Skip to content

Commit

Permalink
Fixing validation key lookup (JeanPhilippeKernel#398)
Browse files Browse the repository at this point in the history
* completed validation rules

* fixed nameof key
  • Loading branch information
JeanPhilippeKernel authored Nov 25, 2024
1 parent effe369 commit d3ecd53
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions Panzerfaust/ViewModels/ProjectWindowViewModel.ValidationRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,46 +30,48 @@ public IEnumerable GetErrors(string? propertyName)

private void ValidateProjectName(string? name)
{
if (_errors.ContainsKey(nameof(ProjectName)))
var key = nameof(ProjectName);
if (_errors.ContainsKey(key))
{
_errors[nameof(ProjectName)].Clear();
_errors.Remove(key);
}

if (string.IsNullOrEmpty(name))
{
_errors[nameof(ProjectName)] = new() { "Name can't be empty" };
_errors[key] = new() { "Name can't be empty" };
}
else if (string.IsNullOrWhiteSpace(name))
{
_errors[nameof(ProjectName)] = new() { "Name can't contain only whitespaces" };
_errors[key] = new() { "Name can't contain only whitespaces" };
}
else if (Regex.IsMatch(name, @"[\s\W]+"))
{
_errors[nameof(ProjectName)] = new() { "Name can't contain special characters or whitespace." };
_errors[key] = new() { "Name can't contain special characters or whitespace." };
}

this.RaisePropertyChanged(nameof(HasErrors));
}

private void ValidateProjectLocation(string? location)
{
if (_errors.ContainsKey(nameof(ProjectLocation)))
var key = nameof(ProjectLocation);
if (_errors.ContainsKey(key))
{
_errors[nameof(ProjectLocation)].Clear();
_errors.Remove(key);
}

if (string.IsNullOrEmpty(location))
{
_errors[nameof(ProjectLocation)] = new() { "Location can't be empty" };
_errors[key] = new() { "Location can't be empty" };
}
else if (string.IsNullOrWhiteSpace(location))
{
_errors[nameof(ProjectLocation)] = new() { "Location can't contain only whitespaces" };
_errors[key] = new() { "Location can't contain only whitespaces" };
}

if (!Directory.Exists(location))
{
_errors[nameof(ProjectLocation)] = new() { "This location isn't valid" };
_errors[key] = new() { "This location isn't valid" };
}

this.RaisePropertyChanged(nameof(HasErrors));
Expand Down

0 comments on commit d3ecd53

Please sign in to comment.