0

I am creating an object as following in my Angular Controller. However, I need to make Password and Confirm Password properties conditionally.

I am currently doing it in an if/else statement. If the condition is true execute the following code else execute the same code without password and confirm_password properties.

I found that it is repetition of code. Is there nicer way I can mention properties conditionally inside the object?

$scope.newStudentForm = {
rules: {
firstname: {
    required: true
},
lastname: {
    required: true
},
email: {
    required: true,
    email: true
},
password: {
    required: true,
    minlength: 6
},
confirm_password: {
    required: true,
    minlength: 6,
    equalTo: "#password"
},
student_role: "required"
},
3
  • Why can't you just set required: false, ? Commented Jun 2, 2015 at 7:02
  • 1
    use ternary opertator 'key' : (condition) ? 'value when condition satisfied' : 'value when condition failed' Commented Jun 2, 2015 at 7:18
  • @strikers, thanks this is also nice way. Commented Jun 2, 2015 at 7:51

2 Answers 2

3

Create $scope.newStudentForm without the required properties. then added the on condition

$scope.newStudentForm = {
    rules: {
    }
};
if(condition){
    $scope.newStudentForm.rules.password = {
        required: true,
        minlength: 6
    };
    $scope.newStudentForm.rules.confirm_password = {
        required: true,
        minlength: 6,
        equalTo: "#password"
    };
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your answer. I am trying it out. Once work, i shall mark your answer as Accepts. thanks again
0

I would do it the way Satpal showed, but if you really want to, it is possible to do this within the object initializer, but it's a bit complex: You use spread to spread out either undefined or an object with isProtected:

$scope.newStudentForm = {
    rules: {
        ...(condition
            ?  {
                password: {
                    required: true,
                    minlength: 6
                },
                confirm_password: {
                    required: true,
                    minlength: 6,
                    equalTo: "#password"
                }
            }
            : undefined
         )
    }
};

Live Example:

function example(condition) {
    return {
        rules: {
            ...(condition
                ?  {
                    password: {
                        required: true,
                        minlength: 6
                    },
                    confirm_password: {
                        required: true,
                        minlength: 6,
                        equalTo: "#password"
                    }
                }
                : undefined
             )
        }
    };
}

console.log("Condition false:");
console.log(example(false));
console.log("Condition true:");
console.log(example(true));
.as-console-wrapper {
    max-height: 100% !important;
}

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.