Kendo Grid - Data Annotations Validations

8 posts, 0 answers
  1. Omar
    Omar avatar
    9 posts
    Member since:
    Aug 2013

    Posted 03 Sep 2013 Link to this post

    I am working on a UI where I am editing grid in an Ajax Popup Edit. The following data annotations do not work as expected in the popup edit:

     [DataType(DataType.Password)]
            [Display(Name = "Confirm password")]
            [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
            public string ConfirmPassword { get; set; }

            [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
            [DataType(DataType.Password)]
            [Display(Name = "Password")]
            public string Password { get; set; }

            [StringLength(500, ErrorMessage = "Image path cannot be more than 500 characters.")]
            [Display(Name = "Image path")]
            [DataType(DataType.ImageUrl)]
            [RegularExpression(@"^http(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$", ErrorMessage = "Image path format is incorrect.")]
            public string ImagePath { get; set; }

    Currently for Add/Edit both, I am getting a minimum password length should be 6. Also both for Add and Edit I get "Image path format is incorrect" even when there is nothing typed in that field

    In addition to the above
    On Edit - I do not want the password to be required. On New I want password to be required.

    Thanks
  2. Daniel
    Admin
    Daniel avatar
    2231 posts

    Posted 05 Sep 2013 Link to this post

    Hi,

    I am not sure if I understand correctly the issue with the length validation. Six seems to be used for the attribute MinLength value. Could you clarify a bit?
     I reproduced the problem with the regular expression validation and we will fix it for one of the next internal builds. A workaround for now is to override the mvcregex rule:

    (function ($, kendo) {
        $.extend(true, kendo.ui.validator, {
            rules: { // custom rules
                mvcregex: function (input, params) {
                    //check for the rule attribute
                    if (input.filter("[data-val-regex]").length && input.val()) {
                        var regex = new RegExp(input.attr("data-val-regex-pattern"));
                        return regex.test(input.val());
                    }
                    return true;
                }
            }
        });
    })(jQuery, kendo);
    Using conditional validation based on the mode is not supported by the Validator. You should implement a custom attribute with a client side rule that gets the model for the row in edit mode and checks if it is new. Another option would be to remove the data-val-required attribute from the input in the Grid edit event:
    function onEdit(e) {
        if (!e.model.isNew()) {
            $("#Password").removeAttr("data-val-required");
        }
    }
    but note that the server validation will still be triggered in the updated action in this case.

    Regards,
    Daniel
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. Omar
    Omar avatar
    9 posts
    Member since:
    Aug 2013

    Posted 05 Sep 2013 Link to this post

    Note from the validator for password that the password is not required (on either add or edit).

    But if the code below is used in an Ajax poupedit, and when tried to submit, I get a minimum password length 6 message which I really should not if the field is left empty.

    Regards
  4. Daniel
    Admin
    Daniel avatar
    2231 posts

    Posted 09 Sep 2013 Link to this post

    Hello again,

    Thank you for clarifying. I reproduced this problem as well and we will fix it. As a small sign of gratitude for bringing these problems to our attention, I also updated your Telerik points.

    Regards,
    Daniel
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  5. Anurag
    Anurag avatar
    4 posts
    Member since:
    Jan 2011

    Posted 11 Nov 2013 Link to this post

    Hi - I created a custom Regex validation and using it as a Data Annotation in our entity but it does not work through Kendo Popup Edit. Is it related to the problem above? Should we be using an alternate solution?

  6. Daniel
    Admin
    Daniel avatar
    2231 posts

    Posted 13 Nov 2013 Link to this post

    Hello,

    Is the attribute using the same client-side attribute name? If not then you should extend the validator with a rule that handles inputs with the used attribute.

    Regards,
    Daniel
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  7. Gentry
    Gentry avatar
    1 posts
    Member since:
    Jan 2011

    Posted 26 Feb 2014 in reply to Daniel Link to this post

    Daniel....has this bug been fixed yet?
    I'm still receiving similar errors...
  8. Daniel
    Admin
    Daniel avatar
    2231 posts

    Posted 28 Feb 2014 Link to this post

    Hello,

    Which problem are you experiencing? The problem wit the not required fields should be fixed.


    Regards,
    Daniel
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Back to Top