How to make password field optional based on radio button input?


#1

I have built a form that has 3 radio buttons to choose auth methods. The problem arises if i choose ‘no authentication’ The password field then is hidden, but it is still a mandatory field. I can’t make the password field optional since it is of necessity when I choose auth method as ‘password’

Is there any workaround for this?

Below is my schema.

 "type":{
        type: String,
        autoform:{
            type:"select-radio-inline",
            options: function (){
                return [
                    {label: "No Authentication", value: "null"},
                    {label: "Password Authentication", value: "password"},
                    {label: "Crypto Authentication", value: "cryptographic"},
                ];
            },
            defaultValue: function () {
            return Defaults.find({"schemaName":"authentication_schema", "label":"type"},{value:1,_id:0}).map(function(c) {return c.value;})[0];
            }
        }
    },
    "password":{
        type: String,
        autoform:{
            placeholder: "Password, 8 chars long",
            type: "password",
            
        }
    }

Below is the Template

{{> afQuickField name="name" }}
{{> afQuickField name ="type"}}
    {{#if afFieldValueIs name="type" value="password"}}
        {{> afQuickField name="password"}} 
    {{/if}}

#2

You can make the password field optional in your schema then use a custom validation function that only fires under your desired circumstances. See the Make a field conditionally required section of the Simple Schema docs.


#4

@hwillson -

I tried the conditional field validation that you linked, and it works. However, I also tried something like below and they don’t appear to work in the way I’ve implemented. Any thought?

I’ve also tried something like this, but i’m not getting the desired result.

 "condition" :{
        type:String,
        defaultValue: 1
    },

 "testfield": {
    type: String,
     autoform:{
        optional: function () {
               if (this.field('condition').value == 1)
                return true;
        }
    }
}

I’ve tried variations such as :

 "testfield": {
    type: String,
     autoform:{
        optional: function () {
        var isCond = this.field('condition').value == 1;
        if (isCond)
                return true;
        }
    }
}

OR

 "testfield": {
    type: String,
    optional: function () {
        var isCond = this.field('condition').value == 1;
         if (isCond)
                return true;
        }
    }

The only thing that actually works is. I know this will always return true, but this type of if block / assignment works. But the moment I declare a var / or do a field(’ ').value, the form fails. This happens when optional field is declared within the autoform or not.

 "testfield": {
    type: String,
    optional: function () {
        if (isCond = true)
                return true;
        }
    }

#5

Along the same lines, how do I get the same working on objects that are within an array?

Here is a brief example of what I’ve done so far:

There is an array containing objects. Each object has a radio button. I need to extract the value of each radio button. How do I traverse through the array?

test_schema = new SimpleSchema ({
    object:{
        type:Array,
    },
    "object.$":{
        type:Object
    },
    "object.$.condition" :{
        type:String,
        autoform:{
            type: "select-radio-inline",
            options:[{label:'1', value:"one"}]
        },
    },
 "zod": {
    type: String,
     optional:true,
     custom: function () {
         alert(this.field('object').value);
     }
}

I tried an incremental approach where I removed the array definition and just to retrieve the data from objects alone. Something like this worked:

this.field('object.condition').value

However, after encompassing the object within an array, something like this does not work.

this.field('object.$.condition').value

What works is :

this.field('object.0.condition').value

This retrieves the first object’s condition value. How do I, lets say extract other elements?