Search code examples
ember.jsember-dataember-validations

Disable/Enable form submit button with ember validations status


I am adding form validations using "ember-validations" plugin. I am able to do inline validations, but I need to disable/enable button based on the validation status.

Component.js

export default Ember.Component.extend(EmberValidations, {
validations: {
    'model.firstName': {
      presence: true,
      presence: { message: 'Please enter valid first name.' }      
    },
    'model.lastName': {
      presence: true,
      presence: { message: 'Please enter valid last name.' }      
    },
    'model.email': {
      presence: true ,
      presence: { message: 'Please enter valid email name.' }     
    },
    'model.department': {
      presence: true,
      presence: { message: 'Please enter valid department name.' }      
    },
}
});

Template.hbs

<form class="form-horizontal" role="form">
<div class="form-group">
<label class="control-label col-sm-3" for="fname">First Name:</label>
<div class="col-sm-8">
    {{validated-input type="text" placeholder="First Name" value=model.firstName errors=errors.model.firstName}}
 </div>
 </div>
<div class="form-group">
<label class="control-label col-sm-3" for="lname">Last Name:</label>
<div class="col-sm-8">
  {{validated-input type="text" placeholder="Last Name" value=model.lastName errors=errors.model.lastName}}
  </div>
 </div>
<div class="form-group">
<label class="control-label col-sm-3" for="email">Email:</label>
<div class="col-sm-8">
   {{validated-input type="email" placeholder="Email" value=model.email errors=errors.model.email}}
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="department">Department:</label>
<div class="col-sm-8">
    {{validated-input type="text" placeholder="Department" value=model.department errors=errors.model.department}}
</div>
</div>
{{#bs-button classNames="table-btn" action=(action "saveAction" model) buttonType="button" type="update"}}Save{{/bs-button}}
</form>

Screenshot

enter image description here

How can I get the total form inputs validation status while using ember validations, so that i can use that flag for disable/enable button?


Solution

  • Looking at ember-validations docs:

    user.validate().then(function() {
      // all validations pass
      user.get('isValid'); // true
    }).catch(function() {
      // any validations fail
      user.get('isValid'); // false
    }).finally(function() {
      // all validations complete
      // regardless of isValid state
     user.get('isValid'); // true || false
    });
    

    I would guess you could set a computed property aliasing yourModel.get("isValid"):

    // in your controller or component:
    isSaveButtonDisabled: Ember.computed.not("user.isValid")
    

    And then in your template:

    ...
    </div>
    {{#bs-button classNames="table-btn" disabled=isSaveButtonDisabled action=(action "saveAction" model) buttonType="button" type="update"}}Save{{/bs-button}}
    </form>
    

    I am just not sure what is the syntax for your bs-button for disabled=isSaveButtonDisabled.