This is a migrated thread and some comments may be shown as answers.

Required Validation with AllowCustomText

4 Answers 53 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Phil H.
Top achievements
Rank 2
Phil H. asked on 27 Oct 2014, 02:53 PM
Hi:

I have to to be persistent, but I can get the validation to be ignored. First the code (view):
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!DOCTYPE html>
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager>
        <div>
            <telerik:RadComboBox ID="haulerDropDown" runat="server"
                DataSourceID = "haulerDataSource" DataTextField="Text"  DataValueField="Value"
                AllowCustomText="true" Width="200" >
            </telerik:RadComboBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
                    ControlToValidate="haulerDropDown" ValidationGroup="HeaderValidationGroup"
                    ErrorMessage="'Hauler' is required" Text="*" ForeColor="Red"
                    />
        </div>
        <asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="HeaderValidationGroup" />
        <asp:ObjectDataSource ID="haulerDataSource" runat="server"
            SelectMethod="Hauler" TypeName="App.DropDowns">
        </asp:ObjectDataSource>
    </form>
    </body>
</html>
Code behind:
Option Strict On
Option Explicit On
Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim _value As String = haulerDropDown.SelectedValue
        Dim _i As Integer = 1
    End Sub
End Class
and data for the data source (in App_Code):
Option Strict On
Option Explicit On
Imports Microsoft.VisualBasic
Namespace App
    Public Class DropDown
        Public Property Value() As String
        Public Property Text() As String
        Public Property Selected() As Boolean
        Public Sub New()
            Me.Value = ""
            Me.Text = ""
            Me.Selected = False
        End Sub
        Public Sub New(ByVal value As String, ByVal text As String)
            Me.Value = value
            Me.Text = text
            Me.Selected = False
        End Sub
        Public Sub New(ByVal value As String, ByVal text As String, ByVal selected As Boolean)
            Me.Value = value
            Me.Text = text
            Me.Selected = selected
        End Sub
    End Class
    '
    Public Class DropDowns
        Public Function Hauler() As List(Of DropDown)
            Dim _dropDown As List(Of DropDown) = _
                New List(Of DropDown) From { _
                    New DropDown With {.Value = "9 Point", .Text = "9 Point"}, _
                    New DropDown With {.Value = "R. A.", .Text = "R. A."}, _
                    New DropDown With {.Value = "A. B.", .Text = "A. B."}, _
                    New DropDown With {.Value = "B. C.", .Text = "B. C."}, _
                    New DropDown With {.Value = "Beck", .Text = "Beck"}}
            Return _dropDown
        End Function
    End Class
End Namespace
Now place a break point in the code behind on the _i int.  Now launch the page.  Click the button and all works as expected (does not submit and the * indicates a validation error).  Now delete the text, and tab out and delete the text again and tab out (the combo-box should be empty now).  Now click the button and the validation control does NOT stop the submit, and if you check the SelectedValue the combo-box is empty.

Phil

4 Answers, 1 is accepted

Sort by
0
Hristo Valyavicharski
Telerik team
answered on 30 Oct 2014, 02:22 PM
Hi Phil,

What is the version of the .NET Framework you have? If you are using .NET 45 please look here:
http://www.telerik.com/forums/asp-net-4-5-unobtrusive-validation-and-radcontrols-for-asp-net-ajax

Please note that the standard ASP.NET RequiredFieldValidator is good for simple scenarios where it needs to validate more simple controls like textboxes. That's why if your logic is a bit complicated you can use CustomValidators.

Regards,
Hristo Valyavicharski
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Phil H.
Top achievements
Rank 2
answered on 30 Oct 2014, 03:30 PM
Hi Hristo:

I am still a 4.0 framework.  I have already run into the 4.5 issue.  
I have also tried a range validator.
So, the end result is if the validator fails to work properly, then it gets to the db and fails on the update (which is not the place I want to get the validation).  I just was running through some testing scenarios, and got the problem again.
I know that the ComboBox and the DatePicker has multiple input fields, and it seem to be checking the wrong field.

Phil
0
Accepted
Hristo Valyavicharski
Telerik team
answered on 31 Oct 2014, 02:36 PM
I made a few tests with both Custom and RequiredField validators. Combo is working with both. Please check these videos:

RequiredFieldValidator - http://screencast.com/t/iDD0BiydH3Z
CustomFieldValidator - http://screencast.com/t/8G5VTZdxEeg

Testing sample is attached.

Regards,
Hristo Valyavicharski
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Phil H.
Top achievements
Rank 2
answered on 03 Nov 2014, 03:23 PM
Hi Hristo:

Thanks for all of your help.  The empty value was getting to the code behind, via an IE delete feature in the combo-box text input field (x).  I would prefer to use a validation solution so I am going with the following client side custom validation:
function customRequiredValidator(source, arguments) {
    var control2Validate = source.controltovalidate;
    var combo;
    arguments.IsValid = true;
    if (arguments.Value == "")
        arguments.IsValid = false;
    else {
        combo = $find(control2Validate);
        if (combo.get_value() == "") {
            arguments.IsValid = false;
        }
    }
}

The above, I think is generic enough to support multiple combo-boxes on the page.  It is also working in a form-view.

Phil
Tags
ComboBox
Asked by
Phil H.
Top achievements
Rank 2
Answers by
Hristo Valyavicharski
Telerik team
Phil H.
Top achievements
Rank 2
Share this question
or