Translate errors
Closed this issue · 3 comments
Hi, after digging a lot into the code, I do not have any clues for translating the errors printed after validation, and there is no parts in the documentation for this. Could you tell us what is the easiest way to translate thoses errors ? thanks a lot
You can pass your own validator to Liform
Line 30 in b249012
A possibility would be to pass a modified version of buildSyncValidation
that uses ajv-i18n
liform-react/src/buildSyncValidation.js
Line 72 in b249012
I've just remembered that I wrote an example of this here https://github.com/nacmartin/liform-daniel-example/blob/master/src/js/Example4.js#L65
with the current buildSyncValidation, why dont add a locale parameter ?
i simply modified the actual buildSyncValidation to :
import localize from 'ajv-i18n' // should be added too
const buildSyncValidation = (schema, ajvParam = null, locale = null ) => {
let ajv = ajvParam
if (ajv === null) {
ajv = new Ajv({ errorDataPath: 'property', allErrors: true, jsonPointers: true })
}
return values => {
const valid = ajv.validate(schema, values)
if (valid) {
return {}
}
const ajvErrors = ajv.errors
if(locale !== null){ //this if is the only thing added with the local props
var validate = ajv.compile(schema);
localize[locale](validate.errors)
}
let errors = ajvErrors.map((error) => {
return setError(error, schema)
})
// We need at least two elements
errors.push({})
errors.push({})
return merge.all(errors)
}
}