views:

82

answers:

1

Here's the easy pseudo-code:

void TextBox1Changed()
{
    //If the text isn't a number, color it red

    if (!IsValidNumber(TextBox1.Text)
        TextBox1.Color = Pink;
    else
        TextBox1.Color = WindowColor;
}

What's the MVC enterprisey version?

A: 

Not trying to be language specific, but the idea is to create a number text control that knows if the value is valid. It's easy to get hung up on the exact roles of M, V, and C. However, for all practical purposes, it makes sense to combine the View and the Controller for Desktop like applications. Swing took that approach because the controller and view had very tight coupling and it made sense to combine them into one. Read up this nice discussion on c2 about the topic.

class NumberTextBox extends TextBox {
    bool isValid() {
        return IsValidNumber(this.Value);
    }
}


ageTextBox = new NumberTextBox();
ageTextBox.addChangeHandler(function() {
    this.Color = this.isValid ? WindowColor : Pink;
});
Anurag