Current filter:
                                You should refresh the page.
                                Support Center
                                0
                                  • Hello,
                                    I have ComboBox with style DropDown. User can select item from combobox list or can put own text. I would like to validate form after submit button.
                                    I can validate SelectedValue, but I dont know how can I validate text.
                                    So the main idea is: User must select one item from combobox or insert new text to combobox. How can I write validation throught model?
                                    Thanks.

                                    [C#]
                                    public class Model { public int? ProviderSourceID { get; set; } [Required(ErrorMessage = "Source required")] public string Source { get; set; } } ... @Html.DevExpress().ComboBoxFor(model => model.ProviderSourceID, combobox => { combobox.Name = "ProviderSourceID"; combobox.Properties.DropDownStyle = DropDownStyle.DropDown; combobox.Properties.TextField = "Source"; combobox.Properties.ValueField = "ProviderSourceID"; combobox.Properties.ValueType = typeof(int); combobox.ShowModelErrors = true; combobox.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithTooltip; combobox.CallbackRouteValues = new { Controller = "Home", Action = "SourcePartial" }; combobox.Properties.ClientSideEvents.BeginCallback = "function(s, e) { e.customArgs['Provider'] = cmbSource.GetValue(); }"; }).BindList(PrototypeSW.Models.MsProviderSource.GetProvidersSources(Model.ProviderID)).GetHtml() ... [HttpPost] public ActionResult LoadData(ModelSource source) { if (ModelState.IsValid) { } ...

                                You must  log in  or  register  to leave comments

                                1 Solution

                                0

                                Hello Marek,

                                I suggest you set the ComboBox.ValidationSettings.RequiredField.IsRequired property to True.
                                Alternatively, you can enable the model validation as described in this help topic.

                                See Also:
                                Built-in Validation

                                UPDATED BY Alessandro (DevExpress Support):

                                Set the ComboBoxProperties.DropDownStyle Property to the DropDownStyle.DropDownList value to avoid the situation when the end-user and enter a custom value.

                                UPDATED BY Alessandro (DevExpress Support):

                                You can handle the ASPxClientEdit.Validation Event in the following manner to check whether or not custom item was selected:

                                [C#]
                                combobox.Properties.ClientSideEvents.Validation = @"function(s, e) { if (s.FindItemByText(s.GetText()) == null) alert('You selected custom item!'); }";

                                Attached is a corresponding code example. I hope this information will help you.

                                UPDATED BY Alessandro (DevExpress Support):

                                Yes, ModelState is a part of a completely different server-side Model Validation mechanism.

                                See Also:
                                What is ModelState.IsValid valid for in ASP.NET MVC in NerdDinner?

                                Show all comments
                                • Marek Bober 01.17.2014

                                  Understand, but what if user not selected item from combobox and put own text - I think it will be problem.

                                • Set the ComboBoxProperties.DropDownStyle Property to the DropDownStyle.DropDownList value to avoid the situation when the end-user and enter a custom value.

                                • Marek Bober 01.19.2014

                                  I think there was a misunderstanding.
                                  I want the user to enter their own value as well as choose from a list. And I want to validate if user enter a value or select from list (one of them).

                                • You can handle the ASPxClientEdit.Validation Event in the following manner to check whether or not a custom item was selected:

                                  [C#]
                                  combobox.Properties.ClientSideEvents.Validation = @"function(s, e) { if (s.FindItemByText(s.GetText()) == null) alert('You selected custom item!'); }";

                                  Attached is a corresponding code example. I hope this information will help you.

                                • Marek Bober 01.20.2014

                                  Ok, by your example. I cannot use ModelState.IsValid where (selectedItem or custom item) return true otherwise false. Am I right?

                                • Yes, ModelState is a part of a completely different server-side Model Validation mechanism.

                                  See Also:
                                  What is ModelState.IsValid valid for in ASP.NET MVC in NerdDinner?

                                • Marek Bober 01.20.2014

                                  Thanks for you help, i will be calculate with this.

                                • You are welcome! We are always glad to help you in case of any problems.

                                You must  log in  or  register  to leave comments

                                If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383

                                FOLLOW US

                                DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, Silverlight, ASP.NET, WinForms, HTML5 or Windows 8, DevExpress tools help you build and deliver your best in the shortest time possible.

                                Copyright © 1998-2014 Developer Express Inc.
                                All trademarks or registered trademarks are property of their respective owners