We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

DropdownList validation

Hi

im using dropdownlist with DataAnnotation RequiredAttribute
but when model is not valid the dropdown border color not change to red 
Is there a special way to validation dropdown ?

 <SfDropDownList FilterType="Syncfusion.Blazor.DropDowns.FilterType.Contains"
                                            @bind-Value="@ModelObject.UnitId"
                                            FloatLabelType="FloatLabelType.Auto"
                                            TItem="SelectWorkgroupUnitViewModel"
                                            FilterBarPlaceholder="search"
                                            Placeholder="unit"
                                            TValue="int?"
                                            AllowFiltering="true"
                                            ShowClearButton="true"
                                            DataSource="@Units"
                                           
                                         >
                                      <Syncfusion.Blazor.DropDowns.DropDownListFieldSettings Text="UnitName" Value="Id"></Syncfusion.Blazor.DropDowns.DropDownListFieldSettings>

                            </SfDropDownList>

my model

public class Model
{
 
        [Required(ErrorMessage = "please select unit")]
        public  int? UnitId { get; set; }
}



7 Replies

JM Jeyanth Muthu Pratheeban Sankara Subramanian Syncfusion Team September 8, 2020 05:13 PM UTC

Hi ALI,

Greetings from Syncfusion support.

We have checked your requirement. We would like to inform you that you can manually add the border for dropdownlist when the control has value by validating in the change event.


 

<EditForm Model="@model"> 
  <SfDropDownList TValue="string" TItem="Countries" CssClass="@Validate" Width="200px" ShowClearButton="true" Placeholder="e.g. Australia" DataSource="@Country"> 
      <DropDownListFieldSettings Text="Name" Value="Code"></DropDownListFieldSettings> 
      <DropDownListEvents TValue="string" ValueChange="OnChange"></DropDownListEvents> 
  </SfDropDownList> 
</EditForm> 


public void OnChange(ChangeEventArgs<stringargs) 
    { 
        if (args.Value == null) 
        { 
            this.Validate = "e-error"; 
        } 
        else 
        { 
            this.Validate = "e-success"; 
        } 
    } 


We have prepared sample for your convenience. Please find the sample in the below link.

Sample Link       : https://www.syncfusion.com/downloads/support/forum/157635/ze/Dropdownlist_Edit_Form_Validation991956043 

Screenshot:

Without value

 
 

With value

 
 

Please let us know if you need any further assistance on this. 

Regards, 
Jeyanth.

 



AL ALI September 9, 2020 10:46 AM UTC

Hi Jeyanth Muthu Pratheeban Sankara Subramanian
thank you
this worked perfect 

The text box control automatically activates validation by setting the binding, but in Drop Down it must be done manually.
I think this is because Drop Down has a value of zero by default
Is my opinion correct?
Is there a way for automatic validation for Darp Down?


JM Jeyanth Muthu Pratheeban Sankara Subramanian Syncfusion Team September 10, 2020 03:20 PM UTC

Hi ALI, 

Thanks for you update. 

We would like to know you that we have already considered as an improvement in our end and these changes will be included in the Volume 3 release which is expected to be rolled out on 30th September 2020.

We appreciate your patience until then. 

Regards, 
Jeyanth. 



AB Andrea Bioli November 3, 2020 01:07 PM UTC

It doesn't work yet, on my side, out of the box: if you implemented this behaviour, could you please offer an example with the new functionality?

Thanks.
Andrea


BC Berly Christopher Syncfusion Team November 4, 2020 01:13 PM UTC

Hi Andrea, 
  
We have prepared the sample based on the implemented behaviour when we used the DropDownList component inside the EditForm without done any manual interaction for validation and attached it below. 
  
  
Regards, 
Berly B.C 



PS psyk July 23, 2021 06:20 PM UTC

Hello, 

can you please give me a sample where the TValue is an Int ? and dropVal is type int ?  It does not seems to work with type int



BC Berly Christopher Syncfusion Team July 26, 2021 09:15 AM UTC

Hi Psyk, 
  
We have checked the DropDownList component TValue with int? and validation is working fine at our end. So, we have attached the sample and screenshot below. 
  
 
<EditForm Model="@model"> 
    <DataAnnotationsValidator /> 
    <SfDropDownList TValue="int?" TItem="Countries" Width="200px" ShowClearButton="true" @bind-Value="@model.dropVal" Placeholder="e.g. Australia" DataSource="@Country"> 
        <DropDownListFieldSettings Text="Name" Value="Code"></DropDownListFieldSettings> 
    </SfDropDownList> 
    <ValidationMessage For="() => model.dropVal" /> 
    <SfButton CssClass="e-small e-info" Type="submit" Content="Submit"></SfButton> 
</EditForm> 
 
 
@code{ 
    public class Countries 
    { 
        public string Name { get; set; } 
 
        public int? Code { get; set; } 
 
        [Required] 
        public int? dropVal { get; set; } = 2; 
    } 
 
    List<Countries> Country = new List<Countries> 
{ 
        new Countries() { Name = "Australia", Code = 1 }, 
        new Countries() { Name = "Bermuda", Code = 2 }, 
        new Countries() { Name = "Canada", Code = 3 } 
 
    }; 
    public Countries model = new Countries(); 
 
} 
 
 
Required validation  
Success validation 
 
 
 
  
  
Regards, 
Berly B.C 


Loader.
Up arrow icon