Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2547: React Hook Form validation examples #3680

Merged
merged 1 commit into from
Nov 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions components/doc/autocomplete/validationdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function ValidationDoc(props) {
render={({ field, fieldState }) => (
<>
<label htmlFor={field.name} className={classNames({ 'p-error': errors.name })}>Search*</label>
<AutoComplete id={field.name} {...field} inputRef={field.ref} suggestions={items} completeMethod={search} className={classNames({ 'p-invalid': fieldState.error })} />
<AutoComplete id={field.name} value={field.value} onChange={field.onChange} inputRef={field.ref} suggestions={items} completeMethod={search} className={classNames({ 'p-invalid': fieldState.error })} />
{getFormErrorMessage(field.name)}
</>
)}
Expand Down Expand Up @@ -75,7 +75,7 @@ export default function ValidationDemo() {
<label htmlFor={field.name} className={classNames({ 'p-error': errors.search })}>
Search*
</label>
<AutoComplete id={field.name} {...field} inputRef={field.ref} suggestions={items} completeMethod={search} className={classNames({ 'p-invalid': fieldState.error })} />
<AutoComplete id={field.name} value={field.value} onChange={field.onChange} inputRef={field.ref} suggestions={items} completeMethod={search} className={classNames({ 'p-invalid': fieldState.error })} />
{getFormErrorMessage(field.name)}
</>
)}
Expand Down Expand Up @@ -124,7 +124,7 @@ export default function InvalidDemo() {
<label htmlFor={field.name} className={classNames({ 'p-error': errors.search })}>
Search*
</label>
<AutoComplete id={field.name} {...field} inputRef={field.ref} suggestions={items} completeMethod={search} className={classNames({ 'p-invalid': fieldState.error })} />
<AutoComplete id={field.name} value={field.value} onChange={field.onChange} inputRef={field.ref} suggestions={items} completeMethod={search} className={classNames({ 'p-invalid': fieldState.error })} />
{getFormErrorMessage(field.name)}
</>
)}
Expand Down Expand Up @@ -157,7 +157,7 @@ export default function InvalidDemo() {
<label htmlFor={field.name} className={classNames({ 'p-error': errors.search })}>
Search*
</label>
<AutoComplete id={field.name} {...field} inputRef={field.ref} suggestions={items} completeMethod={search} className={classNames({ 'p-invalid': fieldState.error })} />
<AutoComplete id={field.name} value={field.value} onChange={field.onChange} inputRef={field.ref} suggestions={items} completeMethod={search} className={classNames({ 'p-invalid': fieldState.error })} />
{getFormErrorMessage(field.name)}
</>
)}
Expand Down
8 changes: 4 additions & 4 deletions components/doc/calendar/validationdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function ValidationDoc(props) {
render={({ field, fieldState }) => (
<>
<label htmlFor={field.name} className={classNames({ 'p-error': errors.name })}>Birth Date*</label>
<Calendar id={field.name} {...field} inputRef={field.ref} dateFormat="dd/mm/yy" className={classNames({ 'p-invalid': fieldState.error })} />
<Calendar id={field.name} value={field.value} inputRef={field.ref} onChange={field.onChange} dateFormat="dd/mm/yy" className={classNames({ 'p-invalid': fieldState.error })} />
{getFormErrorMessage(field.name)}
</>
)}
Expand Down Expand Up @@ -65,7 +65,7 @@ export default function ValidationDemo() {
<label htmlFor={field.name} className={classNames({ 'p-error': errors.birthdate })}>
Birth Date*
</label>
<Calendar id={field.name} {...field} inputRef={field.ref} dateFormat="dd/mm/yy" className={classNames({ 'p-invalid': fieldState.error })} />
<Calendar id={field.name} value={field.value} inputRef={field.ref} onChange={field.onChange} dateFormat="dd/mm/yy" className={classNames({ 'p-invalid': fieldState.error })} />
{getFormErrorMessage(field.name)}
</>
)}
Expand Down Expand Up @@ -109,7 +109,7 @@ export default function InvalidDemo() {
<label htmlFor={field.name} className={classNames({ 'p-error': errors.birthdate })}>
Birth Date*
</label>
<Calendar id={field.name} {...field} inputRef={field.ref} dateFormat="dd/mm/yy" className={classNames({ 'p-invalid': fieldState.error })} />
<Calendar id={field.name} value={field.value} inputRef={field.ref} onChange={field.onChange} dateFormat="dd/mm/yy" className={classNames({ 'p-invalid': fieldState.error })} />
{getFormErrorMessage(field.name)}
</>
)}
Expand Down Expand Up @@ -142,7 +142,7 @@ export default function InvalidDemo() {
<label htmlFor={field.name} className={classNames({ 'p-error': errors.birthdate })}>
Birth Date*
</label>
<Calendar id={field.name} {...field} inputRef={field.ref} dateFormat="dd/mm/yy" className={classNames({ 'p-invalid': fieldState.error })} />
<Calendar id={field.name} value={field.value} inputRef={field.ref} onChange={field.onChange} dateFormat="dd/mm/yy" className={classNames({ 'p-invalid': fieldState.error })} />
{getFormErrorMessage(field.name)}
</>
)}
Expand Down
8 changes: 4 additions & 4 deletions components/doc/checkbox/validationdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function ValidationDoc(props) {
render={({ field, fieldState }) => (
<>
<label htmlFor={field.name} className={classNames('mr-2',{ 'p-error': errors.name })}>Accept*</label>
<Checkbox inputId={field.name} {...field} inputRef={field.ref} checked={field.value} className={classNames({ 'p-invalid': fieldState.error })} />
<Checkbox inputId={field.name} value={field.value} onChange={field.onBlur} inputRef={field.ref} checked={field.value} className={classNames({ 'p-invalid': fieldState.error })} />
{getFormErrorMessage(field.name)}
</>
)}
Expand Down Expand Up @@ -65,7 +65,7 @@ export default function ValidationDemo() {
<label htmlFor={field.name} className={classNames('mr-2',{ 'p-error': errors.accept })}>
Accept*
</label>
<Checkbox inputId={field.name} {...field} inputRef={field.ref} checked={field.value} className={classNames({ 'p-invalid': fieldState.error })} />
<Checkbox inputId={field.name} value={field.value} onChange={field.onBlur} inputRef={field.ref} checked={field.value} className={classNames({ 'p-invalid': fieldState.error })} />
{getFormErrorMessage(field.name)}
</>
)}
Expand Down Expand Up @@ -109,7 +109,7 @@ export default function InvalidDemo() {
<label htmlFor={field.name} className={classNames('mr-2',{ 'p-error': errors.accept })}>
Accept*
</label>
<Checkbox inputId={field.name} {...field} inputRef={field.ref} checked={field.value} className={classNames({ 'p-invalid': fieldState.error })} />
<Checkbox inputId={field.name} value={field.value} onChange={field.onBlur} inputRef={field.ref} checked={field.value} className={classNames({ 'p-invalid': fieldState.error })} />
{getFormErrorMessage(field.name)}
</>
)}
Expand Down Expand Up @@ -142,7 +142,7 @@ export default function InvalidDemo() {
<label htmlFor={field.name} className={classNames('mr-2', { 'p-error': errors.accept })}>
Accept*
</label>
<Checkbox inputId={field.name} {...field} inputRef={field.ref} checked={field.value} className={classNames({ 'p-invalid': fieldState.error })} />
<Checkbox inputId={field.name} value={field.value} onChange={field.onBlur} inputRef={field.ref} checked={field.value} className={classNames({ 'p-invalid': fieldState.error })} />
{getFormErrorMessage(field.name)}
</>
)}
Expand Down
16 changes: 13 additions & 3 deletions components/doc/dropdown/validationdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export function ValidationDoc(props) {
<label htmlFor={field.name} className={classNames({ 'p-error': errors.name })}>City*</label>
<Dropdown
id={field.name}
{...field}
value={field.value}
onChange={field.onChange}
focusInputRef={field.ref}
options={cities}
optionLabel="name"
Expand Down Expand Up @@ -123,7 +124,7 @@ export default function InvalidDemo() {
<label htmlFor={field.name} className={classNames({ 'p-error': errors.city })}>
City*
</label>
<Dropdown id={field.name} {...field} focusInputRef={field.ref} options={cities} optionLabel="name" placeholder="Select a City" className={classNames({ 'p-invalid': fieldState.error })} />
<Dropdown id={field.name} value={field.value} onChange={field.onChange} focusInputRef={field.ref} options={cities} optionLabel="name" placeholder="Select a City" className={classNames({ 'p-invalid': fieldState.error })} />
{getFormErrorMessage(field.name)}
</>
)}
Expand Down Expand Up @@ -156,7 +157,16 @@ export default function InvalidDemo() {
<label htmlFor={field.name} className={classNames({ 'p-error': errors.city })}>
City*
</label>
<Dropdown id={field.name} {...field} focusInputRef={field.ref} options={cities} optionLabel="name" placeholder="Select a City" className={classNames({ 'p-invalid': fieldState.error })} />
<Dropdown
id={field.name}
value={field.value}
onChange={field.onChange}
focusInputRef={field.ref}
options={cities}
optionLabel="name"
placeholder="Select a City"
className={classNames({ 'p-invalid': fieldState.error })}
/>
{getFormErrorMessage(field.name)}
</>
)}
Expand Down
2 changes: 1 addition & 1 deletion components/doc/multistatecheckbox/basicdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { useState } from "react";
import { MultiStateCheckbox } from 'primereact/multistatecheckbox';

