mirror of
https://github.com/Nezumi-2711/react-training.git
synced 2026-09-22 20:01:59 +00:00
Fix form validate
This commit is contained in:
@@ -114,7 +114,7 @@ const RoomForm = ({
|
|||||||
{...register('name', {
|
{...register('name', {
|
||||||
required: REQUIRED_FIELD_ERROR,
|
required: REQUIRED_FIELD_ERROR,
|
||||||
validate: {
|
validate: {
|
||||||
checkValidName: (value) => isValidString(value) || INVALID_FIELD,
|
checkValidRoomName: (value) => isValidString(value) || INVALID_FIELD,
|
||||||
},
|
},
|
||||||
onChange: () => trigger('name'),
|
onChange: () => trigger('name'),
|
||||||
})}
|
})}
|
||||||
@@ -129,8 +129,8 @@ const RoomForm = ({
|
|||||||
valueAsNumber: true,
|
valueAsNumber: true,
|
||||||
required: REQUIRED_FIELD_ERROR,
|
required: REQUIRED_FIELD_ERROR,
|
||||||
validate: {
|
validate: {
|
||||||
checkIdentifiedCode: (v) =>
|
checkPrice: (v) =>
|
||||||
isValidRegex(REGEX.NUMBER, v.toString()) || INVALID_FIELD,
|
isValidRegex(new RegExp(REGEX.NUMBER), v.toString()) || INVALID_FIELD,
|
||||||
},
|
},
|
||||||
onChange: () => trigger('price'),
|
onChange: () => trigger('price'),
|
||||||
})}
|
})}
|
||||||
@@ -145,7 +145,7 @@ const RoomForm = ({
|
|||||||
valueAsNumber: true,
|
valueAsNumber: true,
|
||||||
required: REQUIRED_FIELD_ERROR,
|
required: REQUIRED_FIELD_ERROR,
|
||||||
validate: {
|
validate: {
|
||||||
checkPhoneNum: (v) => isValidDiscount(v) || INVALID_DISCOUNT,
|
checkDiscount: (v) => isValidDiscount(v) || INVALID_DISCOUNT,
|
||||||
},
|
},
|
||||||
onChange: () => trigger('discount'),
|
onChange: () => trigger('discount'),
|
||||||
})}
|
})}
|
||||||
|
|||||||
+14
-15
@@ -19,9 +19,7 @@ import FormRow from '@component/LabelControl/index.tsx';
|
|||||||
import Select, { ISelectOptions } from '@component/Select/index.tsx';
|
import Select, { ISelectOptions } from '@component/Select/index.tsx';
|
||||||
|
|
||||||
// Helpers
|
// Helpers
|
||||||
import {
|
import { isEmptyObj, isValidRegex } from '@helper/validators.ts';
|
||||||
isEmptyObj, isValidRegex,
|
|
||||||
} from '@helper/validators.ts';
|
|
||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
import { ADD_SUCCESS, EDIT_SUCCESS } from '@constant/messages.ts';
|
import { ADD_SUCCESS, EDIT_SUCCESS } from '@constant/messages.ts';
|
||||||
@@ -67,9 +65,7 @@ const UserForm = ({ onCloseModal, reload, setReload, user }: IUserFormProp) => {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const load = async () => {
|
const load = async () => {
|
||||||
const options: ISelectOptions[] = [];
|
const options: ISelectOptions[] = [];
|
||||||
const tempUser = !user
|
const tempUser = !user ? { roomId: 0 } : { ...user };
|
||||||
? { roomId: 0 }
|
|
||||||
: { ...user };
|
|
||||||
|
|
||||||
// Load and set default options room
|
// Load and set default options room
|
||||||
if (rooms.length > 0) {
|
if (rooms.length > 0) {
|
||||||
@@ -160,7 +156,8 @@ const UserForm = ({ onCloseModal, reload, setReload, user }: IUserFormProp) => {
|
|||||||
{...register('name', {
|
{...register('name', {
|
||||||
required: REQUIRED_FIELD_ERROR,
|
required: REQUIRED_FIELD_ERROR,
|
||||||
validate: {
|
validate: {
|
||||||
checkValidName: (value) => isValidRegex(REGEX.NAME, value) || INVALID_FIELD,
|
checkValidName: (value) =>
|
||||||
|
isValidRegex(new RegExp(REGEX.NAME), value) || INVALID_FIELD,
|
||||||
},
|
},
|
||||||
onChange: () => trigger('name'),
|
onChange: () => trigger('name'),
|
||||||
})}
|
})}
|
||||||
@@ -178,7 +175,8 @@ const UserForm = ({ onCloseModal, reload, setReload, user }: IUserFormProp) => {
|
|||||||
required: REQUIRED_FIELD_ERROR,
|
required: REQUIRED_FIELD_ERROR,
|
||||||
validate: {
|
validate: {
|
||||||
checkIdentifiedCode: (v) =>
|
checkIdentifiedCode: (v) =>
|
||||||
isValidRegex(REGEX.NUMBER, v.toString()) || INVALID_FIELD,
|
isValidRegex(new RegExp(REGEX.NUMBER), v.toString()) ||
|
||||||
|
INVALID_FIELD,
|
||||||
},
|
},
|
||||||
onChange: () => trigger('identifiedCode'),
|
onChange: () => trigger('identifiedCode'),
|
||||||
})}
|
})}
|
||||||
@@ -192,7 +190,8 @@ const UserForm = ({ onCloseModal, reload, setReload, user }: IUserFormProp) => {
|
|||||||
{...register('phone', {
|
{...register('phone', {
|
||||||
required: REQUIRED_FIELD_ERROR,
|
required: REQUIRED_FIELD_ERROR,
|
||||||
validate: {
|
validate: {
|
||||||
checkPhoneNum: (v) => isValidRegex(REGEX.PHONE, v) || INVALID_PHONE,
|
checkPhoneNum: (v) =>
|
||||||
|
isValidRegex(new RegExp(REGEX.PHONE), v) || INVALID_PHONE,
|
||||||
},
|
},
|
||||||
onChange: () => trigger('phone'),
|
onChange: () => trigger('phone'),
|
||||||
})}
|
})}
|
||||||
@@ -217,12 +216,12 @@ const UserForm = ({ onCloseModal, reload, setReload, user }: IUserFormProp) => {
|
|||||||
</FormRow>
|
</FormRow>
|
||||||
|
|
||||||
<Form.Action>
|
<Form.Action>
|
||||||
<FormBtn type="submit" name="submit" disabled={!isDirty || !isValid || isSubmitting}>
|
<FormBtn
|
||||||
{
|
type="submit"
|
||||||
!user
|
name="submit"
|
||||||
? 'Add'
|
disabled={!isDirty || !isValid || isSubmitting}
|
||||||
: 'Save'
|
>
|
||||||
}
|
{!user ? 'Add' : 'Save'}
|
||||||
</FormBtn>
|
</FormBtn>
|
||||||
<FormBtn type="button" styled="secondary" onClick={onCloseModal}>
|
<FormBtn type="button" styled="secondary" onClick={onCloseModal}>
|
||||||
Close
|
Close
|
||||||
@@ -7,7 +7,7 @@ import { IoExit } from 'react-icons/io5';
|
|||||||
import Modal from '@component/Modal';
|
import Modal from '@component/Modal';
|
||||||
import Table from '@component/Table';
|
import Table from '@component/Table';
|
||||||
import Menus from '@component/Menus';
|
import Menus from '@component/Menus';
|
||||||
import UserForm from './FormUser';
|
import UserForm from './UserForm';
|
||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
import { STATUS_CODE } from '@constant/responseStatus';
|
import { STATUS_CODE } from '@constant/responseStatus';
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
|
|
||||||
// Components
|
// Components
|
||||||
import UserTable from './TableUser';
|
import UserTable from './UserTable';
|
||||||
|
|
||||||
// Styled
|
// Styled
|
||||||
import Button from '@commonStyle/Button';
|
import Button from '@commonStyle/Button';
|
||||||
@@ -10,13 +10,13 @@ import { StyledUser, Title } from './styled';
|
|||||||
|
|
||||||
// Types
|
// Types
|
||||||
import Modal from '@component/Modal';
|
import Modal from '@component/Modal';
|
||||||
import UserForm from './FormUser';
|
import UserForm from './UserForm';
|
||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
import { FORM } from '@constant/commons';
|
import { FORM } from '@constant/commons';
|
||||||
|
|
||||||
const User = () => {
|
const User = () => {
|
||||||
const ADD_ROOM = 'Add room';
|
const TITLE = 'Add user';
|
||||||
const [reload, setReload] = useState(true);
|
const [reload, setReload] = useState(true);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -32,7 +32,7 @@ const User = () => {
|
|||||||
<Button onClick={onCloseModal}>Add user</Button>
|
<Button onClick={onCloseModal}>Add user</Button>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
<Modal.Window name={FORM.USER} title={ADD_ROOM}>
|
<Modal.Window name={FORM.USER} title={TITLE}>
|
||||||
<UserForm setReload={setReload} reload={reload} />
|
<UserForm setReload={setReload} reload={reload} />
|
||||||
</Modal.Window>
|
</Modal.Window>
|
||||||
</Modal>
|
</Modal>
|
||||||
|
|||||||
@@ -5,7 +5,6 @@
|
|||||||
"lib": ["ES2020", "DOM", "DOM.Iterable"],
|
"lib": ["ES2020", "DOM", "DOM.Iterable"],
|
||||||
"module": "ESNext",
|
"module": "ESNext",
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
"types": ["node"],
|
|
||||||
|
|
||||||
/* Bundler mode */
|
/* Bundler mode */
|
||||||
"moduleResolution": "bundler",
|
"moduleResolution": "bundler",
|
||||||
|
|||||||
Reference in New Issue
Block a user