Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

218
Vistas
How to pass a value to a string inside a JS object variable

I have a simple form that needs validation for user inputs. If the input is too long, I wan't to show an error: "Entered value is too long. Max value is {{max}}.". How can I pass the max value to the error-string inside my dictionary.js file? This file contains all the strings I use in the form.

In the validator.js file, I loop the text fields that have a value and if it exceeds the max, I collect the error message to an array. The error message then shows below the specific field.

The solution below does not work, and it shows a text "[Object object]" below the text field. Any ideas?

dictionary.js

const eng = Object.freeze({
    errors: {
        invalidLength: 'Entered value is too long. Max value is {{max}}.',
        required: 'Mandatory info is missing'
    },
...
...
        

validator.js

const validate = (values) => {
    const errors = {};

    values.forEach(key => {
        const value = key.value;
        const max = key.max;

        if (value.length > max) {
             errors[key.field] = ('errors.invalidLength', {max});
            }
        }
    });

    return errors;
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You could transform your errors object to the following:

dictionary.js

const eng = Object.freeze({
  errors: {
    invalidLength: (maxValue) => `Entered value is too long. Max value is ${maxValue}.`,
    required: 'Mandatory info is missing'
  }
}) 

Then in your validator: validator.js

if (value.length > max) {
  errors[key.field] = eng.errors.invalidLength(max);
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can try updating

  const eng = Object.freeze({
    errors: {
        invalidLength: 'Entered value is too long. Max value is {{max}}.',
        required: 'Mandatory info is missing'
    }

to

const eng = Object.freeze({
    errors: {
        invalidLength: `Entered value is too long. Max value is ${max}.`,
        required: 'Mandatory info is missing'
    }

And,

 if (value.length > max) {
             errors[key.field] = ('errors.invalidLength', {max});
            }

to

if (value.length > max) {
             errors[key.field] = eng.errors.invalidLength;
            }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda