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

Validate project name while creating/editing project #6376

Merged
merged 4 commits into from
May 8, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions frontend/src/components/projectCreate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ const ProjectCreate = () => {

const handleCreate = useCallback(
(cloneProjectData) => {
if (!metadata.projectName.trim()) {
setErr({ error: true, message: intl.formatMessage(messages.noProjectName) });
throw new Error('Missing project name.');
}
if (!/^[a-zA-Z]/.test(metadata.projectName)) {
setErr({ error: true, message: intl.formatMessage(messages.projectNameValidationError) });
throw new Error('Project name validation error.');
}
if (!metadata.geom) {
setErr({ error: true, message: intl.formatMessage(messages.noGeometry) });
throw new Error('Missing geom.');
Expand Down
8 changes: 8 additions & 0 deletions frontend/src/components/projectCreate/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ export default defineMessages({
id: 'management.projects.create.errors.closed_linestring',
defaultMessage: 'Points do not form a closed linestring',
},
noProjectName: {
id: 'management.projects.create.errors.no_project_name',
defaultMessage: 'Name is a required field.',
},
projectNameValidationError: {
id: 'management.projects.create.errors.project_name_validation_error',
defaultMessage: 'Project name should start with an alphabet.',
},
noGeometry: {
id: 'management.projects.create.errors.no_geometry',
defaultMessage: "You need to define the project's area of interest.",
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/components/projectEdit/messages.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { defineMessages } from 'react-intl';

import projectCreateMessages from '../projectCreate/messages';

/**
* Internationalized messages for use on project edit.
*/
Expand Down Expand Up @@ -439,6 +441,7 @@ export default defineMessages({
id: 'projects.formInputs.name',
defaultMessage: 'Name of the project',
},
projectNameValidationError: { ...projectCreateMessages.projectNameValidationError },
dueDate: {
id: 'projects.formInputs.dueDate',
defaultMessage: 'Due date',
Expand Down
25 changes: 22 additions & 3 deletions frontend/src/views/projectEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ export function ProjectEdit() {
});
} else {
const mandatoryFieldsMissing = mandatoryFields.filter(
(m) => Object.keys(defaultLocaleInfo).includes(m) === false || defaultLocaleInfo[m] === '',
(m) =>
Object.keys(defaultLocaleInfo).includes(m) === false ||
defaultLocaleInfo[m].trim() === '',
);
if (mandatoryFieldsMissing.length) {
missingFields.push({
Expand All @@ -130,7 +132,6 @@ export function ProjectEdit() {
});
}
}

const nonLocaleMissingFields = [];
if (projectInfo.mappingTypes.length === 0) nonLocaleMissingFields.push('mappingTypes');
const { mappingEditors, validationEditors, customEditor } = projectInfo;
Expand Down Expand Up @@ -160,7 +161,17 @@ export function ProjectEdit() {
) {
missingFields.push({ type: 'noTeamsAssigned' });
}

// validate name
if (!missingFields?.[0]?.fields?.includes('name')) {
const projectName = defaultLocaleInfo.name;
if (!/^[a-zA-Z]/.test(projectName)) {
missingFields.push({
locale: projectInfo.defaultLocale,
fields: ['projectNameValidationError'],
type: 'nameValidationError',
});
}
}
if (missingFields.length > 0) {
setError(missingFields);
return new Promise((resolve, reject) => reject());
Expand Down Expand Up @@ -350,6 +361,14 @@ const ErrorTitle = ({ locale, numberOfMissingFields, type, projectInfo }) => {
/>
);
}
if (type === 'nameValidationError') {
return (
<FormattedMessage
id="management.projects.create.errors.project_name_validation_error"
defaultMessage="Project Name Validation Error"
/>
);
}
if (locale === null) {
return (
<FormattedMessage {...messages.missingFields} values={{ number: numberOfMissingFields }} />
Expand Down