formio.js icon indicating copy to clipboard operation
formio.js copied to clipboard

CustomDefaultValue for multivalued textfield not working as expected

Open Vedhachala opened this issue 7 years ago • 5 comments

Hi,

I am trying to set value for a multi-valued textfield dynamically from multi-valued select field. Refresh on select field and clear value on refresh configurations are checked for textfield.

JSON: { "display": "form", "components": [ { "label": "Select", "multiple": true, "reorder": false, "mask": false, "tableView": true, "alwaysEnabled": false, "type": "select", "input": true, "key": "select2", "defaultValue": [], "validate": { "customMessage": "", "json": "", "select": false }, "conditional": { "show": "", "when": "", "json": "" }, "data": { "values": [ { "label": "item1", "value": "item1" }, { "label": "item2", "value": "item2" } ] }, "valueProperty": "value", "selectThreshold": 0.3, "encrypted": false, "properties": {}, "customConditional": "", "logic": [], "lazyLoad": false, "selectValues": "", "disableLimit": false, "sort": "", "reference": false }, { "label": "Text Field", "allowMultipleMasks": false, "showWordCount": false, "showCharCount": false, "multiple": true, "reorder": false, "tableView": true, "alwaysEnabled": false, "type": "textfield", "input": true, "key": "textField2", "defaultValue": [ "" ], "validate": { "customMessage": "", "json": "" }, "conditional": { "show": "", "when": "", "json": "" }, "inputFormat": "plain", "encrypted": false, "properties": {}, "customConditional": "", "logic": [], "customDefaultValue": "value = data.select2;", "refreshOn": "select2", "clearOnRefresh": true, "widget": { "type": "" } }, { "type": "button", "label": "Submit", "key": "submit", "disableOnInvalid": true, "theme": "primary", "input": true, "tableView": true } ] }

GIF of this happening: multi-valued-textfield

Expected another input area to be added with value "item2", but it is joining the array into "item1,item2" in the first input dom.

I dont want to use calculate value as i want to edit the textfield after the default value is set from select field.

Also i can observe the edit , remove and copy buttons getting disappeared for textfield.

Thank You.

Vedhachala avatar May 24 '19 20:05 Vedhachala

I'm actually a little surprised this works at all. The custom default value should only fire when the form is created, not on each change event. The calculated value is where your code should probably be. What are you trying to accomplish?

randallknutson avatar May 24 '19 20:05 randallknutson

My understanding about custom default value is it gets invoked whenever the build() for each field is called , it can be while on load or on refresh.

And i am trying to use it on refresh. (By checking "refresh on" in edit configurations.)

And the same thing is working for two textfields , one get refreshed on the other without using calculate value.

textfields

In my case the data types for both multi valued textfield and select field is the same.(Array).Hence it should work.

My use case:

My select field is conditional, it appears only in few cases.

When it appears the value of the multi valued textfield should contain the values selected in the select, and i can add further items in it too.

But when it is not present i should be able to type in my value in textfield.

With calculate value this is not possible because ,

 I cant edit my textfield at all! The value comes from the code in calculate value.

 when the select is not there it always sets the value of my textfield to be undefined/empty. 

Vedhachala avatar May 25 '19 06:05 Vedhachala

In your calculated value you can do something like:

if (data.selectField) {
  value = data.selectField;
}

That way it isn't setting it to undefined if the select is hidden.

randallknutson avatar May 28 '19 20:05 randallknutson

Hi, How to select one value as default value in an array in formio form component if the array has only one value(using custom default value). If that array has more than one value it should not select default value

ArockiaHenishia-Jerald avatar Jul 11 '21 04:07 ArockiaHenishia-Jerald

Closing this as it is stale, please feel free to reopen at any time. Thank you!

heather-formio avatar Sep 29 '22 16:09 heather-formio