Comments (2)
I'm using it in Blazor WASM without any issues. Can you provide more details about the problem you're having?
from fluentvalidation.blazor.
I've been trying to get it to work in Blazor WASM as well, but I'm getting an exception saying a method is not found. I've build a model, an abstract validator for the model, and I'm using the model in a component used on the page which passes the model as a parameter to the component. Here are some examples of my code:
Model:
`public class Period:BaseModel, ICreatedModified
{
public Period() : base()
{
Created=DateTimeOffset.Now;
Modified=DateTimeOffset.Now;
Folders = new List<Folder>();
}
public DateTime? PeriodStart { get; set; }
public DateTime? PeriodEnd { get; set; }
public string Name { get; set; }
public List<Folder> Folders { get; set; }
public DateTimeOffset? Created { get; set; }
public DateTimeOffset? Modified { get; set; }
}`
Validator:
public class PeriodValidator:AbstractValidator<Period> { public PeriodValidator() { RuleFor(p => p.Name).NotEmpty().MaximumLength(50).MinimumLength(5); RuleFor(p => p.PeriodStart).NotEmpty().LessThanOrEqualTo(p => p.PeriodEnd); RuleFor(p => p.PeriodEnd).NotEmpty(); RuleFor(p => p.ItemId).NotEmpty(); } }
Host Page (abbreviated):
`@page "/budget/{id}"
@Inject HttpClient http
@Inject NavigationManager nav
@if (IsLoaded)
{
<PeriodEditorDialog Period="@SelectedPeriod"
ShowDialog="@_showPeriodDialog"
CloseAndAbandonChanges="OnCancelPeriod"
SaveChanges="OnSavePeriod"></PeriodEditorDialog>
}
else
{
<LoadingPanel ShowLoading="@_isLoading"></LoadingPanel>
}
@code {
Budget _budget;
bool _isLoading = true;
protected bool IsLoaded => _budget != null;
bool _showPeriodDialog = false;
Guid _selectedPeriodId = Guid.Empty;
protected override async Task OnInitializedAsync()
{
await LoadBudget();
}
private async Task LoadBudget()
{
var result = await http.GetFromJsonAsync<Budget>($"api/budget/{Id}");
_budget = result;
}
Period SelectedPeriod => _selectedPeriodId != Guid.Empty
? _budget.Periods.FirstOrDefault(p => p.ItemId.Equals(_selectedPeriodId))
: new Period();
protected void OnAddPeriod()
{
_showPeriodDialog = true;
}
protected void OnEditPeriod(Guid id)
{
_selectedPeriodId = id;
_showPeriodDialog = true;
}
protected async Task OnSavePeriod(Period updatedPeriod)
{
/* code omitted */
}
protected void OnCancelPeriod()
{
_selectedPeriodId = Guid.Empty;
_showPeriodDialog = false;
}
[Parameter]
public string Id { get; set; }`
Editor Dialog Component:
`@if (ShowDialog)
{
<div class="period-editor-dialog">
<EditForm Model="@period">
<FluentValidator/>
<div class="dialog">
<div class="dialog-title">Period Information</div>
<div class="dialog-body">
<div class="mb-3">
<label for="periodName" class="form-label">Period Name</label>
<InputText id="periodName" DisplayName="Period Name"
@bind-Value="Period.Name"
class="form-control"></InputText>
<ValidationMessage For="() => Period.Name"></ValidationMessage>
</div>
<div class="mb-3">
<label for="periodStart" class="form-label">Period Start</label>
<InputDate id="periodStart" DisplayName="Period Start"
@bind-Value="Period.PeriodStart"
class="form-control"></InputDate>
<ValidationMessage For="() => Period.PeriodStart"></ValidationMessage>
</div>
<div class="mb-3">
<label for="periodEnd" class="form-label">Period End</label>
<InputDate id="periodEnd" DisplayName="Period End"
@bind-Value="Period.PeriodEnd"
class="form-control"></InputDate>
<ValidationMessage For="() => Period.PeriodEnd"></ValidationMessage>
</div>
</div>
<div class="dialog-actions">
<button type="button" class="btn btn-secondary" @OnClick="@OnClickCancel">Cancel</button>
<button type="submit" class="btn btn-primary">Save</button>
</div>
</div>
</EditForm>
</div>
}
@code {
[Parameter]
public bool ShowDialog { get; set; }
[Parameter]
public Period Period { get; set; }
[Parameter]
public EventCallback CloseAndAbandonChanges { get; set; }
[Parameter]
public EventCallback<Period> SaveChanges { get; set; }
protected async Task OnClickCancel()
{
Period = new Period();
await CloseAndAbandonChanges.InvokeAsync();
}
public async Task SubmitFormAsync()
{
await SaveChanges.InvokeAsync(Period);
}
}
`
Finally, in my startup I have used the FluentValidation.DependencyInjection to add the validators.
`public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddValidatorsFromAssemblyContaining<Period>();
await builder.Build().RunAsync();
}
}`
The error:
crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Method not found: System.Collections.Generic.IList1<FluentValidation.Results.ValidationFailure> FluentValidation.Results.ValidationResult.get_Errors() System.MissingMethodException: Method not found: System.Collections.Generic.IList
1<FluentValidation.Results.ValidationFailure> FluentValidation.Results.ValidationResult.get_Errors()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[<ValidateModel>d__25](<ValidateModel>d__25& stateMachine)
at Microsoft.AspNetCore.Components.Forms.FluentValidator.ValidateModel(EditContext editContext, ValidationMessageStore messages)
at Microsoft.AspNetCore.Components.Forms.FluentValidator.<>c__DisplayClass24_0.<AddValidation>b__0(Object sender, ValidationRequestedEventArgs eventArgs)
at Microsoft.AspNetCore.Components.Forms.EditContext.Validate()
at Microsoft.AspNetCore.Components.Forms.EditForm.HandleSubmitAsync()
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)
from fluentvalidation.blazor.
Related Issues (20)
- InputSelect not clearing message HOT 2
- OnFieldChanged not trigger HOT 1
- Consider using tags to version releases and use Github "releases" tab HOT 4
- How to do async validation HOT 6
- Library is not compatible with Client-Side Blazor due to Exception with async validation HOT 7
- Does not work with FluentValidation 9.x HOT 13
- International Date format yyyy-MM-dd error HOT 4
- Minor FluentValidation integration improvements HOT 1
- Validation messages are link to the first child object when using a list in the Model of the edit context HOT 5
- Silent errors misleading HOT 3
- Validation Message is not working for items inside a list HOT 1
- Nested objects are only validated when submitting the form HOT 2
- EditForm OnValidSubmit event is raised when validation failed using MustAsync() method. HOT 3
- How to trigger the validate method manually HOT 2
- Blazor WASM Project With Both Client & Server Validation HOT 1
- Async validation to server-side call HOT 2
- Upgraded to FluentValidation 10.x and .NET 6 Preview 3, getting the following error HOT 5
- Is this repo abandoned? HOT 2
- Async dispose results in null reference.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fluentvalidation.blazor.