Validate required fields

Hello,

I would like to check if a field is a required field.
However, my query with fd.field(value).required == true does not work.
What do I have to enter here instead of value?
This is my code for now:

var RequiredMessages = ['Country_x0020_BP', 'Selection_x0020_of_x0020_Busines', 'Ort', 'Land'];

var TranslateRequiredMessagesValidator= {
    name: 'Translate Required Messages',
    error: 'This field is required.',
    validate: function(value){
    if(FormLang == 'de'){
    this.error = 'Dies ist ein Pflichtfeld!';
    }
        if (fd.field(value).required == true && value == null){
           return true;
        }
           return false;
       }

}

for(i=0; i< RequiredMessages.length; i++){
  fd.field(RequiredMessages[i]).addValidator(TranslateRequiredMessagesValidator);
}

Hope you can help me.

Best regards

Dear @Sternchen,
It should work, though you can always use:

if (fd.field(value).required) {

Sadly it didn´t work.
I always get this error when i try to save the form.
image

if (fd.field(value).required && value == null){
return true;
}
return false;
}

Dear @Sternchen,
Hmm, most likely the value as the field name, I've just noticed that - it wouldn't work this way. Value is not the name of the field, and here you can only use the name of the field:

fd.field(value)

I am not even sure why you need such a validator - if a field is required, it will not allow you to save unless field has a value, and vice versa - if it's not required, it will not require value.

What exactly are you trying to do with this code? You can just set fields as required, like this:

fd.field('Country_x0020_BP').required = true;

Or set as not required, like this:

fd.field('Country_x0020_BP').required = false;

I want to show the error message in different languages.
For Example in german.
This is my validator now. Only for one field. But I need it for 4 fields.

fd.field('Business_x0020_partner').addValidator({
name: 'Translate Required Messages',
error: 'This field is required.',
validate: function(value){
if(FormLang == 'de'){
this.error = 'Dies ist ein Pflichtfeld!';
}
if (fd.field(value).required && value == null){
return true;
}
return false;
}

});

If there is a different way to change the error description language depending on a variable I´ll try it.

Dear @Sternchen,
This is definitely not the way to change messages, it's to add extra conditions which are not covered by making the field required.

To change messages, use something like this:

fd.created(function() {
  fd.messages.RequiredValidator_Error = "Dies ist ein Pflichtfeld!"
});

Here you can find all messages to change - JavaScript framework basics — SharePoint forms

You can also try checking current culture, like this:

fd.created(function() {
    if(_spPageContextInfo.currentUICultureName == 'de-de'){
       fd.messages.RequiredValidator_Error = "Dies ist ein Pflichtfeld!"
    }
});

How to create custom forms for different languages, here - SharePoint forms for alternative languages — SharePoint forms