export default function BasicDoc() {
const [value, setValue] = useState('public');s
const [value, setValue] = useState('public');
const options = [
{ value: 'public', icon: 'pi pi-globe' },
{ value: 'protected', icon: 'pi pi-lock-open' },
Expand Down
176 changes: 176 additions & 0 deletions components/doc/multistatecheckbox/validationdoc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import { useState } from 'react';
import { Controller, useForm } from 'react-hook-form';
import { Button } from '../../lib/button/Button';
import { MultiStateCheckbox } from '../../lib/multistatecheckbox/MultiStateCheckbox';
import { classNames } from '../../lib/utils/Utils';
import { DocSectionCode } from '../common/docsectioncode';
import { DocSectionText } from '../common/docsectiontext';

export function ValidationDoc(props) {
const [formData, setFormData] = useState({});
const defaultValues = { level: '' };
const form = useForm({ defaultValues });
const errors = form.formState.errors;

const options = [
{ value: 'public', icon: 'pi pi-globe' },
{ value: 'protected', icon: 'pi pi-lock-open' },
{ value: 'private', icon: 'pi pi-lock' }
];

const onSubmit = (data) => {
setFormData(data);
};

const getFormErrorMessage = (name) => {
return errors[name] && <small className="p-error ml-2">{errors[name].message}</small>;
};

const code = {
basic: `
<Controller name="level" control={form.control} rules={{ required: 'Level is required.'}}
render={({ field, fieldState }) => (
<>
<label htmlFor={field.name} className={classNames({ 'p-error': errors.name })}>Level*</label>
<MultiStateCheckbox id={field.name} value={field.value} onChange={field.onChange} options={options} optionValue="value" className={classNames({ 'p-invalid': fieldState.error })} />
{getFormErrorMessage(field.name)}
</>
)}
/>
`,
javascript: `
import { useState } from 'react';
import { Controller, useForm } from 'react-hook-form';
import { Button } from '../../lib/button/Button';
import { MultiStateCheckbox } from "primereact/multistatecheckbox";
import { classNames } from '../../lib/utils/Utils';

export default function ValidationDemo() {
const [formData, setFormData] = useState({});
const defaultValues = {level: ''};
const form = useForm({ defaultValues });
const errors = form.formState.errors;

const options = [
{ value: 'public', icon: 'pi pi-globe' },
{ value: 'protected', icon: 'pi pi-lock-open' },
{ value: 'private', icon: 'pi pi-lock' }
];

const onSubmit = (data) => {
setFormData(data);
};

const getFormErrorMessage = (name) => {
return errors[name] && <small className="p-error ml-2">{errors[name].message}</small>
};

return (
<form onSubmit={form.handleSubmit(onSubmit)} className="p-fluid">
<div className="field">
<Controller
name="level"
control={form.control}
rules={{ required: 'Level is required.' }}
render={({ field, fieldState }) => (
<>
<label htmlFor={field.name} className={classNames('mr-2', { 'p-error': errors.level })}>
Level*
</label>
<MultiStateCheckbox id={field.name} value={field.value} onChange={field.onChange} options={options} optionValue="value" className={classNames({ 'p-invalid': fieldState.error })} />
{getFormErrorMessage(field.name)}
</>
)}
/>
</div>
<Button label="Submit" type="submit" icon="pi pi-check" />
</form>
)
}
`,
typescript: `
import { useState } from 'react';
import { Controller, useForm } from 'react-hook-form';
import { Button } from '../../lib/button/Button';
import { MultiStateCheckbox } from "primereact/multistatecheckbox";
import { classNames } from '../../lib/utils/Utils';

export default function InvalidDemo() {
const [formData, setFormData] = useState<any>({});
const defaultValues = {level: ''};
const form = useForm({ defaultValues });
const errors = form.formState.errors;

const options = [
{ value: 'public', icon: 'pi pi-globe' },
{ value: 'protected', icon: 'pi pi-lock-open' },
{ value: 'private', icon: 'pi pi-lock' }
];

const onSubmit = (data: any) => {
setFormData(data);
};

const getFormErrorMessage = (name: string) => {
return errors[name] && <small className="p-error ml-2">{errors[name].message}</small>
};

return (
<form onSubmit={form.handleSubmit(onSubmit)} className="p-fluid">
<div className="field">
<Controller
name="level"
control={form.control}
rules={{ required: 'Level is required.' }}
render={({ field, fieldState }) => (
<>
<label htmlFor={field.name} className={classNames('mr-2', { 'p-error': errors.level })}>
Level*
</label>
<MultiStateCheckbox id={field.name} value={field.value} onChange={field.onChange} options={options} optionValue="value" className={classNames({ 'p-invalid': fieldState.error })} />
{getFormErrorMessage(field.name)}
</>
)}
/>
</div>
<Button label="Submit" type="submit" icon="pi pi-check" />
</form>
)
}
`
};

return (
<>
<DocSectionText {...props}>
<p>
<a href="https://react-hook-form.com/">React Hook Form</a> is the most popular React library for form validation. The field will be highlighted and receive focus on validation failure.
</p>
</DocSectionText>
<div className="card flex justify-content-center">
<div className="flex flex-column gap-2">
<form onSubmit={form.handleSubmit(onSubmit)} className="p-fluid">
<div className="field">
<Controller
name="level"
control={form.control}
rules={{ required: 'Level is required.' }}
render={({ field, fieldState }) => (
<>
<label htmlFor={field.name} className={classNames('mr-2', { 'p-error': errors.level })}>
Level*
</label>
<MultiStateCheckbox id={field.name} value={field.value} onChange={field.onChange} options={options} optionValue="value" className={classNames({ 'p-invalid': fieldState.error })} />
{getFormErrorMessage(field.name)}
</>
)}
/>
</div>
<Button label="Submit" type="submit" icon="pi pi-check" />
</form>
</div>
</div>
<DocSectionCode code={code} />
</>
);
}
Loading