|
| 1 | +# dynamicOptions |
| 2 | + |
| 3 | +Sometimes a field needs to have properties added/removed when a field has a certain value. This is where `dynamicOptions` comes into play. |
| 4 | +`dynamicOptions` can alter any of a [field's top level properties](../field.md), like `children`, `component`, `fieldOptions` etc. when the given `schema` is `true` or `false`. The behavior is the same as [displayOptions](displayOptions.md), however in contrast to `displayOptions` you can pass in an array of options by using an array. |
| 5 | + |
| 6 | +There are 2 options: Full and Single. |
| 7 | + |
| 8 | +## Full |
| 9 | + |
| 10 | +The full model uses the entire form model as data. |
| 11 | + |
| 12 | +```js |
| 13 | +// Option 1 - full JSON schema |
| 14 | +data() { |
| 15 | + return { |
| 16 | + uiSchema: [{ |
| 17 | + component: 'div', |
| 18 | + children: [{ |
| 19 | + component: 'div', |
| 20 | + dynamicOptions: { |
| 21 | + schema: { |
| 22 | + type: object, |
| 23 | + properties: { |
| 24 | + firstName: { |
| 25 | + type: 'string', |
| 26 | + minLength: 3 |
| 27 | + } |
| 28 | + }, |
| 29 | + required: ['firstName'] |
| 30 | + }, |
| 31 | + options: { |
| 32 | + // any of a field's top level property is valid here |
| 33 | + fieldOptions: { |
| 34 | + // any value that is valid in fieldOptions can be used here |
| 35 | + class: ['text-success'] |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + }] |
| 40 | + }] |
| 41 | + } |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +## Single |
| 46 | + |
| 47 | +The Full option can be a bit verbose when you only rely on a single field's model, and thus you set the `model` property on the `displayOptions` object to only use the value of that field's model. |
| 48 | + |
| 49 | +```js |
| 50 | +// Option 2 - single schema |
| 51 | +data() { |
| 52 | + return { |
| 53 | + uiSchema: [{ |
| 54 | + component: 'div', |
| 55 | + children: [{ |
| 56 | + component: 'div', |
| 57 | + // Here we use an array of dynamic options, but you don't have to do that just to use the single schema |
| 58 | + // You can just as well use an object like in the "Full" example above. |
| 59 | + dynamicOptions: [ |
| 60 | + { |
| 61 | + // Here we set to use the firstName model as the value to evaluate the schema against |
| 62 | + model: 'firstName', |
| 63 | + schema: { |
| 64 | + type: 'string', |
| 65 | + minLength: 3 |
| 66 | + }, |
| 67 | + options: { |
| 68 | + // any of a field's top level property is valid here |
| 69 | + fieldOptions: { |
| 70 | + // any value that is valid in fieldOptions can be used here |
| 71 | + class: ['text-success'], |
| 72 | + domProps: { |
| 73 | + innerHTML: 'Looking good, person with a first name with more than 3 characters!' |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + }, |
| 78 | + { |
| 79 | + model: 'firstName', |
| 80 | + schema: { |
| 81 | + type: 'string', |
| 82 | + maxLength: 2 |
| 83 | + }, |
| 84 | + options: { |
| 85 | + // any of a field's top level property is valid here |
| 86 | + fieldOptions: { |
| 87 | + // any value that is valid in fieldOptions can be used here |
| 88 | + class: ['text-warning'], |
| 89 | + domProps: { |
| 90 | + innerHTML: 'Hey, that is cool! Your first name is less than or equal to 2 characters!' |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + ] |
| 96 | + }] |
| 97 | + }] |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
0 commit comments