如果我正确理解您的情况和代码,那么您只对验证组件状态中的某些变量感兴趣。
您可以通过向组件添加以下方法来实现所需的验证行为:
getCustomValidation() {
// Extract a list for validation on only the state fields that are
// relevance to the form validation
const fieldsToValidate = [
"ppvLimit",
"securityCode",
"primaryPhone",
"secondaryPhone",
"email",
"billingAddressLine1",
"billingAddressLine2",
"billingAddressCity",
"billingAddressTerritoryCode",
"billingAddressPostalCode",
"contactAddressLine1",
"contactAddressLine2",
"contactAddressCity",
"contactAddressTerritoryCode",
"contactAddressPostalCode",
"authorizedUser1",
"authorizedUser2",
"authorizedUser3",
"authorizedUser4"
];
// Iterate each field to check to determine if field is valid
for(const fieldToValidate of fieldsToValidate) {
// Extract value for current field
const value = this.state[ fieldToValidate ]
// If value is "truthy" then return valid (true)
if( !!value ) {
return true
}
}
// If none of the validation fields are valid, return false
return false;
}
要利用这一点,您可以在您的渲染方法中更新提交组件,如下所示:
<Button primary disabled={ !this.getCustomValidation() } type="submit">
{ loading ? <div><LoadingIcon size="lg" /> Saving...</div> : "Update" }
</Button>