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

Validation when value is empty

3 Answers 244 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Mattias
Top achievements
Rank 1
Mattias asked on 31 Aug 2011, 11:17 AM
Hi,
I have this combobox:
@{Html.Telerik()
           .ComboBox()
           .Name("CustomerID")
           .Encode(false)
           .BindTo(new SelectList((IList<SelectListItem>)ViewData["SelectListCustomers"], "Value", "Text"))
           .AutoFill(false)
           .HtmlAttributes(new { @class = "fake-autocomplete", style = "width: 300px" })
           .HighlightFirstMatch(true)
           .ClientEvents(events => events.OnChange("CustomerChange"))
           .Filterable(filter => filter.FilterMode(AutoCompleteFilterMode.Contains))
           .Value(Model.CustomerID.ToString())
           .Render();
       }

The problem occurs when I enter my own text in the control, then I get: The field CustomerID must be a number.
CustomerIDs type is int. Shouldn't the value automatic be 0 then?
You have exactly the same behavior on your demo page:
http://demos.telerik.com/aspnet-mvc/combobox/clientvalidation

 I haven't set any validation attribute on my viewmodel so I don't understand why it page stops on it.

How should I solve this?
Can validation be turned off when entering a custom text?

Regards,
Mattias


3 Answers, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 01 Sep 2011, 06:21 AM
Hello Mattias,

 
The Combobox UI component allows to enter custom value, which in this case is string. The aforementioned behavior is a result of the dynamic nature of the JavaScript. Hence one variable can be set to Number and then can be set to String. That is why the validation turns on in this case. Actually this is the expected behavior. If you need to restrict custom values I will suggest you use the DropDownList UI component, which does not allow to enter custom values. If you need to restrict the type of the custom values you will need to wire change event of the combobox and use this event handler:

function combo_change(e) {
   var value = e.value;
   var combobox = $(this).data("tComboBox");
 
   if (isNaN(value)) {
     combobox.value(combobox.previousValue || "");
   }
}

Regards,
Georgi Krustev
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Mattias
Top achievements
Rank 1
answered on 01 Sep 2011, 07:28 AM
Hi,
I don't really need to allow custom text beyond what is binded to the control, so it is ok that validation kicks in if any value can't be found for the custom text.
But what I do need is the combobox to be left blank, the control is not a required field on my page.

Isn't there any way to disable validation or at least tweak it to allow empty value?

/Mattias
0
Georgi Krustev
Telerik team
answered on 01 Sep 2011, 07:57 AM
Hi Mattias,

The combobox does not provide client validation. The validation message which you mentioned comes from a validation framework. Could you please send us a simple test project, which reproduce this behavior? Thus I will be able to review it locally and advice you further.

Kind regards,
Georgi Krustev
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
ComboBox
Asked by
Mattias
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Mattias
Top achievements
Rank 1
Share this question
or