Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds FieldIdentifier parameter to FluentValidationMessage #1489

Conversation

thebarrettlo
Copy link
Contributor

@thebarrettlo thebarrettlo commented Feb 9, 2024

Pull Request

πŸ“– Description

Adds a new parameter, Field (of type FieldIdentifier) to the FluentValidationMessage component. This allows validation messages to be specified with a direct reference to a field within an EditContext/EditForm, rather than only through an accessor.

For fields that represent a property on a pre-defined model passed to an EditContext, this parameter is extraneous since those model (auto-)properties double as a field accessor. However, for dynamic models that do not have design-time accessors, a FieldIdentifier is more reliable than an accessor.

Example usage

With some additional work, an EditForm can work with a dictionary rather than an object model:

<EditForm EditContext=@_editContext>
    @foreach (KeyValuePair<string, string?> kvp in _dynamicModel)
    {
        <FluentTextField Id=@kvp.Key
                         Label=@kvp.Key
                         Placeholder="Enter a string"
                         Value=@(_dynamicModel[kvp.Key])
                         ValueChanged=@(value => OnValueChanged(kvp.Key, value)) />
        <FluentValidationMessage Field=@(_editContext.Field(kvp.Key)) TValue=FieldIdentifier />
        @* <FluentValidationMessage For@(() => _dynamicModel[kvp.Key]) /> or something similar will not work *@
    }
</EditForm>

@code {
    private EditContext _editContext = new(new object());
    private Dictionary<string, string?> _dynamicModel = new();
    private ValidationMessageStore? _validationMessageStore;

    protected override void OnInitialized()
    {
        // Example model with a dictionary
        _dynamicModel = new()
        {
            { "Key 0", null },
            { "Key 1", null }
        };
        _editContext = new EditContext(_dynamicModel);
        _validationMessageStore = new ValidationMessageStore(_editContext);
    }

    private void OnValueChanged(string key, string value)
    {
        _dynamicModel[key] = value;

        // Validate the field here for example
        FieldIdentifier fieldIdentifier = _editContext.Field(key);
        _validationMessageStore?.Clear(fieldIdentifier);

        if (string.IsNullOrWhiteSpace(value))
        {
            _validationMessageStore?.Add(fieldIdentifier, "The field is required.");
        }
    }
}

No input given:

image

Valid input and invalid (white space) input:

image
(I don't have a fix for the "valid" border color showing even though the field is invalid. Suggestions very welcome πŸ˜ƒ)

🎫 Issues

πŸ‘©β€πŸ’» Reviewer Notes

πŸ“‘ Test Plan

No tests affected by this addition. This new property has been tested in a scenario such as above and more complex.

βœ… Checklist

General

  • I have added tests for my changes.
  • I have tested my changes.
  • I have updated the project documentation to reflect my changes.
  • I have read the CONTRIBUTING documentation and followed the standards for this project.

Component-specific

  • I have added a new component
  • I have added Unit Tests for my new compontent
  • I have modified an existing component
  • I have validate Unit Tests for an existing component

⏭ Next Steps

@vnbaaij vnbaaij enabled auto-merge (squash) February 13, 2024 08:36
@vnbaaij vnbaaij merged commit 266971d into microsoft:dev Feb 13, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants