J
J
Jaguar_sea2014-10-02 11:49:12
C++ / C#
Jaguar_sea, 2014-10-02 11:49:12

How to validate textboxes on button click?

There are several TextBoxes in the dialog box in which the update of the data source is deferred until the user clicks the OK button. Please tell me how to properly validate i.e. check if there are empty values ​​in TextBoxes, for example.
I'll try to rephrase my question. I am trying to get to grips with Entity Framework Code First. I have a Bank class with properties that are assigned various attributes, such as [Required(ErrorMessage = "required")]. The data is bound to the DataGrid of the main window, so it is necessary to perform CRUD operations. When the add event is called, a new bank is created, the data in which should get from the dialog box, more precisely from its TextBoxes. The source in TextBoxes is not updated when data is entered, but after the user finishes entering the required values ​​and clicks the "OK" button in the dialog box, otherwise when editing the selected item in the DataGrid, the data (collection) would change not only in TextBoxes, but also in the collection itself as a whole, i.e. and in the main window, even before the user clicks the "OK" button.

<TextBox Text="{Binding Path=ItemName, UpdateSourceTrigger=Explicit}" />

So the source is updated when the "OK" button is clicked
BindingExpression be = itemNameTextBox.GetBindingExpression(TextBox.TextProperty);
be.UpdateSource();

Now the question arises: How to prevent properties with empty values ​​from getting into the collection? As far as I understand it is necessary to use ValidationRules. Unfortunately, google could not find a suitable example of such an implementation for me, which is why I wrote here, with the hope that more literate people will tell you how to do it.
Maybe I'm not doing everything right at all, I'm just trying to understand this technology. If you suggest a simpler implementation, I will be very grateful.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aush, 2014-10-02
@aush

There is no abstractly correct or incorrect validation. It all depends on your business objective.
You can check for an empty value like this:
UPD:
In mvvm you have a ViewModel that is bound to View: controls for display are bound to VM properties, controls for management are bound to VM commands. Accordingly, when the user clicks on the button, from your command handler you have access to the properties of the model, which means you can validate them.
UPD2:
Ok, then this: firstly, EF in the context of your task does not matter. Things like [Required] are not EF-specific, but EF uses them and doesn't let you work with data that doesn't pass validation. Of course, you can try to upload data through EF, get a validation exception and handle it, but this is a dirty move.
So, you've created a new Bank , set it as the model for the textbox dialog box, and bound the Bank's properties to it. Then, when you clicked on OK, you updated the bindings (in my opinion, this is superfluous, it’s easier in this case to make a regular binding) and the data from the textboxes got into the model. Now you should actually validate the model:

bool ValidateModel()
{
    var validationContext = new ValidationContext(Bank, null, null);
    var validationResults = new List<ValidationResult>();
    Validator.TryValidateObject(Bank, validationContext, validationResults);

    if (validationResults.Any())
    {
        foreach (var validationResult in validationResults)
        {
            ShowError(validationResult.MemberNames.First(), validationResult.ErrorMessage);
        }

        return false;
    }
    else
    {
        return true;
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question