multiselect with required: true

Hello,

I am trying to validate multiselect input: the idea is to select at least one item. required: true is set, however it doesn’t have any effect. At the end of multiselect page, there is a Details section which states:

To validate a multiselect field you should define a custom validation logic. You can’t use a standard validation as it works ONLY with string values and the item operates with arrays.

So far, so good, however, it seems to be triggered only in case when user selects at least one item in multiselect control.

Below you can find a sample form_items structure:

var form_items = [
    {type: 'multiselect', name: 'shipper', label: 'Shippers', required: true, validate: function(data) {
        console.log(data);
    }},
    {type: 'button', name: 'submit', value: 'Submit'}
];

So the question is: How can I force a user to select at least one option?

TIA,

George

Hi

custom user function is ok, but it will not fired if no value selected, logic is like “if nothing selected what to validate then?”

at least one option for multiselect:
{type: “multiselect”, validate: “NotEmpty” }

Hello Andrei,

From the multiselect document:

required - (boolean) adds the * after the label marking the input as mandatory. Also, setting the attribute to true automatically assignes the ‘NotEmpty’ validation rule to the input

Anyway, I tried to put validate: “NotEmpty” as you suggested:

{type: 'multiselect', name: 'shipper', label: 'Shippers', required: true, validate: 'NotEmpty'}

It doesn’t work :frowning:

Any ideas?

Thanks,

George

download latest version from our site?

Andrei,

I am using the latest version from you site. here you can find a sample application which fails.

TIA,

George

Hi,

Attached you can find a complete demo.

TIA,

George

multiselect.tar.gz (739 Bytes)

Hi

finally I got it. the problem occured for compiled version, because both form and grid have validation module with the same names, but isNotEmpty a bit different in a form. in a compiled version it was overwritten by grid’s logic.

for now please use the following fix (add once on init stage after dhtmlx.js loaded):

dhtmlxValidation.isNotEmpty = function(value) { return (value instanceof Array?value.length>0:!value == ''); };

We will include fix into next library update.