Instead of setting error messages, adding them to the existing list.
errorMessages
(Array.<Object>): A list of error message objects with the following properties:fieldName
(String): Field name.errorMessages
(Array.<String|ReactComponent>): A list of error messages.
form.addErrors([
{fieldName: 'firstName', errorMessages: ['This field is required']},
{fieldName: 'lastName', errorMessages: ['This field is required']}
]);
Clears errors in the given field names.
fieldNames
(Array.<String>): A list of field names.
form.clearErrors(['firstName', 'lastName']);
Clears the entire form, resetting it to the uninitialized state. This means setDefaultValue()
will work again.
form.clearForm();
Set the form enabled state to false, this disables the entire form. For more information, please see Disabling Fields.
form.disable();
Disables a specific field. For more information, please see Disabling Fields.
fieldName
(String): Field name to be disabled.
import { createForm } from 'soya-form';
export default createForm('FORM_ID')(({ form }) => (
<button onClick={() => form.disableField('lastName')}>Disable</button>
));
Disable all fields included in fieldNameList
. For more information, please see Disabling Fields.
fieldNameList
(Array.<String>): A list of field names to be disabled.
import { createForm } from 'soya-form';
export default createForm('FORM_ID')(({ form }) => (
<button onClick={() => form.disableFields(['firstName', 'lastName'])}>Disable Fields</button>
));
Set the form enabled state to true, this enables the form. For more information, please see Disabling Fields.
form.enable();
Enables a specific field. For more information, please see Disabling Fields.
fieldName
(String): Field name to be enabled.
import { createForm } from 'soya-form';
export default createForm('FORM_ID')(({ form }) => (
<button onClick={() => form.enableField('lastName')}>Enable</button>
));
Enable all fields included in fieldNameList
. For more information, please see Disabling Fields.
fieldNameList
(Array.<String>): A list of field names to be enabled.
import { createForm } from 'soya-form';
export default createForm('FORM_ID')(({ form }) => (
<button onClick={() => form.enableFields(['firstName', 'lastName'])}>Enable Fields</button>
));
Get the form ID.
(String): Form ID given to the form.
import { createForm } from 'soya-form';
export default createForm('FORM_ID')(({ form }) => form.getFormId());
Disables the entire form for submission. This is a state different from field and form enabled status. For more information, please see Disabling Fields.
form.lockSubmission();
Same as setValue()
, however this won't actually set the value if the field is already initialized in redux state.
fieldName
(String): Field name.value
(Any): Any field value.
form.setDefaultValue('firstName', 'Anri');
Same as setValues()
, however this won't actually set the value if the field is already initialized in redux state.
values
(Array.<Object>): An array of object with the following properties:fieldName
(String): Field name.value
(Any): Any field value.
form.setDefaultValues([
{fieldName: 'firstName', value: 'Anri'},
{fieldName: 'lastName', value: 'of Astora'}
]);
Sets error messages to multiple fields. errorMessages
is a list of error messages (strings).
fieldName
(String): Field name.errorMessages
(Array.<String|ReactComponent>): A list of error messages.
form.setErrors('nickname', [
'Must not contain special characters',
'Must be more than three letters'
]);
Sets value to the field with the given name. Will always override.
fieldName
(String): Field name.value
(Any): Any field value.
form.setValue('firstName', 'Anri');
Batch set value to form.
values
(Array.<Object>): An array of object with the following properties:fieldName
(String): Field name.value
(Any): Any field value.
form.setValues([
{fieldName: 'firstName', value: 'Anri'},
{fieldName: 'lastName', value: 'of Astora'}
]);
Disables the form and runs all sync and async validations on all fields registered to the form.
callback
(Function): A function that accepts the validation result as the sole argument. It'll be called regardless of validation result.- [
formWideValidation
] (Function): Useful if you want to do validations that deal with multiple fields at once (password confirmation fields, for example). This function will be called only if the values pass sync and async validations of each field. The function accepts a single argument, the values map itself, and must return a validation result structure (see examples for more detail).
import { createForm } from 'soya-form';
const callback = function(result) {
if (result.isValid) {
// You can access form values with result.values
}
};
const formWideValidation = function(data) {
if (data.password !== data.passwordConfirm) {
return {
isValid: false,
errorMessages: [
{fieldName: 'passwordConfirm', errorMessages: ['Value must be the same as password field.']}
]
};
}
return {isValid: true, errorMessages:{}};
};
export default createForm('FORM_ID')(({ form }) => (
<button
type='submit'
onClick={() => form.submit(callback, formWideValidation)}
>
Submit
</button>
));
Enables the entire form for submission. This is a state different from field and form enabled status. For more information, please see Disabling Fields.
form.unlockSubmission();