2016-03-25 44 views
1

我正在使用“ember-validation”插件添加表单验证。我能够做内联验证,但我需要根据验证状态禁用/启用按钮。使用ember验证状态禁用/启用表单提交按钮

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> 

截图

enter image description here

如何在使用烬验证时获得总表单输入验证状态,以便我可以将该标志用于禁用/启用按钮?

回答

2

看着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 
}); 

我想你可以设置一个计算的属性混叠yourModel.get("isValid")

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

,然后在模板:

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

我只是不知道你的bs-button fo的语法是什么r disabled=isSaveButtonDisabled

+0

感谢Pavol的回应。我们需要将验证从component.js移动到模型。否则,它会给错误验证不是一个函数。我修改了这样的代码,它工作。 –