I have a form with 2 fields (Name and address) all both required field in my model.
On my model I have 5 properties, (ID (Pk), Name , Address, Createdby, and Updatedby), which are all set to required exceptthe primark key ID.
On my Editform I would like to allow just the Name and Address field to be populated, but the createdby and updatedby to be done in my Insert.
How do I get this to valid on my form. I'm getting not valid because of the required rule on the (Createdby and Updatedby) and this comes up when i add validationsummary tag. if i dont add the tag, the textboxes are valid, but the submitPerson method does not.
Is there a way to get by this? Please see my code below.
Thanks in advance.
****MY EDITFORM ********
<EditForm EditContext="@myEditContext" OnSubmit="@SubmitPerson">
<DataAnnotationsValidator />
<InputText class="form-control" id="NameInput" @bind-Value="Person.Name" />
<InputText class="form-control" id="AddressInput" @bind-Value="Person.Address" />
<button type="submit" class="btn btn-primary">Submit</button>
</EditForm>
*********** MY CODE ****************
@code {
public PersonModel Person = new PersonModel();
EditContext myEditContext { get; set; }
private string ValidMEssage;
protected override void OnInitialized() {
myEditContext = new EditContext(Person);
base.OnInitialized();
}
// my submit button method
protected void SubmitPerson(EditContext editContext) {
bool isFormValid = editContext.Validate();
if (isFormValid)
{
//apply when the form is valid
ValidMEssage = "Save successfully";
}
else
{
//apply when the form is not valid
ValidMEssage = "Invalid";
}
}
*****MY MODEL ********
public class PersonModel {
public int ID { get; set; }
[Required(AllowEmptyStrings = false)]
public String Name { get; set; }
[Required(AllowEmptyStrings = false)]
public String Address { get; set; }
[Required(AllowEmptyStrings = false)]
public String CreatedBy { get; set; }
[Required(AllowEmptyStrings = false)]
public String UpdatedBy { get; set; }
}