Hidden Fields
Hidden fields let you include data that cannot be seen or modified by users when a form is submitted. A good example is a CSRF token or an ID for the content being submitted.
As such, they do not have most of the features of other fields.
Features
- Offers a
useHiddenField
composable and a renderlessHiddenField
component.
Anatomy
Hidden Field Component
Unlike other fields, you can use the HiddenField
component to create a hidden field without having to create your own components. This is because we view the hidden field as a utility field for declaring values declaratively.
Alternatively, you can use the useHiddenField
composable to create a custom hidden field component.
To demonstrate how hidden fields work, we will use a form
in the following example. We will try to keep it as simple as possible, but you can check the form guide for more information.
Validation
Hidden fields do not support validation, although their API supports showing any possible error messages that the form might have for the field. The main reason for this is that hidden fields are, well, hidden. That means the user cannot interact with them, and as such, you should ensure they always have valid values; otherwise, it will be confusing to the user.
Usage
Disabled
Marking a hidden field as disabled will make it non-submittable, meaning its value will not be included in the form data when the form is submitted.
API
Props
These are the properties that can be passed to the useHiddenField
composable and the HiddenField
component.
Name | Type | Description |
---|---|---|
disabled | Whether the hidden field is disabled. | |
name | The name attribute for the hidden field. | |
value | The value of the hidden field. |
Returns
These are the properties in the object returned by the useHiddenField
composable.
Name | Type | Description |
---|---|---|
displayError | Display the error message for the field. | |
errorMessage | The error message for the field. | |
errors | The errors for the field. | |
fieldValue | The value of the field. | |
isDirty | Whether the field is dirty. | |
isDisabled | Whether the field is disabled. | |
isTouched | Whether the field is touched. | |
isValid | Whether the field is valid. | |
setErrors | (messages: Arrayable<string>) => void | Sets the errors for the field. |
setTouched | Sets the touched state for the field. | |
setValue | Sets the value for the field. |