Base solution for your next web application
Open Closed

This is probably a stupid question - but where do I extend form validation on modals? #9111


User avatar
0
marble68 created

I have a checkbox on an entity CreateOrEdit modal, and if it's changed, I need to make one of the inputs required (change the rules).

In the _CreateOrEditModal.js file's init event - I'm doing this:

            _$manualCleared = _modalManager.getModal().find('input[name=wasManuallyCleared]'); // $('#wasManuallyCleared]');
            _$manualCleared.change(function (e) {
                console.log('clear checked');
                console.log(e);
                // if this is set, Taker_wasManuallyClearedReason is required.
                $("#Taker_wasManuallyClearedReason").rules("add", {
                    required: true,
                    minlength: 2,
                    messages: {
                        required: "This field is required",
                        minlength: jQuery.validator.format("More than {0} characters are necessary")
                    }
                })
            });

Thanks for any advice.


1 Answer(s)
  • User Avatar
    0
    marble68 created

    For anyone wondering - this in the right place - but I was doing it wrong.

    The key is to add rules at the end of the init function, after .validate() is called.

    I'm still not sure this is the best place, as I fear my rules will be dumped on an entity regen.

    This worked:

             _$takerInformationForm.validate();
                $("#Taker_wasManuallyClearedReason").rules("add", {
                    required: function () { return ($("#Taker_isclear").is(':checked') != _TakerClearStatusOnLoad) },
                    minlength: 5,
                    messages: {
                        required: "Please provide a reason for changing the clear status",
                        minlength: jQuery.validator.format("At least {0} characters are necessary")
                    }
                });