Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

219
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda