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

Fix : When using multilingual option for event fields, all required language fields should be validated [SDESK-6869] #1788

Merged
merged 4 commits into from
Apr 28, 2023
Merged
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
36 changes: 34 additions & 2 deletions client/validators/profile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {get, isEmpty} from 'lodash';
import {gettext} from '../utils';

export const formProfile = ({field, value, profile, errors, messages}) => {
export const formProfile = ({field, value, profile, errors, messages, diff}) => {
// If the field is not enabled or no schema defined, then simply return
if (!get(profile, `editor.${field}.enabled`, false) || !get(profile, `schema.${field}`)) {
return;
Expand Down Expand Up @@ -35,9 +35,41 @@ export const formProfile = ({field, value, profile, errors, messages}) => {
errors[field] = gettext('Too long');
messages.push(gettext('{{ name }} is too long', {name: fieldLabel}));
}
} else if (schema.required && (typeof fieldValue === 'number' ? !fieldValue : isEmpty(fieldValue))) {
} else if (schema.required && !schema.multilingual && (
typeof fieldValue === 'number' ? !fieldValue : isEmpty(fieldValue))) {
errors[field] = gettext('This field is required');
messages.push(gettext('{{ name }} is a required field', {name: fieldLabel}));
} else if (schema.required && schema.multilingual && field !== 'language') {
const multilingualField = diff?.translations?.filter((e) => e.field === field) || [];
const missingLangs = diff?.languages?.filter((lang) => !multilingualField.some((
obj) => obj.language === lang)) || [];
const emptyValues = multilingualField.filter((obj) => obj.value === '');

missingLangs.forEach((qcode) => {
const name = `${fieldLabel} (${qcode})`;
const fieldError = `${field}.${qcode}`;

errors[fieldError] = gettext('This field is required');
messages.push(gettext('{{ name }} is a required field', {name}));
});

emptyValues.forEach(({language}) => {
const name = `${fieldLabel} (${language})`;
const fieldError = `${field}.${language}`;

errors[fieldError] = gettext('This field is required');
messages.push(gettext('{{ name }} is a required field', {name}));
});

Object.keys(errors).forEach((fieldError) => {
const [fieldName, lang] = fieldError.split('.');

if (fieldName === field && diff.languages.includes(lang)) {
if (!missingLangs.includes(lang) && !emptyValues.some((obj) => obj.language === lang)) {
delete errors[fieldError];
}
}
});
} else if (get(schema, 'minlength', 0) > 0 && get(fieldValue, 'length', 0) < schema.minlength) {
if (get(schema, 'type', 'string') === 'list') {
errors[field] = gettext('Not enough');
Expand Down