Limenius/liform-react

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

validate: props.syncValidation || buildSyncValidation(schema, props.ajv),

A possibility would be to pass a modified version of buildSyncValidation that uses ajv-i18n

const buildSyncValidation = (schema,

https://github.com/epoberezkin/ajv-i18n#usage

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)
            }

        }