If I use initialValue like this:
<AutoField name="field1" initialValue={2} />
it does not work if the field is rendered as a SelectField, but works for a TextField.
I know that I can set a defaultValue in the schema, but I use the schema in other parts of the application and don't want to change it there, as the form that I am working on needs different initial values to the other forms that use the same schema.
If I use value instead then the value can't be changed.
Can you have initialValue (or defaultValue if you want to call it that) work for all field types?
Hello once again @todda00,
i could not reproduce this issue. I tested with the following schema:
new SimpleSchema({
size: {
type: String,
allowedValues: ['xs', 's', 'm', 'l', 'xl'],
uniforms: {initialValue: 'xl'}
},
})
and you can test it yourself here: https://uniforms.tools/
Could you be more specific and tell us which theme package you are using, how your schema looks like and how your form component is defined?
Did you use that schema to try to reproduce the error like this?:
<AutoField name="size" initialValue="s" />
It should display "s" initially (instead of "xl" as defined in the schema). I want to override the initial or default values that were defined in the schema (if there were any defaultValue or initialValue specified).
I use the uniforms-bootstrap4 package.
<AutoForm
schema={Collection1.schema}
>
<AutoField name="method" initialValue="L" />
method: {
type: String,
optional: true,
uniforms: {
options: [{ label: "item1", value: "1" }, {label: "location", value: "L" }, { label: "Internet", value: "I" }}]
}
},
After some experimentation, I've found that optional: true causes the uniforms schema initialValue to not even work. When I remove optional: true from the schema, the uniforms schema initialValue works but not initialValue in the JSX.
@tab00: if you'll pass model with this value _(and won't change it's reference!)_ then it behaves like initialValue for the whole form.
Let's make it clear. There's no point in such feature because of: one, another, even easier way to do it (the model prop), two, it'd have to be implemented within schemas, which should have as little custom logic as possible.
@radekmie is there an overview of allowed attributes by JSONSchemaBridge?
I use it like this:
const mySchema = {
title: 'MySchema',
type: 'object',
properties: {
firstName: {
label: 'Fist name',
type: 'string',
},
lastName: {
label: 'Last name',
type: 'string',
},
},
required: ['firstName', 'lastName'],
};
const bridge = new JSONSchemaBridge(mySchema, () => null);
It's easy to use but I am missing the typings for the schema, so I don't know if I can set an initialValue attribute to my lastName property.
You can use default keyword:
...
firstName: {
label: 'Fist name',
type: 'string',
default: 'John'
}
...
Simple as that! For schema typings, you can always check their sources here and see if they are supported in uniforms 馃榿
Thank you. This brought me further! From what I understood, you can also set a uniforms property on the JSON Schema. Like:
...
config: {
label: 'Configuration',
type: 'string',
uniforms: {
component: LongTextField,
},
},
...
Where can I find documentation about the allowed values inside of uniforms?
You can set uniforms and it is nothing fancy, just yet another way to inject props to your component. Allowed values inside of uniforms depend on the component you use, for LongTextField and others you can check here + guaranteed props listed here(except label, name, placeholder but you can set these with other keywords I guess)
If you use ajv to validate your schema, remember to enable uniforms keyword like that:
ajv.addKeyword('uniforms')
Most helpful comment
You can set
uniformsand it is nothing fancy, just yet another way to inject props to your component. Allowed values inside ofuniformsdepend on the component you use, for LongTextField and others you can check here + guaranteed props listed here(exceptlabel,name,placeholderbut you can set these with other keywords I guess)If you use ajv to validate your schema, remember to enable
uniformskeyword like that:ajv.addKeyword('uniforms')