Solution (Name Input)
| type FormValues = { name: string; email: string; }; |
| |
| export function Form() { |
| const { register, handleSubmit, formState: { errors } } = useForm<FormValues>(); |
| const onSubmit: SubmitHandler<FormValues> = (data) => console.log(data); |
| |
| return ( |
| <form onSubmit={handleSubmit(onSubmit)}> |
| <input type="text" placeholder="Dr. React" |
| className={errors?.name?.type ? styles.inputError : ''} |
| {...register('name', { required: { value: true, message: "Name is required!" }, maxLength: { value: 25, message: "The name should be shorter than 25 characters!" } })} /> |
| {errors?.name?.type && <span className={styles.errorMessage}>{errors.name.message}</span>} |
| <input type="submit" value="save" /> |
| </form> |
| ); |
| } |
| type FormValues = { name: string; email: string; }; |
| |
| export function Form() { |
| const { register, handleSubmit, formState: { errors } } = useForm<FormValues>(); |
| const onSubmit: SubmitHandler<FormValues> = (data) => console.log(data); |
| |
| return ( |
| <form onSubmit={handleSubmit(onSubmit)}> |
| <input type="text" placeholder="Dr. React" |
| className={errors?.name?.type ? styles.inputError : ''} |
| {...register('name', { required: { value: true, message: "Name is required!" }, maxLength: { value: 25, message: "The name should be shorter than 25 characters!" } })} /> |
| {errors?.name?.type && <span className={styles.errorMessage}>{errors.name.message}</span>} |
| <input type="submit" value="save" /> |
| </form> |
| ); |
| } |
| type FormValues = { name: string; email: string; }; |
| |
| export function Form() { |
| const { register, handleSubmit, formState: { errors } } = useForm<FormValues>(); |
| const onSubmit: SubmitHandler<FormValues> = (data) => console.log(data); |
| |
| return ( |
| <form onSubmit={handleSubmit(onSubmit)}> |
| <input type="text" placeholder="Dr. React" |
| className={errors?.name?.type ? styles.inputError : ''} |
| {...register('name', { required: { value: true, message: "Name is required!" }, maxLength: { value: 25, message: "The name should be shorter than 25 characters!" } })} /> |
| {errors?.name?.type && <span className={styles.errorMessage}>{errors.name.message}</span>} |
| <input type="submit" value="save" /> |
| </form> |
| ); |
| } |
| type FormValues = { name: string; email: string; }; |
| |
| export function Form() { |
| const { register, handleSubmit, formState: { errors } } = useForm<FormValues>(); |
| const onSubmit: SubmitHandler<FormValues> = (data) => console.log(data); |
| |
| return ( |
| <form onSubmit={handleSubmit(onSubmit)}> |
| <input type="text" placeholder="Dr. React" |
| className={errors?.name?.type ? styles.inputError : ''} |
| {...register('name', { required: { value: true, message: "Name is required!" }, maxLength: { value: 25, message: "The name should be shorter than 25 characters!" } })} /> |
| {errors?.name?.type && <span className={styles.errorMessage}>{errors.name.message}</span>} |
| <input type="submit" value="save" /> |
| </form> |
| ); |
| } |
Solution (E-Mail Input)
| export function Form() { |
| |
| return ( |
| <form onSubmit={handleSubmit(onSubmit)}> |
| /* ... (see previous slide) */ |
| <input |
| type="text" placeholder="react@zuhlke.com" |
| className={errors?.email?.type ? styles.inputError : ''} |
| {...register('email', { |
| required: { value: true, message: "The 'email' field is required!" }, |
| pattern: { value: /^[A-Z0-9._+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i, message: "Invalid email!" } |
| })} |
| /> |
| {errors?.email?.type && <span className={styles.errorMessage}>{errors.email.message}</span>} |
| <input type="submit" value="save" /> |
| </form> |
| ); |
| } |
| export function Form() { |
| |
| return ( |
| <form onSubmit={handleSubmit(onSubmit)}> |
| /* ... (see previous slide) */ |
| <input |
| type="text" placeholder="react@zuhlke.com" |
| className={errors?.email?.type ? styles.inputError : ''} |
| {...register('email', { |
| required: { value: true, message: "The 'email' field is required!" }, |
| pattern: { value: /^[A-Z0-9._+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i, message: "Invalid email!" } |
| })} |
| /> |
| {errors?.email?.type && <span className={styles.errorMessage}>{errors.email.message}</span>} |
| <input type="submit" value="save" /> |
| </form> |
| ); |
| } |