Skip to content

Commit

Permalink
Merge pull request #62 from BUMETCS673/hw27_manage_profile_form_valid…
Browse files Browse the repository at this point in the history
…ation

validate manage profile form
  • Loading branch information
amanda-yee authored Oct 6, 2024
2 parents 202f10f + 93130a5 commit 6068a76
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 3 deletions.
81 changes: 81 additions & 0 deletions code/client/src/__tests__/validateProfileForm.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { validateProfileForm } from '../utils/validateProfileForm';

test('empty input form fields', () => {
const formData = {
name: '', gender: '',
dob: {
year: 1990,
month: 1,
day: 1
},
height: {
feet: 0,
inches: 0
} };
const result = validateProfileForm(formData);
expect(result.isValid).toBe(false);
});

test('name contains invalid characters', () => {
const formData = {
name: 'Test123', gender: 'female',
dob: {
year: 1990,
month: 1,
day: 1
},
height: {
feet: 0,
inches: 0
} };
const result = validateProfileForm(formData);
expect(result.isValid).toBe(false);
});

test('dob year is outside of valid range', () => {
const formData = {
name: 'Test Form', gender: 'female',
dob: {
year: 2030,
month: 1,
day: 1
},
height: {
feet: 0,
inches: 0
} };
const result = validateProfileForm(formData);
expect(result.isValid).toBe(false);
});

test('height (feet) is outside of valid range', () => {
const formData = {
name: 'Test Form', gender: 'female',
dob: {
year: 1997,
month: 12,
day: 31
},
height: {
feet: 10,
inches: 0
} };
const result = validateProfileForm(formData);
expect(result.isValid).toBe(false);
});

test('height (inches) is outside of valid range', () => {
const formData = {
name: 'Test Form', gender: 'female',
dob: {
year: 1997,
month: 12,
day: 31
},
height: {
feet: 5,
inches: -2
} };
const result = validateProfileForm(formData);
expect(result.isValid).toBe(false);
});
2 changes: 1 addition & 1 deletion code/client/src/components/CreateGoal.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function CreateGoal() {
const validateAndSetMessages = (formData) => {
const validationResult = validateGoalForm(formData);
if (!validationResult.isValid) {
setErrorMessage('Error: Please review your inputs and try again');
setErrorMessage(validationResult.message);
return false;
}
return true;
Expand Down
10 changes: 8 additions & 2 deletions code/client/src/components/ManageProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import apiClient from "../services/apiClient.js";
import { Box, Typography, TextField, MenuItem, Button } from "@mui/material";
import { box, bigTitle, inputBackground, menuPropsStyles, submitButton } from "./style/styles.js";
import { authenticated } from "../utils/authenticate.js";
import { validateProfileForm } from "../utils/validateProfileForm.js";

function ManageProfile() {



const [profileData, setProfileData] = useState({
userId: "",
Expand Down Expand Up @@ -74,6 +73,13 @@ function ManageProfile() {
setSuccessMessage('');
setErrorMessage('');

// Validate form
const validationResult = validateProfileForm(profileData);
if (!validationResult.isValid) {
setErrorMessage(validationResult.message);
return; // Prevent form submission
}

try {
await apiClient.post("/api/users/manage-profile", profileData);
console.log("Updating profile")
Expand Down
30 changes: 30 additions & 0 deletions code/client/src/utils/validateProfileForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export function validateProfileForm(formData) {
const { name, gender, dob, height } = formData;

// Checking there is input data in each field
if (!name || !gender || !dob || !height) {
return { isValid: false, message: 'Form field(s) missing' };
}

// Checking validity of name - con only contain alphabet or hyphens
if (!/^[A-Za-z-\s]+$/.test(name)) {
return { isValid: false, message: 'Name contains invalid characters. Only alphabetic characters, hyphens, and spaces allowed.' };
}

// Checking validity of DOB year input
if (dob.year < 1900 || dob.year > 2024) {
return { isValid: false, message: 'DOB year not in valid range. Must be between 1900 and 2024.' };
}

// Checking validity of height inputs
if (height.feet < 0 || height.feet > 7) {
return { isValid: false, message: 'Height (feet) not in valid range. Must be between 0 and 7.' };
}

if (height.inches < 0 || height.inches > 12) {
return { isValid: false, message: 'Height (inches) not in valid range. Must be between 0 and 12.' };
}

// If all validations pass
return { isValid: true, message: 'Form is valid' };
}

0 comments on commit 6068a76

Please sign in to comment.