ember/handlebars - How to toggle object class based on boolean?

542 Views Asked by At

I have a form styled with bootstrap4 and am using ember-cp-validations to validate it.

  <div class="form-group {{if showNameError 'has-danger' ''}}">
    <label for="name" class="cols-sm-2 control-label">Full Name</label>
    <div class="cols-sm-10">
      <div class="input-group">
        <span class="input-group-addon">{{fa-icon "user"}}</span>
        {{input id="name" class="form-control" value=user.firstname placeholder="Enter your Name" focus-out=(action (mut showNameError) true)}}
      </div>
    </div>

    {{#if showNameError}}
      {{#if (v-get user "firstname" "isInvalid")}}
        <div class="form-control-feedback container">
          <span>{{v-get user 'firstname' 'message'}}</span>
        </div>
      {{/if}}
    {{/if}}
  </div>

Using {{if showNameError 'has-danger' ''}}, I am able to set the class to has-danger when showNameError is true, however when it is false, the class remains there and the has-danger persists.

  1. When the page loads step 1

  2. When I force the error step 2

  3. After I fix the error enter image description here

As you can see, after I fix the error the has-danger class remains. My question is, can I make it so the class toggles based on whether the input is valid or not.

1

There are 1 best solutions below

2
On

Changed to and it is performing as it should.

<div class="form-group {{if showNameError (if (v-get user 'firstname' 'isInvalid') 'has-danger' 'has-success')}}">
<label for="name" class="cols-sm-2 control-label">Full Name</label>
<div class="cols-sm-10">
  <div class="input-group">
    <span class="input-group-addon">{{fa-icon "user"}}</span>
    {{input id="name" class="form-control" value=user.firstname placeholder="Enter your Name" focus-out=(action (mut showNameError) true)}}
  </div>
</div>

{{#if showNameError}}
  <div class="form-control-feedback container">
    <span>{{v-get user 'firstname' 'message'}}</span>
  </div>
{{/if}}