KnockoutJS validation: Modifying radio button validation message display location(s)
Asked Answered
D

1

6

I'm using Knockout Validation, and when I use it to validate radio button groups, the validation error message appears next to every radio button. I would like it to display in just one location. Presumably I will have to "turn off" automatic display of the validation messages for the radio button group, and then manually display the validation message in a particular location. But... I haven't figured out a way to do that.

Here's a fiddle demonstrating what I'm talking about: http://jsfiddle.net/jbeall/tD4nU/2/

I would like for other input types (e.g., text inputs) to retain the behavior of automatically displaying a message to the right.

How can I set this up?

Thanks!

p.s. For posterity, jsfiddle code is below:

// HTML

<div>
    <div>First name: <input type='text' data-bind='value: firstname'/></div>
    <div>Last name: <input type='text' data-bind='value: lastname'/></div>
</div>
<div>
    Question Type:
    <div>
        <label>
            <input type='radio' value='sales' name='questionType' data-bind="checked: questionType"/>
            Sales
        </label>
    </div>
    <div>
        <label>
            <input type='radio' value='support' name='questionType' data-bind="checked: questionType"/>
            Support
        </label>
    </div>
    <div>
        <label>
            <input type='radio' value='other' name='questionType' data-bind="checked: questionType"/>
            Other
        </label>
    </div>
</div>

<div>
    <input type='button' data-bind='click: triggerGroupValidation' value='Trigger validation via group() function'/>
</div>

<div data-bind='text: ko.toJSON(questionType)'></div>


<div>
    Click button above to update these values
    <div>Error count: <span data-bind='text: errorCount'/></div>
    <div>Error messages: <span data-bind='text: errorMessages' /></div>
</div>


// JavaScript

ko.validation.init({
    insertMessages: true,
    decorateElement: true,
    errorMessageClass: 'app--validation--error-message',
    errorElementClass: 'app--validation--invalid-input-element'
});

var responseOptions = [
                    {
                        "id": 1,
                        "text": "Sales"
                    },
                    {
                        "id": 2,
                        "text": "Support"
                    },
                    {
                        "id": 3,
                        "text": "Other"
                    }
];

var vm = {
    firstname: ko.observable(""),
    lastname: ko.observable(""),
    questionType: ko.observable(''),
    triggerGroupValidation: function(){
        var errors = ko.validation.group(vm, { deep: true, observable: false });
        vm.errorCount(errors().length)
        var stringErrors = '';
        for (var i = 0; i < errors().length; i++) {
            stringErrors = stringErrors + '||' + errors()[i]();
        }
        vm.errorMessages(stringErrors);
        errors.showAllMessages();
    },
    errorCount: ko.observable(0),
    errorMessages: ko.observable('')
};



vm.questionType.extend({
    required: {
        message: "Question type required",
        params: true
    }
});

vm.firstname.extend({
    required: {
        message: "The first name is required",
        params: true
    },
    minLength: {
        message: "The first name is too short",
        params: 3
    },
})
vm.lastname.extend({
    required: {
        message: "The last name is required",
        params: true
    },
    minLength: {
        message: "The last name is too short",
        params: 3
    },
})



ko.applyBindings(vm);
Dammar answered 17/6, 2013 at 16:22 Comment(0)
R
11

You can use the validationOptions binding to overrule the global options for an element. Wrap the radio buttons in a div and add the data-binding validationOptions: {insertMessages: false}. Add an additional span-element for the display of the error message and bind it with: data-bind="validationMessage: questionType":

<div data-bind="foreach: [{val: 'sales', title: 'Sales'}, {val: 'support', title: 'Support'}, {val: 'other', title: 'Other'}], validationOptions: {insertMessages: false}">
    <div><label>
        <input type='radio' name='questionType' data-bind="value: val, checked: $parent.questionType"/>
        <span data-bind="text: title"></span>
    </label></div>
</div>
<span data-bind="validationMessage: questionType" class="app--validation--error-message"></span>

Working example: fiddle

Ryannryazan answered 17/6, 2013 at 21:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.