I have the following code in my project:
//code used in my NEXT button
let errorMessage:String = validateAllFields()
if ( errorMessage != "" ) {
AlertActions.showBasicAlert(erroParaExibir: errorMessage, currentView: self)
return
}
...more code to be executed
//code to validate if all fields are empty
func validateAllFields() -> String {
var errorMessage = ""
if( nomeAnimalTextField.text == ""){
errorMessage = "Preencha o nome do animal"
} else if( microchipAnimalTextField.text == ""){
errorMessage = "Preencha o microchip do animal"
} else if( microchipAnimalTextField.text!.trim().count < 15){
errorMessage = "Microchip do animal deve possuir 15 posições númericas"
} else if( mesNascimentoTextField.text == ""){
errorMessage = "Preencha o mês do nascimento"
} else if( anoNascimentoTextField.text == ""){
errorMessage = "Preencha o ano de nascimento"
}
return errorMessage
}
The code below is my guard clause
if ( errorMessage != "" ) {
AlertActions.showBasicAlert(erroParaExibir: errorMessage, currentView: self)
return
}
The return is protecting the rest of the code to not be executed if the condition fails.
I want to know how can I use only the validateAllFields() and insert the if condition with the "return" part of the code inside this function, is that possible?