1

I am working on a project that relies heavily on Angular for its front-end. I am still in the process of learning Angular, and have come across a complex scenario.

Here is what I have:

Dropdown with options

The above is part of a wizard. Clicking Next moves the user along the wizard. So far so good. I do not want the user to be able to click Next unless they select an option from the drop down.

If they select the Payment Taken option, I would like them to be able to move along the wizard, but instead what is currently happening is this:

Payment Taken option selected in Drop Down

As you can see, the Next button is disabled when I want it to be enabled.

Moving on .. if Payment Deferred is selected, I would like the user to input a reason for why the payment was deferred .. also this reason must be over 10 characters long, if that rule is satisfied, then the Next button should be enabled.

Payment Deferred option selected

The above seems to work fine!

CODE:

<form name="paymentForm">
    <div class="form-group" id="Div2">
        <select ng-model="selectedPaymentStatus" ng-options="ps.name for ps in paymentStatus" required></select>
    </div><!-- .form_group -->

    <div ng-show="selectedPaymentStatus.value === 'paymentDeferred'" class="form-group" id="Div3">
        <p>Reason</p>
        <textarea class="form-control" ng-disabled="selectedPaymentStatus.value === 'paymentTaken'" required ng-model="crate.paymentDeferredReason" ng-minlength="10"></textarea>
    </div><!-- .form_group -->
</form>

<button class="btn wizard-next-btn pull-right" ng-disabled="!paymentForm.$valid" ng-click="nextStep()">Next</button>
<button class="btn wizard-back-btn" ng-click="previousStep()">Back</button>

WHAT I THINK:

I think the reason it is not behaving as expected when I select Payment Taken in the drop down is because I have told my markup that the hidden Reason textarea is also required.

To get over this, I made my Reason text area disabled when the user selects Payment Deferred.

ng-disabled="selectedPaymentStatus.value === 'paymentTaken'"

But this apparently does not override the validation rules set on that DOM element (the textarea); that, or I'm doing something wrong.

1
  • 1
    As a quick idea, can you replace ng-show with ng-if (Angular >= 1.2)? ng-if removes the element and, hopefully, may also retract the validation failure. Commented Jun 24, 2014 at 8:59

2 Answers 2

5

ng-show applies the display: none CSS rule (ref), but the functionality of the content is still present. In your case, the <textarea> remains required, despite the block being hidden.

ng-if on the other hand completely removes its content from the DOM and Angular's hierarchy. The removed ng-model controller will not take part in validation, which is what you want:

<div ng-if="selectedPaymentStatus.value === 'paymentDeferred'" ...>
    <textarea ...>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant, thanks Nikos, you know a lot about Angular :)
0

This may help you

<ng-container *ngIf="selectedPaymentStatus.value === 'paymentDeferred;">
   <div>
      <textarea> Some Text Goes Here </textarea>
   </div>
</ng-container>

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.