0

I'm trying to create error messages for form validation in multiple languages. Unfortunately, the transfer of the "target" parameter to my function does not work. Maybe target is interpreted as a string?!

function formMessages(field, target) {
  var messages = {
    'de' : {
      'valueMissing': 'Bitte füllen Sie dieses Feld aus.',
      'typeMismatch': {
        'email': 'Bitte geben Sie eine E-Mail ein.',
        'url': 'Bitte geben Sie eine URL ein.'
      }
    }
  };

  // Don't work!
  // return messages.de.target;

  // This works! But it is not dynamic!
  // return messages.de.typeMismatch.email;
}

if (validity.typeMismatch) {
  // Email
  if (field.type === 'email') return formMessages(field, 'typeMismatch.email');
}
3
  • Try to console.log(messages["de"]["valueMissing"]); in order to see if you actually go to the message. Commented Apr 6, 2018 at 8:32
  • stackoverflow.com/questions/6491463/… Commented Apr 6, 2018 at 8:34
  • That's because you are passing string as argument and are using dot notation, instead use [ ] notation and pass 'typeMismatch' , 'email' as two separate arguments or spilt() 'typeMismatch.email' into two Commented Apr 6, 2018 at 8:37

4 Answers 4

2

Use eval Method

return eval('messages.de.'+target);
Sign up to request clarification or add additional context in comments.

Comments

1

try this:

function formMessages(field, target) {
  var messages = {
    de: {
      valueMissing: "Bitte füllen Sie dieses Feld aus.",
      typeMismatch: {
        email: "Bitte geben Sie eine E-Mail ein.",
        url: "Bitte geben Sie eine URL ein."
      }
    }
  };

  return target.split(".").reduce((re, v) => re[v], messages.de);

  // This works! But it is not dynamic!
  // return messages.de.typeMismatch.email;
}

if (validity.typeMismatch) {
  // Email
  if (field.type === "email") return formMessages(field, "typeMismatch.email");
}

messages.de.targetequalsmessages['de']['target'],so target works as string.

If you want target works as variable,it should be messages.de[target].

However,in your case,target is typeMismatch.email,so you have to use reduce to accumulate.

Comments

0

Use bracket notation to access the dynamic property

function formMessages(field) 
{
  var messages = {
    'de' : {
      'valueMissing': 'Bitte füllen Sie dieses Feld aus.',
      'typeMismatch': {
        'email': 'Bitte geben Sie eine E-Mail ein.',
        'url': 'Bitte geben Sie eine URL ein.'
      }
    }
  };

  return messages.de.typeMismatch[ field.type ]; //pass the field.type itself
}

if (validity.typeMismatch) 
{
  if ( field.type === 'email') return formMessages(field); //pass the field itself
}

Comments

0

If you are passing strings as arguments then use [ ] notation and pass 'typeMismatch' , 'email' as two separate arguments.

function formMessages(field, target1, target2) {
  var messages = {
    'de' : {
      'valueMissing': 'Bitte füllen Sie dieses Feld aus.',
      'typeMismatch': {
        'email': 'Bitte geben Sie eine E-Mail ein.',
        'url': 'Bitte geben Sie eine URL ein.'
      }
    }
  };

  return messages.de[target1][target2];

}

if (validity.typeMismatch) {
  // Email
  if (field.type === 'email') return formMessages(field, 'typeMismatch', 'email');
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.