Validate partial form

2 posts, 0 answers
  1. Daniel Blendea
    Daniel Blendea avatar
    43 posts
    Member since:
    Mar 2010

    Posted 21 Feb 2020 Link to this post

    Hello,

    I have this valdiation summary:

    <div asp-validation-summary="All" class="text-danger" >
        <span class="k-invalid-msg" data-for="Description" data-valmsg-for="Description"
            id="Description_validationMessage" role="alert" >
         </span>
            ... more fields
    </div>
     
    <textarea asp-for="Description" class="form-control mc0" data-bind="value: ent.Description">
    </textarea>

     
    let viewModel = kendo.observable({
     
        save: function() {
            let editForm = $("#edit-form").kendoValidator().data("kendoValidator");
     
            if (editForm.validate()) {
              this.entDataSource.sync();
        }
        },
    })

     

    Because of the business rules, I need to validate only certain fields, based on the current entity state.

    Is there a way to validate only certain fields, like all fields with class "mc0" (or fields in a "div#id-for-state-X") and have the error messages displayed in the summary?

    Thanks.

  2. Petar
    Admin
    Petar avatar
    436 posts

    Posted 24 Feb 2020 Link to this post

    Hi Daniel,

    This Dojo example demonstrates how we can use the submit event to add the "required" attribute to all elements that have the "needValidation" class.

    In the above Dojo, if you click on the "Submit" button the below line will select all DOM elements that have "needValidation" class and will add the "required" attribute to these elements.

    $(".needValidation").attr("required", true);

    After that, the form will be validated based on the presence of the "required" attribute in the different DOM elements. 

    There is also this line in the submit event function:

    $('#ticketsForm').find("[required=\"required\"]").attr("required", false);

    It removes all "required" fields before setting the same attribute to the elements that has the "needValidation" class.

    I hope the above solution will help you resolve the issue in the application you are working on.

    Regards,
    Petar
    Progress Telerik

    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top