1

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; }

    }

1 Answer 1

1

Your code is working as expected. You have a PersonModel and your model never satisfies the [Required()] attributes.

A couple options:

  1. Initialize your model to be valid.

     @code { 
    
         public PersonModel Person = new PersonModel() 
                                        { 
                                            CreatedBy="NonEmpty", 
                                            UpdatedBy="NonEmpty" 
                                        }
    
  2. Remove the constraints that your model is not satisfying.

public class PersonModel { public int ID { get; set; }

    [Required(AllowEmptyStrings = false)]
    public String Name { get; set; }

    [Required(AllowEmptyStrings = false)]
    public String Address { get; set; }

    public String CreatedBy { get; set; }

    public String UpdatedBy { get; set; }

}
  1. Pick a possibly more appropriate Model for a "form with 2 fields"

    public class PersonModel
    {

         public int ID { get; set; }
    
         [Required(AllowEmptyStrings = false)]
         public String Name { get; set; }
    
         [Required(AllowEmptyStrings = false)]
         public String Address { get; set; }
    
     }
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.