Skip to content

Commit

Permalink
Display validation errors in corresponding fields
Browse files Browse the repository at this point in the history
  • Loading branch information
audserraCGI committed Jul 18, 2022
1 parent bec1941 commit dd7ec84
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@
id=@nameof(DeviceDetails.DeviceID)
Label="Device ID / DevEUI"
Variant="Variant.Outlined"
Validation=@(standardValidator.ValidateValue)
For="@(()=> Device.DeviceID)"
Required="true"
Mask="@maskLoRaDeviceID"
HelperText="DeviceID must contain 16 hexadecimal characters (numbers from 0 to 9 and/or letters from A to F)" />
}
Expand All @@ -90,8 +90,8 @@
id=@nameof(DeviceDetails.DeviceID)
Label="Device ID"
Variant="Variant.Outlined"
Validation=@(standardValidator.ValidateValue)
For="@(()=> Device.DeviceID)"
Required="true"
HelperText="The device identifier should be of ASCII 7-bit alphanumeric characters plus certain special characters" />
}
</MudItem>
Expand Down
12 changes: 12 additions & 0 deletions src/AzureIoTHub.Portal/Client/Validators/DeviceDetailsValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

namespace AzureIoTHub.Portal.Client.Validators
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AzureIoTHub.Portal.Models.v10;
using FluentValidation;

Expand Down Expand Up @@ -35,5 +39,13 @@ public DeviceDetailsValidator()
.WithMessage("DeviceID is required. It should be a case-sensitive string (up to 128 characters long) of ASCII 7-bit alphanumeric characters plus certain special characters: - . + % _ # * ? ! ( ) , : = @ $ '.");
});
}

public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<DeviceDetails>.CreateWithOptions((DeviceDetails)model, x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}
}

0 comments on commit dd7ec84

Please sign in to comment.