I have a blazor EditForm tied to a model.
The model for example is StudentModel which has other class instantiated like lets say Phone.
public class Student
{
[Required]
public string Name {get; set;}
public Phone Phone {get; set;}
}
public class Phone
{
[Required]
public string Number {get; set;}
}
When validation runs on the form, the Name in the model does get validated, but the Number property on the Phone class does not. Why? How do I get properties validated that are in other classes but are instantiated in the model?
You have a complex object. In this case it's relatively easy to flatten out.
In the code below I've created an edit context from the object and moved the validation to that object. Create an instance of
StudentEditContext
in the edit form and then submitStudentEditContext.AsStudent
back into the data pipeline to persist it.That being said, I'm guessing you've simplified your requirement and your object isn't so simple to deal with.
While it's relatively easy to get such a data object from the data domain [EF makes it simple], dealing with it safely and properly in the core business domain is far more complex. An explanation of how to construct and use aggregate objects is far too complex a subject for an SO answer.