Skip to content

Commit

Permalink
🚸 Add basic form validation rules
Browse files Browse the repository at this point in the history
  • Loading branch information
0x46616c6b committed Apr 28, 2024
1 parent 9c7cf90 commit 090d30a
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 8 deletions.
16 changes: 15 additions & 1 deletion src/components/ticker/form/Description.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,21 @@ const Description: FC = () => {
<Controller
name="description"
control={control}
render={({ field }) => <TextField {...field} margin="dense" maxRows={10} multiline label="Description" required />}
rules={{
required: 'Description is required',
}}
render={({ field, fieldState: { error } }) => (
<TextField
{...field}
error={!!error}
helperText={error?.message ? error.message : null}
margin="dense"
maxRows={10}
multiline
label="Description"
required
/>
)}
/>
)
}
Expand Down
15 changes: 14 additions & 1 deletion src/components/ticker/form/Domain.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,20 @@ const Domain: FC = () => {
<Controller
name="domain"
control={control}
render={({ field }) => <TextField {...field} label="Domain" margin="dense" required disabled={!user?.roles.includes('admin')} />}
rules={{
required: 'Domain is required',
}}
render={({ field, fieldState: { error } }) => (
<TextField
{...field}
label="Domain"
margin="dense"
error={!!error}
helperText={error?.message ? error.message : null}
required
disabled={!user?.roles.includes('admin')}
/>
)}
/>
)
}
Expand Down
11 changes: 10 additions & 1 deletion src/components/ticker/form/Email.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ const Email: FC = () => {
<Controller
name="information.email"
control={control}
render={({ field }) => (
rules={{
required: false,
pattern: {
value: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
message: 'E-Mail is invalid',
},
}}
render={({ field, fieldState: { error } }) => (
<TextField
{...field}
InputProps={{
Expand All @@ -21,6 +28,8 @@ const Email: FC = () => {
</InputAdornment>
),
}}
error={!!error}
helperText={error?.message ? error.message : null}
label="E-Mail"
margin="dense"
/>
Expand Down
11 changes: 10 additions & 1 deletion src/components/ticker/form/Facebook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ const Facebook: FC = () => {
<Controller
name="information.facebook"
control={control}
render={({ field }) => (
rules={{
required: false,
pattern: {
value: /^([a-zA-Z0-9._]+)$/,
message: 'Invalid Facebook username',
},
}}
render={({ field, fieldState: { error } }) => (
<TextField
{...field}
InputProps={{
Expand All @@ -22,6 +29,8 @@ const Facebook: FC = () => {
</InputAdornment>
),
}}
error={!!error}
helperText={error?.message ? error.message : null}
label="Facebook"
margin="dense"
/>
Expand Down
11 changes: 10 additions & 1 deletion src/components/ticker/form/Telegram.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ const Telegram: FC = () => {
<Controller
name="information.telegram"
control={control}
render={({ field }) => (
rules={{
required: false,
pattern: {
value: /^\w{5,32}$/,
message: 'Invalid Telegram username',
},
}}
render={({ field, fieldState: { error } }) => (
<TextField
{...field}
InputProps={{
Expand All @@ -22,6 +29,8 @@ const Telegram: FC = () => {
</InputAdornment>
),
}}
error={!!error}
helperText={error?.message ? error.message : null}
label="Telegram"
margin="dense"
/>
Expand Down
15 changes: 14 additions & 1 deletion src/components/ticker/form/Title.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,20 @@ import { Controller, useFormContext } from 'react-hook-form'
const Title: FC = () => {
const { control } = useFormContext()

return <Controller name="title" control={control} render={({ field }) => <TextField {...field} label="Title" margin="dense" required />} />
return (
<Controller
name="title"
control={control}
rules={{
required: 'Title is required',
minLength: { value: 3, message: 'Title is too short' },
maxLength: { value: 255, message: 'Title is too long' },
}}
render={({ field, fieldState: { error } }) => (
<TextField {...field} error={!!error} helperText={error?.message ? error.message : null} label="Title" margin="dense" required />
)}
/>
)
}

export default Title
11 changes: 10 additions & 1 deletion src/components/ticker/form/Twitter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ const Twitter: FC = () => {
<Controller
name="information.twitter"
control={control}
render={({ field }) => (
rules={{
required: false,
pattern: {
value: /^([a-zA-Z0-9._]+)$/,
message: 'Invalid Twitter username',
},
}}
render={({ field, fieldState: { error } }) => (
<TextField
{...field}
InputProps={{
Expand All @@ -22,6 +29,8 @@ const Twitter: FC = () => {
</InputAdornment>
),
}}
error={!!error}
helperText={error?.message ? error.message : null}
label="Twitter"
margin="dense"
/>
Expand Down
11 changes: 10 additions & 1 deletion src/components/ticker/form/Url.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ const Url: FC = () => {
<Controller
name="information.url"
control={control}
render={({ field }) => (
rules={{
required: false,
pattern: {
value: /^(https?:\/\/)?([a-z0-9-]+\.)+[a-z]{2,}(:\d{1,5})?(\/.*)?$/i,
message: 'Homepage is invalid',
},
}}
render={({ field, fieldState: { error } }) => (
<TextField
{...field}
InputProps={{
Expand All @@ -21,6 +28,8 @@ const Url: FC = () => {
</InputAdornment>
),
}}
error={!!error}
helperText={error?.message ? error.message : null}
label="Homepage"
margin="dense"
/>
Expand Down

0 comments on commit 090d30a

Please sign in to comment.