Note that there are some explanatory texts on larger screens.

plurals
  1. POknockout Validation: Any option to apply the errorElementClass to parent of input?
    primarykey
    data
    text
    <p>Does the Knockout validation plugin have any option to apply the error class to the parent or parent's-parent of the input in error? If not can anybody suggest an approach to modify knockout validation to do this?</p> <p>Please see the following fiddle for an example of what I'm trying to achieve. Out of the box knockout validation sets the class of the input, but I want it to instead set the class of the containing control-group:</p> <p><a href="http://jsfiddle.net/fbA4m/1/" rel="noreferrer">http://jsfiddle.net/fbA4m/1/</a></p> <p><strong>View</strong></p> <pre><code>&lt;p&gt;Clear the field and tab out of it to "see" the current behaviour - error class is added directly to the input by knockout validation. But I want to add class "error" to control-group containing the input instead of directly to the input.&lt;/p&gt; &lt;div class="form-horizontal"&gt; &lt;div class="control-group"&gt; &lt;label class="control-label"&gt;Field&lt;/label&gt; &lt;div class="controls"&gt; &lt;input type="text" data-bind="value: testField" class=""&gt; &lt;/div&gt; &lt;/div&gt; &lt;button data-bind="click: setErrorOnControlGroup"&gt;Click to see desired effect&lt;/button&gt; &lt;/div&gt; </code></pre> <p>Javascript</p> <pre><code>ko.validation.configure({ registerExtenders: true, messagesOnModified: true, decorateElement: true, errorClass: 'error', insertMessages: false, parseInputAttributes: true, messageTemplate: null }); var viewModel = { testField: ko.observable("123").extend({required: true}), setErrorOnControlGroup: function() { $("input.error").removeClass("error"); $(".control-group").addClass("error"); } }; ko.applyBindings(viewModel); </code></pre> <p><strong>Stylesheet</strong> (Illustrative only - I shouldn't need this because I want to the use the twitter bootstrap styles)</p> <pre><code>/*NOTE: This is not actually the style I want, it just illustrates the default behaviour of knockout validation*/ input.error { background-color: #aaffaa; } </code></pre> <p>I ask because I'm using twitter bootstrap styling and it uses the input's parent control-group element to style "error" inputs.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload