简体   繁体   中英

Knockout validation on select lists with no data-bind 'option' or 'caption'

Is it possible to have knockout validation on a select list that does not include a data-bind 'option' or 'optionCaption'?

My select looks like the following:

<select data-bind="value: viewModels.vm.MyValue">
                        <option>--Please Select--</option>
                        <option value="yes">Yes</option>
                        <option value="no">No</option>
                    </select>

My validation looks like:

viewModels.vm.MyValue= ko.observable().extend({
        required: {
            params: true,
            message: " (you must make a selection)"
        }
    });

I was hoping that as the top option did not have a value then the validation would kick in.

Is there any way of telling the validation if it is a certain record to ignore it?

I cannot actually bind the values to the observable as the model is being passed in and mapped via json

Any help would be great

Since your first <option> doesn't define a value attribute, the browser uses the caption ("--Please Select--") as the element's value, and the validator rightfully treats it as valid.

Try this:

<option value="">--Please Select--</option>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM