Import
import { Field } from '@dnb/eufemia/extensions/forms'render(<Field.Name />)render(<Field.Name.First />)render(<Field.Name.Last />)render(<Field.Name.Company />)
Description
Field.Name is a wrapper component for the input of strings, with user experience tailored for first name, last name and company names.
There is a corresponding Value.Name component.
import { Field, Form } from '@dnb/eufemia/extensions/forms'function MyForm() {return (<Form.Handler><Field.Name /><Field.Name.First value="Nora" /><Field.Name.Last value="Mørk" /><Field.Name.Company value="DNB" /></Form.Handler>)}
Relevant links
Sources
Characteristics
Allowed characters
Field.Name.FirstandField.Name.Last: Only letters, hyphens, and spaces are allowed.Field.Name.Company: Letters, numbers, punctuation marks, spaces, and dots are allowed.
Behavior
- Trailing spaces are automatically removed.
- The HTML
autocompleteattribute is automatically set:nameforField.Namegiven-nameforField.Name.Firstfamily-nameforField.Name.LastorganizationforField.Name.Company
Validation rules
All name fields have the following validation rules:
- Minimum length:
- For
Field.Name.FirstandField.Name.Last: Names must be at least 1 character long. - For
Field.Name.Company: Company names must be at least 3 characters long.
- For
- Pattern validation:
- For
Field.Name.FirstandField.Name.Last: Names must start and end with a letter, and cannot contain consecutive hyphens or spaces. Only letters, spaces, and hyphens are allowed. - For
Field.Name.Company: Must start and end with a letter or number, and cannot contain consecutive hyphens, spaces, or dots. Letters, numbers, punctuation marks, spaces, and dots are allowed in between.
- For
The validation happens on blur, internally using the onBlurValidator property.
Note: The validation patterns are tailored to Norwegian name and company name requirements. If you need support for additional characters or different validation rules, you can extend the validation using the onBlurValidator property. See the Validators section below for more information.
Validators
Internal validators exposed
Field.Name and Field.Name.Company expose validators through their onBlurValidator property:
-
nameValidator: Validates names forField.Name,Field.Name.First, andField.Name.Last. It checks that the name:- Is at least 1 character long.
- Matches the name pattern (starts and ends with a letter, no consecutive hyphens or spaces).
-
companyValidator: Validates company names forField.Name.Company. It checks that the company name:- Is at least 3 characters long (default, can be customized via
minLengthprop). - Matches the company pattern (starts and ends with a letter or number, no consecutive hyphens, spaces, or dots).
- Is at least 3 characters long (default, can be customized via
You can extend the validation by providing your own onBlurValidator function. Access the internal validator through the validators parameter and combine it with your custom validation. This allows you to add additional validation rules while keeping the default validation intact.
import { Field } from '@dnb/eufemia/extensions/forms'// Extend validation to require company names to contain "Corp"const myValidator = (value, { validators }) => {const { companyValidator } = validatorsconst customValidator = (value: string) => {if (value && !value.includes('Corp')) {return new Error('Company name must contain "Corp"')}}return [companyValidator, customValidator]}render(<Field.Name.Company onBlurValidator={myValidator} />)