Required Property
The required property in FormEngine Core is a special boolean property that
indicates whether a form field is mandatory. When a component has the required property set to true, it typically displays visual
indicators (like asterisks) and participates in form validation to ensure the field is not left empty.
Understanding the Required Property
The required property is different from standard boolean properties because
it's automatically managed by the validation system. When a component has a required validation rule enabled, the component automatically
receives required: true as a prop. This allows your custom components to adapt their appearance and behavior based on whether they're
mandatory fields.
The Required Annotation
FormEngine Core provides a special required annotation that you can use when defining custom component properties:
import {define, required} from '@react-form-builder/core'
const RequiredInput = ({required, ...props}: any) => {
const label = required ? 'Required' : 'Optional'
return <label>
{label} <input required={required} aria-required={required} {...props} />
</label>
}
export const requiredInput = define(RequiredInput, 'RequiredInput')
.props({
required: required,
})
.build()
The required annotation is a special annotation builder for boolean properties that tells FormEngine this property should be automatically
managed based on validation rules.
Relationship Between Required Property and Validation
The required property gets its value from the component's validation configuration. When a component has the required validation rule
enabled, the component automatically receives required: true as a prop.
Here's how it works:
- Component Definition: You define a component with a
requiredproperty using therequiredannotation - Form Configuration: In your form JSON, you add a
requiredvalidation rule to the component - Runtime Behavior: FormEngine automatically sets
required: trueon the component when it has therequiredvalidation rule - Visual Feedback: Your component can use the
requiredprop to display visual indicators
Example Form Configuration
{
"key": "requiredInput",
"type": "RequiredInput",
"schema": {
"validations": [
{
"key": "required"
}
]
}
}
Live Example
function App() { const RequiredInput = ({required, ...props}) => { const label = required ? 'Required' : 'Optional' return <label> {label} <input required={required} aria-required={required} {...props} /> </label> } const requiredInput = define(RequiredInput, 'RequiredInput') .props({ required: required, }) .build() const view = createView([requiredInput.model]) const formJson = { "form": { "key": "Screen", "type": "Screen", "children": [ { "key": "requiredInput", "type": "RequiredInput", "schema": { "validations": [ { "key": "required" } ] } }, { "key": "optionalInput", "type": "RequiredInput" } ] } } return ( <FormViewer view={view} getForm={() => JSON.stringify(formJson)} /> ) }
In this configuration, the RequiredInput component will automatically receive required: true as a prop because it has the required
validation rule.
Automatic ClassName Addition
FormEngine Core automatically adds the required CSS class to components that have required validation. You can leverage this automatic
className addition in your custom components
Best Practices
-
Always Use the Required Annotation: Use the
requiredannotation instead of the regularbooleanannotation for required properties to ensure proper integration with FormEngine's validation system. -
Provide Visual Feedback: Always provide clear visual indicators when a field is required. Common patterns include:
- Asterisk (*) next to the label
- Different border colors for required fields
- Optional text indicating the field is required
-
Use Semantic HTML Attributes: When implementing your component, use the appropriate HTML attributes:
<input required={required} aria-required={required} /> -
Leverage Automatic ClassName: Use the automatically added
requiredclassName for styling instead of relying only on the prop value:&.required {
/* Your styles for required fields */
} -
Test with Validation: Always test your components with both enabled and disabled required validation to ensure they behave correctly.
-
Consider Accessibility: Ensure your required indicators are accessible to screen readers by using appropriate ARIA attributes and providing text alternatives.
Troubleshooting
Issue: Required Prop is Not Being Passed
Solution: Ensure that:
- You're using the
requiredannotation from@react-form-builder/core - The component has a
requiredvalidation rule in the form JSON - You're accessing the prop correctly in your component implementation
Issue: Required сlassName is Not Being Added
Solution: Check that:
- Your component uses the
classNameproperty correctly - You're not overriding the
classNameproperty manually - The component has the
requiredvalidation rule enabled
Issue: Required Indicator Shows When Field Has Value
Solution: Remember that the required property indicates whether a field is mandatory, not whether it's currently empty. The validation
system handles checking if required fields are filled.
Summary
The required property in FormEngine Core is a powerful feature that allows your custom components to:
- Integrate with validation: Automatically receive
required: truewhen configured with required validation - Provide visual feedback: Display indicators to users about mandatory fields
- Maintain consistency: Work seamlessly with FormEngine's state management and validation systems
By using the required annotation and following the patterns described in this guide, you can create custom components that fully
participate in FormEngine's validation ecosystem while providing excellent user experience.