Knockout-Validation icon indicating copy to clipboard operation
Knockout-Validation copied to clipboard

Stop the validation-message to appear between checkbox and it's label

Open Svakinn opened this issue 12 years ago • 8 comments

When the validation is initialized it injects span element for the validation messages immediatley after the input control being validated. In most cases this is exactly what you would prefer.

<input type="text"/><span class="validationMessage" style="display: none;"></span>

When dealing with checkboxes and radio-buttons it is quite customary to have the label after the checkbox and in that case you do not want to show the error-message between the checkbox and the label, but after the label.

<input type="checkbox"/><span class="validationMessage" style="display: none;"></span><label>my text</label>

Another scenario (which is what caused me some problems) is that people are starting styling checkboxes (on CSS3 level) and for such styling to work a label element must be placed directly after the checkbox (see this example here).
This styling then gets disabled when the knockout validation squeezes it validation span between the checkbox and the label.

For that reason I would propose that by default knockout-validation puts the validation element after a label placed immediately after the input control. I modified my kv.utils().insertAfter method to handle this:

insertAfter: function (node, newNode) {
    var nodeNext = node.nextSibling;
    var nextEl = node.nextElementSibling;
    if (nextEl && nextEl.tagName == 'LABEL')
        nodeNext = nextEl.nextSibling;
    node.parentNode.insertBefore(newNode, nodeNext);
    },

This implementation should work in all major browsers but in IE8 the element gets inserted as before right after the input control..

Svakinn avatar Sep 02 '14 20:09 Svakinn

@Svakinn You should use the validationOptions to control where to display the error messages instead. Have a look a this example to see how you can change where to display the validation message. The wiki also has some documentation about this.

crissdev avatar Oct 31 '14 15:10 crissdev

@crissdev Thanks for the example. Yes I am aware of the validationOptions.
It is nice to have if you want the error messages in one place away from the input fields. You can also use it if you need better control where to display the message.
However I like the default option {insertMessages: true} much better.
Why ?..

  1. The error message displays next to the actual error, making it easier for the user to pinpoint the error, especially in a large form.
  2. I do not have to write any javascript code and/or html markup for the message display – (the code you do not have to write is always the best code).
    My point is that the current default error display options do not work to well with radios and checkboxes.
    So the best solution would be to change the default behaviour to handle the correlation between check/radio boxes with their labels.
    My humble opinion is that you may want consider this suggestion, especially if coders like myself are willing to hack the library rather than use the valdationOptions option :)

Having that said I still understand that changing the default behaviour of message display can in some cases have unwanted consequences for input forms already in use.
Therefore the best practice could be to offer configuration flag indicating that automatic error messages should or should not be displayed before following sibling label element.

Svakinn avatar Nov 01 '14 03:11 Svakinn

@Svakinn

I would say it's pretty common to face this kind of issues from time to time. But it's important to take a step back and see why some features are implemented differently than what you might expect.

Having the error message next to the input control matches most of the cases but misses this special case with checkbox / radios. Furthermore, it could be considered an even worse case considering how checkbox can be coded in Bootstrap 3.

I always fallback to validationOptions when it's the case - often when using components. Consider this: if an option is added for this case, then it will be quite common to create configuration options for other cases as well - which may transform the API into something unusable.

ko.validation.insertValidationMessage might be your best option if you have so many checkboxes / radios on your page. But if you don't want to patch the library itself then you can create a fork and add your custom implementation and pull changes from the original repository to keep it in sync.

But don't take my oppinion too serios - the repo owner or other contributers might have a different point of view for this.

crissdev avatar Nov 03 '14 13:11 crissdev

@crissdev I see your point about limiting options and keeping the library as simple as possible.
We do not agree on the importance of the library handling check/radio-boxes validation messages out of the box. You are not selling me the idea that ugly validation message on check/radio-boxes is only an inconvenience, users encounter at a time to time. For me this is a major inconvenience to this library that can be rectified relatively painlessly.
But anyway thanks for taking the time to discuss this issue with me :+1:

Svakinn avatar Nov 04 '14 22:11 Svakinn

@Svakinn I think every aspect covered by the library is important. But it's not possible to match every use case perfectly. I actually created a plunk, where I've used your forementioned code with a bit of Bootstrap 3 specific markup, just to prove my point. As you can see, it couldn't show the message where you would expect. I'm not reluctant to adding functionality - but IMHO this change wouldn't make a difference as there are many variations on how a checkbox / radio can be defined.

crissdev avatar Nov 04 '14 22:11 crissdev

@crissdev Aha... This modification to the node insert method solves that problem:

        insertAfter: function (node, newNode) {
            var nodeNext = node.nextSibling;
            var nextEl = node.nextElementSibling;
            var nodeHandled = false;
            //If next element is label we want to insert after the label
            if (nextEl && nextEl.tagName == 'LABEL') {
                nodeNext = nextEl.nextSibling;
            }
            //If element is within label we want the validation message at the end of that element (at least if current element is check or radiobox)
            else if (node.type === 'checkbox' || node.type === 'radio') {
                var parEl = nextEl.parentNode;
                if (parEl && parEl.tagName == 'LABEL') {
                    node.parentNode.appendChild(newNode);
                    nodeHandled = true;
                }
            }
            if (!nodeHandled)
                node.parentNode.insertBefore(newNode, nodeNext);
        },

Regarding complicated issues where you want to control your validation message, it would also help if you could do that for specific field in a form instead of turning off 'insertMessages' on the form and then having to insert validationmessage bindings for all the fields in the form. For example if you have 100 text-fields and one checkbox.

Svakinn avatar Nov 05 '14 01:11 Svakinn

..... to answer my own point above.. "it would also help if you could do that for specific field in a form instead of turning off 'insertMessages' on the form" .... I see now that I can actually do this..

<div class="panel-body">
  <div class="form-group has-feedback">
    <label class="col-sm-4 control-label" for="login_username" data-bind="text: vm.userTitle"></label>
    <div class="col-sm-8" data-bind="validationOptions: {insertMessages: false}">
      <!-- Turn off auto insert messages for the input field below only-->
      <input id="login_username" class="form-control" type="text" data-bind="value: vm.validModel().userName, valueUpdate: 'afterkeyup'" />
      <span class="glyphicon glyphicon-user form-control-feedback"></span>
      <div class="mySpecialErrorArea">
        <span class="validationMessage" data-bind="validationMessage: vm.validModel().userName"></span>
      </div>
    </div>
  </div>
  <div class="form-group has-feedback">
    <label class="col-sm-4 control-label" for="login_password" data-bind="text: vm.passwTitle"></label>
    <div class="col-sm-8">
      <input id="login_password" class="form-control" type="password" data-bind="value: vm.validModel().password, valueUpdate: 'afterkeyup'" required />
      <!-- Default error message wil be inserted here-->
      <span class="glyphicon glyphicon-lock form-control-feedback"></span>
    </div>
  </div>
</div>

Apparently I managed to misunderstand the documentation in the way that the "insertMessages" validation option was on form level only. This at least solves the problem where you have 100 textboxes and one checkbox in a form.

Perhaps the documentation for the validation options should be clearer so idiots like myself do not misunderstand it :)

Svakinn avatar Nov 05 '14 08:11 Svakinn

@Svakinn I agree we should have more documentation and examples around configuration options.

crissdev avatar Nov 05 '14 09:11 crissdev