InputPanel for UWP | ComponentOne
Features / Data Validation / Data Validation through Event
In This Topic
    Data Validation through Event
    In This Topic

    InputPanel provides another way of handling input validation through event. The control comes with the ValidateCurrentItem event that can be used for validating user input.

    The following image shows validation applied through event.

    To implement data validation through event, subscribe the ValidateCurrentItem event of InputPanel and add validation rules in the event handler in code. The following code shows how to apply validation using event. This example uses the sample created in Quick Start.

    Dim customer As Customer = TryCast(InPanel.CurrentItem, Customer)
    
    If customer IsNot Nothing Then
        Dim errorList = New ObservableCollection(Of ErrorInfo)()
    
        If customer.Name IsNot Nothing AndAlso String.IsNullOrWhiteSpace _
            (customer.Name.ToString()) Then
            errorList.Add(New ErrorInfo() With {
       .ErrorInputName = "Name",
        .ErrorContent = "This field cannot be empty."
    })
        End If
        If customer.Weight > 150 Then
            errorList.Add(New ErrorInfo() With {
        .ErrorInputName = "Weight",
        .ErrorContent = "Value out of range."
    })
        End If
        InPanel.ValidationErrors = errorList
        If errorList.Count > 0 Then
            e.Cancel = True
        End If
    End If
    
    Customer customer = InPanel.CurrentItem as Customer;
    
    if (customer != null)
    {
        var errorList = new ObservableCollection<ErrorInfo>();
    
        if (customer.Name != null && string.IsNullOrWhiteSpace(
            customer.Name.ToString()))
        {
            errorList.Add(new ErrorInfo { ErrorInputName = "Name",
                          ErrorContent = "This field cannot be empty." });
        }
        if (customer.Weight > 150)
        {
            errorList.Add(new ErrorInfo { ErrorInputName = "Weight",
                          ErrorContent = "Value out of range." });
        }
        InPanel.ValidationErrors = errorList;
        if (errorList.Count > 0)
        {
            e.Cancel = true;
        }
    }
    
    See Also