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

187-add-form-validation-for-url-text-inputs #266

Merged
merged 2 commits into from
Apr 28, 2024
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
21 changes: 21 additions & 0 deletions src/components/TextInput/TextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getTextInputStyles,
getTextInputDescriptionStyles,
} from "./TextInput.styles";
import React, { useState } from 'react';

export interface TextInputProps
extends TextInputStylesProps,
Expand All @@ -28,6 +29,11 @@ export interface TextInputProps
* Description of the input field.
*/
description?: string;

/**
* Function to validate the input value.
*/
validate?: (value: string) => boolean;
}

export const TextInput: React.FC<TextInputProps> = ({
Expand All @@ -37,9 +43,20 @@ export const TextInput: React.FC<TextInputProps> = ({
description,
srLabel = false,
required,
validate,
...inputProps
}) => {
const describedby = `text-input-description-${inputProps.id}`;
const [error, setError] = useState<string | null>(null);

const handleBlur = (event: React.FocusEvent<HTMLInputElement>) => {
if (validate && !validate(event.target.value)) {
setError("Invalid URL format.");
} else {
setError(null);
}
};

return (
<div>
<label
Expand All @@ -57,8 +74,12 @@ export const TextInput: React.FC<TextInputProps> = ({
describedby,
].join(" ")}
className={getTextInputStyles({ invalid, className })}
onBlur={handleBlur}
/>
</div>
{error && (
<p className="text-red-500 text-sm mt-1">{error}</p>
)}
{description && (
<p
className={getTextInputDescriptionStyles({ invalid })}
Expand Down
8 changes: 8 additions & 0 deletions src/components/forms/hackerApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import {
volunteerSpecificOptions,
} from "@data";
import type { FormInput } from "./types";
import * as z from 'zod';

function isValidUrl(url: string) {
return z.string().url().safeParse(url).success;
}

export const hackerAppFormInputs: FormInput[] = [
{
Expand Down Expand Up @@ -150,6 +155,7 @@ export const mentorSpecificForm: FormInput[] = [
required: false,
id: "linkedin-url",
placeholder: "https://linkedin.com/in/john-smith",
validate: (value: string) => isValidUrl(value) || value === "",
},
name: "linkedinUrl",
},
Expand All @@ -160,6 +166,7 @@ export const mentorSpecificForm: FormInput[] = [
required: false,
id: "github-url",
placeholder: "https://github.com/SherRao",
validate: (value: string) => isValidUrl(value) || value === "",
},
name: "githubUrl",
},
Expand All @@ -170,6 +177,7 @@ export const mentorSpecificForm: FormInput[] = [
required: false,
id: "personal-website-url",
placeholder: "https://hawkhacks.ca",
validate: (value: string) => isValidUrl(value) || value === "",
},
name: "personalWebsiteUrl",
},
Expand Down
14 changes: 14 additions & 0 deletions src/pages/Application/Application.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ function getLogEventName(component: string) {
return "dev_app_interaction"; // not logging the different components becuase it will fill the reports with spam
}

function isValidUrl(url: string) {
return z.string().url().safeParse(url).success;
}

export const ApplicationPage = () => {
// TODO: save steps in firebase to save progress
const [steps, setSteps] = useState<Step[]>([
Expand Down Expand Up @@ -125,6 +129,16 @@ export const ApplicationPage = () => {
const validate = () => {
clearErrors();

// Check URL validation status
let urlFields: (keyof ApplicationData)[] = ['linkedinUrl', 'githubUrl', 'personalWebsiteUrl'] as const;
for (let field of urlFields) {
const fieldValue = application[field as keyof ApplicationData] as string;
if (fieldValue && !isValidUrl(fieldValue)) {
setErrors((prev) => [...prev, `${field} has an invalid URL.`]);
return false;
}
}

// validate step form
const validateFn = stepValidations[activeStep];

Expand Down