Skip to content

Commit

Permalink
Merge branch 'master' of github.com:formio/formio.js into additional-…
Browse files Browse the repository at this point in the history
…5x-changes
  • Loading branch information
travist committed Nov 19, 2024
2 parents 259ad94 + 5777d3d commit 61a16bb
Show file tree
Hide file tree
Showing 7 changed files with 597 additions and 8 deletions.
15 changes: 8 additions & 7 deletions src/components/_classes/component/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -3745,12 +3745,6 @@ export default class Component extends Element {
}

shouldSkipValidation(data, row, flags = {}) {
const { validateWhenHidden = false } = this.component || {};
const forceValidOnHidden = (!this.visible || !this.checkCondition(row, data)) && !validateWhenHidden;
if (forceValidOnHidden) {
// If this component is forced valid when it is hidden, then we also need to reset the errors for this component.
this._errors = [];
}
const rules = [
// Do not validate if the flags say not too.
() => flags.noValidate,
Expand All @@ -3761,7 +3755,14 @@ export default class Component extends Element {
// Check to see if we are editing and if so, check component persistence.
() => this.isValueHidden(),
// Force valid if component is hidden.
() => forceValidOnHidden
() => {
if (!this.component.validateWhenHidden && (!this.visible || !this.checkCondition(row, data))) {
// If this component is forced valid when it is hidden, then we also need to reset the errors for this component.
this._errors = [];
return true;
}
return false;
}
];

return rules.some(pred => pred());
Expand Down
4 changes: 4 additions & 0 deletions src/utils/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ export class I18n {

t(text, ...args) {
if (this.currentLanguage[text]) {
const customTranslationFieldName = args[0]?.field;
if (customTranslationFieldName && this.currentLanguage[customTranslationFieldName]) {
args[0].field = this.currentLanguage[customTranslationFieldName]
}
return Evaluator.interpolateString(this.currentLanguage[text], ...args);
}
return Evaluator.interpolateString(text, ...args);
Expand Down
25 changes: 25 additions & 0 deletions test/forms/translationErrorMessages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export default {
name: "textrandom",
path: "textrandom",
type: "form",
display: "form",
components: [
{
label: "My textField",
applyMaskOn: "change",
tableView: true,
validate: {
minLength: 5,
minWords: 2
},
validateWhenHidden: false,
key: "textField",
type: "textfield",
input: true
},
],
created: "2024-11-14T15:52:30.402Z",
modified: "2024-11-15T07:03:41.301Z",
machineName: "glvmkehegcvqksg:text",
}

24 changes: 24 additions & 0 deletions test/unit/Webform.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import webformWithNestedWizard from '../forms/webformWIthNestedWizard';
import formWithUniqueValidation from '../forms/formWithUniqueValidation.js';
import formWithConditionalEmail from '../forms/formWithConditionalEmail.js';
import formsWithSimpleConditionals from '../forms/formsWithSimpleConditionals.js';
import translationErrorMessages from '../forms/translationErrorMessages.js';
const SpySanitize = sinon.spy(FormioUtils, 'sanitize');

if (_.has(Formio, 'Components.setComponents')) {
Expand Down Expand Up @@ -928,6 +929,29 @@ describe('Webform tests', function() {
}).catch(done);
});

it('Should translate field name in error messages', (done) => {
const element = document.createElement('div');
const form = new Webform(element, {
language: 'en',
i18n: {
en: {
'My textField': 'My Value'
},
}
});
form.setForm(translationErrorMessages).then(() => {
const textField = form.getComponent('textField');
textField.setValue('123');
textField.onChange()
setTimeout(() => {
assert.equal(form.errors.length, 2);
assert.equal(form.errors[0].message, 'My Value must have at least 5 characters.');
assert.equal(form.errors[1].message, 'My Value must have at least 2 words.');
done();
}, 300);
}).catch(done);
});

it('Should translate value in franch if _userInput option is provided and value does not present in reserved translation names', done => {
const formElement = document.createElement('div');
const selectLabel = 'Select test label';
Expand Down
Loading

0 comments on commit 61a16bb

Please sign in to comment.