-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Validation doesn't automatically traverse members #10526
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
Comments
Possible solution: This will add the field state to the EditContext and ensure it is validated. |
Moving this out of the backlog. We should consider doing something about this in 3.1 since the experience is fairly poor right now. |
Summary of an in-person discussion:
|
All you need to do is have InputBase ensure it has an entry in the EditContext when its EditContext changes. That will ensure only the items being edited are validated. This should be a minimum requirement. If you also want to provide the ability to validate a whole object regardless of which properties have associated edits, please put a parameter on the validator that allows the user to disable it. Also, please make the traversal code part of EditContext and not DataAnnoticationsValidator - so other validation frameworks can use it too. |
@mrpmorris do you have sample code of your suggestion? |
The following code adds a hacky The "fixed" InputText (should be done in InputBase) simply calls EditContext.GetFieldState when its EditContext changes, this ensures EditContext._fieldStates has an entry for this input. If we add an This suggestion is non breaking and gives us 1: Deep validation into complex types.
|
@mrpmorris I'm not sure how that works. Would each of these fields be the models passed in to data annotations? That would mean validation attributes decorated on properties wouldn't be discovered. In addition, the |
Sorry, can you elaborate? I'm not quite sure I am following what your concerns are. |
@mrpmorris for your ask, we're going to use #14426 to track adding the API to the application. We'll use this issue to continue tracking the original goal of the issue which is to produce an experimental package based on the sample: https://github.com/aspnet/samples/tree/master/samples/aspnetcore/blazor/Validation/Validation |
I work for the UK government Department for Education. A common approach is to allow the user to edit a large object over multiple steps, the experimental approach wouldn't work and would require us to create multiple classes that identically represent parts of the object being edited and map the properties across. It's more work than it needs to be. If InputBase would just instruct EditContext to create FieldState for its FieldIndentifier whenever its EditContext changes then Blazor would ensure all input controls are validated. |
@pranavkm please make sure this happens as the last thing in 3.1, as there is no time pressure. |
* Validation fixes for Blazor * Ensure validation result that are not associated with a member are recorded. Fixes dotnet/aspnetcore#10643 * Add support for showing model-specific errors to ValidationSummary * Add support for nested validation and a more suitable CompareAttribute. Fixes dotnet/aspnetcore#10526
Validation doesn't automatically traverse the members of something so you have to manually add validation support for any satellite objects you want validated.
Example: https://github.com/rynowak/ComponentFunTime/blob/master/Components/Shared/EmployeeEditor.razor#L85
The text was updated successfully, but these errors were encountered: