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

provide possibility to override expression used for validation #272

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Sebbstar
Copy link

@Sebbstar Sebbstar commented Sep 30, 2019

We use some textfields to bind to arrays or other not supported datatypes. We rely on DataAnnotations on our models and do not want to copy these rules to the razor files.

For these properties we created proxy properties which convert between string and the type of the models.

image

Currently MatBlazor extracts the FieldIdentifier for the validation from the valueexpression which makes it impossible to use different properties for value and validation.
To workaround this problem we had to disable the validation of the field and manually set the validation classes. What we need to solve our problem is a way to override the BaseMatInputComponent<T>.FieldIdentifier to point to the real model and not to our value-providing proxy.
Currently our code looks like this:

   <MatTextField InputClass="@DnsValidationClass" ValidationDisabled="true" @bind-Value="@PROXYDnsEntries" Label="DnsEntries" HelperText="(comma seperated list)" />
        <ValidationMessage For="(() => ProxySettings.DnsEntries)" />
        public string DnsValidationClass
        {
            get
            {
                if (EditContext.GetValidationMessages(EditContext.Field(nameof(Model.DnsEntries))).Any())
                {
                    return "invalid";
                }
                else if (EditContext.IsModified(EditContext.Field(nameof(Model.DnsEntries))))
                {
                    return "valid modified";
                }
                else
                {
                    return string.Empty;
                }
            }
        }

        public string PROXYDnsEntries
        {
            get
            {
                return Model.DnsEntries == null ? string.Empty : string.Join(", ", Model.DnsEntries);
            }
            set
            {
            {
                Model.DnsEntries = value.Split(',').Select(x => x.Trim()).ToArray();
                EditContext.NotifyFieldChanged(EditContext.Field(nameof(Model.DnsEntries)));
            }
        }

With the proposed change from this merge request we could simplify our code like this:

   <MatTextField ValidateValueExpression="@(() => Model.DnsEntries)" @bind-Value="@PROXYDnsEntries"  />
        <ValidationMessage For="(() => Model.DnsEntries)" />
        public string PROXYDnsEntries
        {
            get
            {
                return Model.DnsEntries == null ? string.Empty : string.Join(", ", Model.DnsEntries);
            }
            set
            {
            {
                Model.DnsEntries = value.Split(',').Select(x => x.Trim()).ToArray();
            }
        }

@Sebbstar Sebbstar marked this pull request as ready for review October 1, 2019 18:59
@mattgenious
Copy link

Is it correctly understood that this pull request fixes #278 #213 #184 ?

@SamProf
Copy link
Owner

SamProf commented Oct 17, 2019

I am sorry, but at this branch (https://github.com/SamProf/MatBlazor/tree/feature/forms-2.1)and this issue (#271) I working on new forms components.
So, I will check this PR after I will complete all stuff with EditContext and etc...

@Christian-Oleson
Copy link
Contributor

@Sebbstar ,
There are merge conflicts here

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

5 participants