Editform validation - Required fields

Hi,

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 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 />

            <div class="form-group">

                <label for="NameInput">Name</label>

                <InputText class="form-control" id="NameInput" @bind-Value="Person.Name" />

            </div>

            <div class="form-group">

                <label for="AddressInput">Address</label>

                <InputText class="form-control" id="AddressInput" @bind-Value="Person.Address" />

            </div>

            <div>

                <button type="submit" class="btn btn-primary">Submit</button>

            </div>

            @*<ValidationSummary></ValidationSummary>*@

        </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)]

        [StringLength(255)]

        public String Name { get; set; }


        [Required(AllowEmptyStrings = false)]

        [StringLength(255)]

        public String Address { get; set; }


        [Required(AllowEmptyStrings = false)]

        [StringLength(255)]

        public String CreatedBy { get; set; }


        [Required(AllowEmptyStrings = false)]

        [StringLength(255)]

        public String UpdatedBy { get; set; }

    }


1 Reply

SP Sureshkumar P Syncfusion Team January 19, 2022 01:17 PM UTC

Andrew, 
 
You can achieve your requirement by preselect the inputs (from your end insert value textboxes). Please find the code example here: 
<EditForm EditContext="@myEditContext" OnSubmit="@SubmitPerson"> 
 
    <DataAnnotationsValidator /> 
 
    <div class="form-group"> 
 
        <label for="NameInput">Name</label> 
 
        <InputText class="form-control" id="NameInput" @bind-Value="Person.Name" /> 
 
    </div> 
 
    <div class="form-group"> 
 
        <label for="AddressInput">Address</label> 
 
        <InputText class="form-control" id="AddressInput" @bind-Value="Person.Address" /> 
 
    </div> 
    <div class="form-group"> 
 
        <label for="AddressInput">Address</label> 
 
        <InputText class="form-control" id="AddressInput1" @bind-Value="Person.CreatedBy" /> 
 
    </div> 
    <div class="form-group"> 
 
        <label for="AddressInput">Address</label> 
 
        <InputText class="form-control" id="AddressInput2" @bind-Value="Person.UpdatedBy" /> 
 
    </div> 
 
    <div> 
 
        <button type="submit" class="btn btn-primary">Submit</button> 
 
    </div> 
 
    @*<ValidationSummary></ValidationSummary>*@ 
 
</EditForm> 
 
@code { 
 
    public PersonModel Person = new PersonModel(); 
 
    EditContext myEditContext { get; set; } 
 
    private string ValidMEssage; 
 
    protected override void OnInitialized() 
 
    { 
 
        myEditContext = new EditContext(Person); 
 
        base.OnInitialized(); 
 
    } 
 
    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"; 
 
        } 
    } 
 
    public class PersonModel 
    { 
 
        public int ID { get; set; } 
 
        [Required(AllowEmptyStrings = false)] 
 
        [StringLength(255)] 
 
        public String Name { get; set; } 
 
 
 
        [Required(AllowEmptyStrings = false)] 
 
        [StringLength(255)] 
 
        public String Address { get; set; } 
 
 
 
        [Required(AllowEmptyStrings = false)] 
 
        [StringLength(255)] 
 
        public String CreatedBy { get; set; } = "Andrew"; 
 
 
 
        [Required(AllowEmptyStrings = false)] 
 
        [StringLength(255)] 
 
        public String UpdatedBy { get; set; } = "Andrew"; 
 
    } 
 
    } 
 
 
 
Regards, 
Sureshkumar P 


Loader.
Up arrow icon