feat: Add client-side validation for required invite code
Some checks failed
ParentFlow CI/CD Pipeline / Backend Tests (push) Has been cancelled
ParentFlow CI/CD Pipeline / Frontend Tests (push) Has been cancelled
ParentFlow CI/CD Pipeline / Security Scanning (push) Has been cancelled
ParentFlow CI/CD Pipeline / Build Docker Images (map[context:maternal-app/maternal-app-backend dockerfile:Dockerfile.production name:backend]) (push) Has been cancelled
ParentFlow CI/CD Pipeline / Build Docker Images (map[context:maternal-web dockerfile:Dockerfile.production name:frontend]) (push) Has been cancelled
ParentFlow CI/CD Pipeline / Deploy to Development (push) Has been cancelled
ParentFlow CI/CD Pipeline / Deploy to Production (push) Has been cancelled
CI/CD Pipeline / Lint and Test (push) Has been cancelled
CI/CD Pipeline / E2E Tests (push) Has been cancelled
CI/CD Pipeline / Build Application (push) Has been cancelled
Some checks failed
ParentFlow CI/CD Pipeline / Backend Tests (push) Has been cancelled
ParentFlow CI/CD Pipeline / Frontend Tests (push) Has been cancelled
ParentFlow CI/CD Pipeline / Security Scanning (push) Has been cancelled
ParentFlow CI/CD Pipeline / Build Docker Images (map[context:maternal-app/maternal-app-backend dockerfile:Dockerfile.production name:backend]) (push) Has been cancelled
ParentFlow CI/CD Pipeline / Build Docker Images (map[context:maternal-web dockerfile:Dockerfile.production name:frontend]) (push) Has been cancelled
ParentFlow CI/CD Pipeline / Deploy to Development (push) Has been cancelled
ParentFlow CI/CD Pipeline / Deploy to Production (push) Has been cancelled
CI/CD Pipeline / Lint and Test (push) Has been cancelled
CI/CD Pipeline / E2E Tests (push) Has been cancelled
CI/CD Pipeline / Build Application (push) Has been cancelled
- Add validation in onSubmit to check for invite code when required - Display user-friendly error message instead of API error - Prevent form submission until invite code is provided 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -25,7 +25,8 @@ import Link from 'next/link';
|
|||||||
import { useTheme } from '@mui/material/styles';
|
import { useTheme } from '@mui/material/styles';
|
||||||
import apiClient from '@/lib/api/client';
|
import apiClient from '@/lib/api/client';
|
||||||
|
|
||||||
const registerSchema = z.object({
|
// Create a function to generate schema dynamically based on requireInviteCode
|
||||||
|
const createRegisterSchema = (requireInviteCode: boolean) => z.object({
|
||||||
name: z.string().min(2, 'Name must be at least 2 characters'),
|
name: z.string().min(2, 'Name must be at least 2 characters'),
|
||||||
email: z.string().email('Invalid email address'),
|
email: z.string().email('Invalid email address'),
|
||||||
password: z.string()
|
password: z.string()
|
||||||
@@ -34,7 +35,9 @@ const registerSchema = z.object({
|
|||||||
.regex(/[a-z]/, 'Password must contain at least one lowercase letter')
|
.regex(/[a-z]/, 'Password must contain at least one lowercase letter')
|
||||||
.regex(/[0-9]/, 'Password must contain at least one number'),
|
.regex(/[0-9]/, 'Password must contain at least one number'),
|
||||||
confirmPassword: z.string(),
|
confirmPassword: z.string(),
|
||||||
inviteCode: z.string().optional(),
|
inviteCode: requireInviteCode
|
||||||
|
? z.string().min(1, 'Invite code is required')
|
||||||
|
: z.string().optional(),
|
||||||
dateOfBirth: z.string().min(1, 'Date of birth is required'),
|
dateOfBirth: z.string().min(1, 'Date of birth is required'),
|
||||||
parentalEmail: z.string().email('Invalid email address').optional().or(z.literal('')),
|
parentalEmail: z.string().email('Invalid email address').optional().or(z.literal('')),
|
||||||
agreeToTerms: z.boolean().refine(val => val === true, {
|
agreeToTerms: z.boolean().refine(val => val === true, {
|
||||||
@@ -70,7 +73,7 @@ const registerSchema = z.object({
|
|||||||
path: ['dateOfBirth'],
|
path: ['dateOfBirth'],
|
||||||
});
|
});
|
||||||
|
|
||||||
type RegisterFormData = z.infer<typeof registerSchema>;
|
type RegisterFormData = z.infer<ReturnType<typeof createRegisterSchema>>;
|
||||||
|
|
||||||
export default function RegisterPage() {
|
export default function RegisterPage() {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
@@ -89,8 +92,10 @@ export default function RegisterPage() {
|
|||||||
handleSubmit,
|
handleSubmit,
|
||||||
watch,
|
watch,
|
||||||
formState: { errors },
|
formState: { errors },
|
||||||
|
setValue,
|
||||||
|
trigger,
|
||||||
} = useForm<RegisterFormData>({
|
} = useForm<RegisterFormData>({
|
||||||
resolver: zodResolver(registerSchema),
|
resolver: zodResolver(createRegisterSchema(requireInviteCode)),
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
agreeToTerms: false,
|
agreeToTerms: false,
|
||||||
agreeToPrivacy: false,
|
agreeToPrivacy: false,
|
||||||
@@ -146,6 +151,13 @@ export default function RegisterPage() {
|
|||||||
|
|
||||||
const onSubmit = async (data: RegisterFormData) => {
|
const onSubmit = async (data: RegisterFormData) => {
|
||||||
setError(null);
|
setError(null);
|
||||||
|
|
||||||
|
// Validate invite code if required
|
||||||
|
if (requireInviteCode && (!data.inviteCode || data.inviteCode.trim() === '')) {
|
||||||
|
setError('Invite code is required to register');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user