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

🐛 fix(Form): reset operation will throw an exception when ValueExpression is missing #2023

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Masa.Blazor/Components/Form/MForm.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,12 @@ public bool Validate(IValidatable validatable)
{
var valid = validatable.Validate();

if (valid && EditContext is not null)
var valueIdentifier = validatable.ValueIdentifier;

if (valid && EditContext is not null && valueIdentifier.HasValue)
{
EditContext.NotifyFieldChanged(validatable.ValueIdentifier);
valid = valid && !EditContext.GetValidationMessages(validatable.ValueIdentifier).Any();
EditContext.NotifyFieldChanged(valueIdentifier.Value);
valid = valid && !EditContext.GetValidationMessages(valueIdentifier.Value).Any();
}

return valid;
Expand Down
2 changes: 1 addition & 1 deletion src/Masa.Blazor/Components/Input/IValidatable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public interface IValidatable
{
FieldIdentifier ValueIdentifier { get; set; }
FieldIdentifier? ValueIdentifier { get; set; }

bool Validate();

Expand Down
15 changes: 9 additions & 6 deletions src/Masa.Blazor/Components/Input/MInput.Validatable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected void UpdateInternalValue(TValue? value, InternalValueChangeType change

protected EditContext? OldEditContext { get; set; }

public FieldIdentifier ValueIdentifier { get; set; }
public FieldIdentifier? ValueIdentifier { get; set; }

protected bool HasInput { get; set; }

Expand Down Expand Up @@ -407,9 +407,9 @@ protected void InternalValidate()
return;
}

if (!EqualityComparer<FieldIdentifier>.Default.Equals(ValueIdentifier, default))
if (ValueIdentifier.HasValue && !EqualityComparer<FieldIdentifier>.Default.Equals(ValueIdentifier.Value, default))
{
EditContext.NotifyFieldChanged(ValueIdentifier);
EditContext.NotifyFieldChanged(ValueIdentifier.Value);
}
}

Expand Down Expand Up @@ -485,7 +485,10 @@ public void Reset()
HasInput = false;
HasFocused = false;

EditContext?.MarkAsUnmodified(ValueIdentifier);
if (ValueIdentifier.HasValue)
{
EditContext?.MarkAsUnmodified(ValueIdentifier.Value);
}

UpdateInternalValue(default, InternalValueChangeType.InternalOperation);
}
Expand All @@ -501,7 +504,7 @@ protected void HandleOnValidationStateChanged(object? sender, ValidationStateCha
// 1. Force validation, because it validates all input elements
// 2. The input pointed to by ValueIdentifier has been modified
if (!_forceStatus && EditContext?.IsModified() is true
&& !EditContext.IsModified(ValueIdentifier)
&& !EditContext.IsModified(ValueIdentifier!.Value)
&& InternalRules.Any() is false)
{
return;
Expand All @@ -511,7 +514,7 @@ protected void HandleOnValidationStateChanged(object? sender, ValidationStateCha

ErrorBucket.Clear();

var editContextErrors = EditContext!.GetValidationMessages(ValueIdentifier).ToList();
var editContextErrors = EditContext!.GetValidationMessages(ValueIdentifier!.Value).ToList();
ErrorBucket.AddRange(editContextErrors);

var ruleErrors = ValidateRules(InternalValue);
Expand Down
7 changes: 6 additions & 1 deletion src/Masa.Blazor/Components/Input/MInput.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,12 @@ protected async Task TryInvokeFieldChangeOfInputsFilter(bool isClear = false)
return;
}

await InputsFilter.FieldChange(ValueIdentifier.FieldName, isClear);
if (!ValueIdentifier.HasValue)
{
throw new InvalidOperationException("If you split the @bind-Value into Value and ValueChanged, the ValueExpression is required.");
}

await InputsFilter.FieldChange(ValueIdentifier.Value.FieldName, isClear);
}

public void ResetFilter()
Expand Down
Loading