I think we all have faced this check, do any field value is blank or not?
I did it like this(in lightning)
var commonObj = component.get('v.commonObj');
if( $A.util.isEmpty(commonObj.FirstName)
|| $A.util.isEmpty(commonObj.LastName)
|| $A.util.isEmpty(commonObj.FatherName)
|| $A.util.isEmpty(commonObj.Gender)
|| $A.util.isEmpty(commonObj.Mobile)
|| $A.util.isEmpty(commonObj.City)
|| $A.util.isEmpty(commonObj.State)){
alert('Please fill the missing values.');
}
I feel it's the poorest way of doing this as
- What if the number of fields is large(say 60-80)
But, before writing the above code I tried to use reduce method as
var commonObj = component.get('commonObj');
var validInput = commonObj.reduce(function (validSoFar, fieldVal) {
return validSoFar && $A.util.isEmpty(fieldVal);
}, true);
if(validInput){
}
But this code says
Uncaught Error in $A.getCallback() [commonObj.reduce is not a
function]
I'm looking for some efficient way of doing the same. Also, 1 more thing, why reduce method is not a function error is occurring? I tried using reduce method many times to do client-side validation of data entered as
var isAllValid = component.find('myform').reduce(function (validSoFar, inputCmp) {
inputCmp.showHelpMessageIfInvalid();
return validSoFar && inputCmp.get('v.validity').valid;
}, true);
if(isAllValid){}
But every time it gave me the same error
reduce is not a
function
Please guide me.
Best Answer
You are doing mistake with reduce method syntax. Yoiu need to find attribute and then need to validate value while currently it looks like you are fetching object and then doing validation.
and then in controller