Question

I'm validating TextFields with a foreach loop with an if/else statement inside. This all happens inside a public virtual Boolean Method(). Only the first element is validated and I don't know why the rest is not validated.

How do I have to change my method so it will validate all the items in TextFieldList?

This is my method:

public virtual Boolean ValidateTextFields(){

    foreach (UITextField item in TextFieldList) {
        if (item.Text == "") {
            item.AttributedPlaceholder = new NSAttributedString (item.Placeholder, foregroundColor: UIColor.Red);
            return false;
        } else {
            return true;
        }
    }
    return true;
}

EDIT:

I got it working but now I have another issue, I have multiple methods like ValidateTextFields and I check them like this:

if (ValidateTextFields() && ValidateEmail() ) {
    Console.WriteLine ("CONTINUE TO NEXT SCREEN");
} else {
    Console.WriteLine ("ERRORRRRRR");
}

Now if ValidateTextFields() is false then ValidateEmail() is never called so the EmailTextFields won't be validated. Only after ValidateTextFields() is true I can validate ValidateEmail(). Is there a way to call both methods at the same time and check if they are true?

Was it helpful?

Solution

Try this: If have any invalid field the Method returns false, else if all fields is valid returns true.

  public virtual Boolean ValidateTextFields(){

        foreach (UITextField item in TextFieldList) {
            if (item.Text == "") {
                item.AttributedPlaceholder = new NSAttributedString (item.Placeholder, foregroundColor: UIColor.Red);
                return false;
            } 
        }
        return true;
    }

Or you can valid all itens to put a attributedplaceholder for each item and returns if has any invalid, like this:

  public virtual Boolean ValidateTextFields(){
        bool hasInvalidFields = false;
        foreach (UITextField item in TextFieldList) {
            if (item.Text == "") {
                item.AttributedPlaceholder = new NSAttributedString (item.Placeholder, foregroundColor: UIColor.Red);
                hasInvalidFields = true;
            } 
        }
        return !hasInvalidFields;
    }

For your edit, to call all validates you can:

bool validTextFields = ValidateTextFields();
bool validEmails = ValidateEmail();

if ( validTextFields &&  validEmails) {
    Console.WriteLine ("CONTINUE TO NEXT SCREEN");
} else {
    Console.WriteLine ("ERRORRRRRR");
}

OTHER TIPS

Your code will always return after the first element has been processed, whether it's valid or not.

What you probably want instead is to validate all elements and return afterwards:

var result = true;
foreach (UITextField item in TextFieldList) {
    if (item.Text == "") {
        item.AttributedPlaceholder = 
         new NSAttributedString (item.Placeholder, foregroundColor: UIColor.Red);
        result = false;
    } 
}

return result;

You simply need to remember what to return in the end instead of returning immediately.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top