formhero/examples/errorsAndValidation.tsx

56 lines
1.2 KiB
TypeScript
Raw Normal View History

2019-09-28 15:20:56 +00:00
import React from 'react'
import ReactDOM from 'react-dom'
import { useForm } from '../'
const Index: React.FC = () => {
2019-10-19 06:45:14 +00:00
const { field, form, errors, isValid } = useForm(
{
username: '',
email: '',
password: '',
},
{
username: value => value.length > 3,
email: {
validator: /@/,
message: 'Must contain an @',
},
password: [
{
validator: /[A-Z]/,
message: 'Must contain an uppercase letter',
},
{
validator: /[\d]/,
message: 'Must contain a digit',
},
],
}
)
return (
<form
onSubmit={e => {
e.preventDefault()
if (isValid) console.log(form)
}}
>
<h1>Errors & Validation</h1>
<input {...field('username')} placeholder="Username" />
{errors.username && 'Must be longer than 3'}
<input {...field('email')} placeholder="EMail" />
{errors.email}
<input {...field('password')} placeholder="Password" type="password" />
{errors.password}
<input type="submit" />
</form>
)
2019-09-28 15:20:56 +00:00
}
2019-10-19 06:45:14 +00:00
ReactDOM.render(<Index />, document.getElementById('errors'))