Question T3046
Visible to All Users

How do I validate on column level with expression in an Matrix drop down?

created 5 years ago

Hello!

I cant seem to get validation on column level with expressions to work. My goal is to verify that two different values from two different columns in matrix drop downs are the same. Ive tried on question level and that works but not on column level. Why is that? I paste my json code where ive tested this. The first two questions is on question level and the next two is on column level:

{
locale: "sv",
pages: [
{
name: "page1",
elements: [
{
type: "matrixdropdown",
name: "Q1",
validators: [
{
type: "expression",
text: "Q1 and Q2 has to be of the same value!",
expression: "{Q1.Row 1.Column 1} = {Q2.Row 1.Column 1}"
}
],
columns: [
{
name: "Column 1",
cellType: "text",
inputType: "number"
}
],
choices: [
1,
2,
3,
4,
5
],
cellType: "text",
rows: [
"Row 1"
]
},
{
type: "matrixdropdown",
name: "Q2",
validators: [
{
type: "expression",
text: "Q1 and Q2 has to be of the same value!",
expression: "{Q1.Row 1.Column 1} = {Q2.Row 1.Column 1}"
}
],
columns: [
{
name: "Column 1",
cellType: "text",
inputType: "number"
}
],
choices: [
1,
2,
3,
4,
5
],
rows: [
"Row 1"
]
},
{
type: "matrixdropdown",
name: "Q1ValidatorOnColumnLevel",
columns: [
{
name: "Column 1",
cellType: "text",
validators: [
{
type: "expression",
text: "Q1ValidatorOnColumnLevel.Row 1.Column 1 has to be of the same value as Q2ValidatorOnColumnLevel.Row 1.Column 1!",
expression: "{Q1ValidatorOnColumnLevel.Row 1.Column 1} = {Q2ValidatorOnColumnLevel.Row 1.Column 1}"
}
],
inputType: "number"
},
{
name: "Column 2",
cellType: "text",
validators: [
{
type: "expression",
text: "Q1ValidatorOnColumnLevel.Row 1.Column 2 has to be of the same value as Q2ValidatorOnColumnLevel.Row 1.Column 2!",
expression: "{Q1ValidatorOnColumnLevel.Row 1.Column 2} = {Q2ValidatorOnColumnLevel.Row 1.Column 2}"
}
],
inputType: "number"
}
],
choices: [
1,
2,
3,
4,
5
],
cellType: "text",
rows: [
"Row 1"
]
},
{
type: "matrixdropdown",
name: "Q2ValidatorOnColumnLevel",
columns: [
{
name: "Column 1",
cellType: "text",
validators: [
{
type: "expression",
text: "Q1ValidatorOnColumnLevel.Row 1.Column 1 has to be of the same value as Q2ValidatorOnColumnLevel.Row 1.Column 1!",
expression: "{Q1ValidatorOnColumnLevel.Row 1.Column 1} = {Q2ValidatorOnColumnLevel.Row 1.Column 1}"
}
],
inputType: "number"
},
{
name: "Column 2",
cellType: "text",
validators: [
{
type: "expression",
text: "Q1ValidatorOnColumnLevel.Row 1.Column 2 has to be of the same value as Q2ValidatorOnColumnLevel.Row 1.Column 2!",
expression: "{Q1ValidatorOnColumnLevel.Row 1.Column 2} = {Q2ValidatorOnColumnLevel.Row 1.Column 2}"
}
],
inputType: "number"
}
],
choices: [
1,
2,
3,
4,
5
],
cellType: "text",
rows: [
"Row 1"
]
}
]
}
]
}

Answers approved by surveyjs Support

created 5 years ago

Hello,
Please use "row" instead of "Q1.Row 1", in this case it will get the value from the current row.

Thank you,
Andrew
SurveyJS Team

    Other Answers

    created 5 years ago

    Thank you for quick reply!

    I was a bit confusing with my question, i try to clarify:

    The first two questions was just to show that validating with an expression on question level works fine. The problem was getting this to work on column level which was the last two questions. Ill post the json of just these two questions:

    {
    locale: "sv",
    pages: [
    {
    name: "page1",
    elements: [
    {
    type: "matrixdropdown",
    name: "Q1ValidatorOnColumnLevel",
    columns: [
    {
    name: "Column 1",
    cellType: "text",
    validators: [
    {
    type: "expression",
    text: "Q1ValidatorOnColumnLevel.Row.Column 1 has to be of the same value as Q2ValidatorOnColumnLevel.Row.Column 1!",
    expression: "{Q1ValidatorOnColumnLevel.Row.Column 1} = {Q2ValidatorOnColumnLevel.Row.Column 1}"
    }
    ],
    inputType: "number"
    },
    {
    name: "Column 2",
    cellType: "text",
    validators: [
    {
    type: "expression",
    text: "Q1ValidatorOnColumnLevel.Row.Column 2 has to be of the same value as Q2ValidatorOnColumnLevel.Row.Column 2!",
    expression: "{Q1ValidatorOnColumnLevel.Row.Column 2} = {Q2ValidatorOnColumnLevel.Row.Column 2}"
    }
    ],
    inputType: "number"
    }
    ],
    choices: [
    1,
    2,
    3,
    4,
    5
    ],
    cellType: "text",
    rows: [
    "Row 1"
    ]
    },
    {
    type: "matrixdropdown",
    name: "Q2ValidatorOnColumnLevel",
    columns: [
    {
    name: "Column 1",
    cellType: "text",
    validators: [
    {
    type: "expression",
    text: "Q1ValidatorOnColumnLevel.Row.Column 1 has to be of the same value as Q2ValidatorOnColumnLevel.Row.Column 1!",
    expression: "{Q1ValidatorOnColumnLevel.Row.Column 1} = {Q2ValidatorOnColumnLevel.Row.Column 1}"
    }
    ],
    inputType: "number"
    },
    {
    name: "Column 2",
    cellType: "text",
    validators: [
    {
    type: "expression",
    text: "Q1ValidatorOnColumnLevel.Row.Column 2 has to be of the same value as Q2ValidatorOnColumnLevel.Row.Column 2!",
    expression: "{Q1ValidatorOnColumnLevel.Row.Column 2} = {Q2ValidatorOnColumnLevel.Row.Column 2}"
    }
    ],
    inputType: "number"
    }
    ],
    choices: [
    1,
    2,
    3,
    4,
    5
    ],
    cellType: "text",
    rows: [
    "Row 1"
    ]
    }
    ]
    }
    ]
    }

      Comments (3)

        Yes, it is more complicated, since you are trying to check values from different matrices. You must use custom functions functionality and register your own function.
        Here is your working example.

        Thank you,
        Andrew
        SurveyJS Team

          Thank you for great support!

            You are welcome!

            Thank you,
            Andrew
            SurveyJS Team