9

I have 1 Text field (Check boxes) in a Schema:

Above field have a Custom URL which points to a .NET page.

Values of Check box are each A, B, C.

I want to get and set the value, which is retrieved from the custom URL page.

I confirm to get value, but can't set value to text field (check box).

when setting value, catch below error message.

a.contains is not a function

I confirm to get and set value to simple text field (not check box).

So,I want to set value to text check box field.

I shown below my source code

get value

function getValue() {
var value = "";
var args = window.dialogArguments;
if (args) {
    var test = args.container.getField("test");
    if (test) {
        value = test.getValues();
    }
}
}

set value

function setLValue() {
var value = "";
var args = window.dialogArguments;
if (args) {
    var test = args.container.getField("test");
    if (test) {
        try
        {
          test.setValues([""]);
      }
      catch (e) {
          window.alert(e);        
        }
    }
}
}
8
  • Are you setting a value using one of the options (A, B, or C)? Or are you explicitly trying to set an empty string ("") for field test? Commented Aug 20, 2015 at 22:38
  • Thank you for answering. Of cource, I set a value A , B , C , or empty(""). But anyone is not work.
    – Arai Yuhei
    Commented Aug 21, 2015 at 10:44
  • So if other method (setValues()) exsist, please teach me.
    – Arai Yuhei
    Commented Aug 21, 2015 at 10:45
  • I've only personally used setValues with single-value fields. Since checkboxes are multi-value text fields, maybe try an example from the Tridion "Cookbook": test.setValues(test.getValues().concat("A")). Someone else might know better, though. Commented Aug 21, 2015 at 22:04
  • One more note, the cookbook also mentions a way to handle "Keyword" fields in case A, B, and C come from a Category. Commented Aug 22, 2015 at 8:26

3 Answers 3

7
+50

The error message indicates that your array doesn't have the 'contains' method, which is something that is normally added to the Array prototype by the CME. But since the relevant files are not included in your custom URL page, you cannot just write "[]" and have it work.

The good news is that you can simply read the values first, which will give you an array that has the 'contains' method. Then you can add or replace the values as you see fit.

Example code:

function setLValue() {
        var field = window.dialogArguments.container.getField("test");
        var values = field.getValues();

        // Optional: clear the values
        values.splice(0, values.length);

        values.push("A");
        values.push("C");
        field.setValues(values);
}
3
  • Thank you for answering my question. It did work by You grate proposal method. I solve my quesion. Thank you.
    – Arai Yuhei
    Commented Aug 25, 2015 at 1:20
  • Presumably, in this case, adding your own 'contains' method to Array would also work. Commented Aug 25, 2015 at 7:24
  • It would get you further, for sure -- but it's clearly expecting this type of object so it might just break later on when it tries to use one of the other methods that are added to the prototype. Getting the values first is the safest option. Commented Aug 25, 2015 at 8:30
2

I've tried using your code on my system but even getValues() will always only return me the first value in a multivalue text field. setValues() will only work if I pass a single value or an array with exactly one value; all other scenario's don't set any fields in the GUI.

Based on that I believe you would have to use the Anguilla framework to manipulate your field values; the previously mentioned cookbook has examples for field manipulations.

Edit: further inquiry into the nature of the getValues and setValues functions (sourcecode ftw) has lead me to the following solution:

test.setValues("a,b")

This will successfully set a multivalue checkbox field to both a and b. This must be an exact string match (so if value is A use A and not a).

To clear all values use

test.setValues("null")

It's rather counter-intuitive and afaict riddled with bugs (normal multi value fields don't work, only checkbox fields. Radio buttons are processed as multivalue fields which they are clearly not. Etc.)

1
  • Thank you for answering my question and try to use my source code. I solve above method. values.push("").
    – Arai Yuhei
    Commented Aug 25, 2015 at 1:24
1

I'm not sure but maybe you could set the checkbox this way:

test.getInputControl().item(0).checked = false;

Where test is a fieldbuilder with the checkbox field loaded.

You have to use logic to set true or false in order to set the checks, for example: to set "B":

test.getInputControl().item(1).checked = true;
2
  • 1
    The catch with using the position as in "item(0)" or "item(1)" for the fields is when the list of options or even the type of control changes. Commented Aug 24, 2015 at 22:10
  • Thank you for answering my question. I solve above method. values.push("").
    – Arai Yuhei
    Commented Aug 25, 2015 at 1:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.