RequiredValidator when a list has 0 items

Hi all,

We have a single select DropDown. When the user selects an item, it is added to a list. How do we show the RequiredValidator when that list has 0 items? In order to inform the user to add items using the DropDown. The validator below is not working.

<RadzenTemplateForm TItem="Model"
                    Data="_model"
                    Submit="OnSubmit">

    <RadzenDropDown TValue="Resource"
                    Data="_resources"
                    Change="o => AddSelected((Resource)o)"
                    TextProperty="Id"
                    Name="DropDown"/>

    <RadzenRequiredValidator Component="DropDown"
                             Text="Select resources"
                             Visible="_model.SelectedResources.Count == 0"/>

    <RadzenButton ButtonType="ButtonType.Submit" Text="Submit"/>

</RadzenTemplateForm>

Thanks!
Ben

Hi @benghaner,

This isn't a supported scenario. RadzenRequiredValidator will trigger only if the dropdown doesn't have a value. I don't have a workaround for your case.

Thank you for checking @korchev !

I just had an idea. If you somehow can tie the Value of the DropDown with the model it could maybe work. Here is what I mean

<RadzenDropDown TValue="Resource"
                    Data="_resources"
                    @bind-Value="@_model.SelectedResource"
                    Change="o => AddSelected((Resource)o)"
                    TextProperty="Id"
                    Name="DropDown"/>

 <RadzenRequiredValidator Component="DropDown"
                             Text="Select resources"
                             DefaultValue="0"
                             Visible="_model.SelectedResources.Count == 0"/>
@code {
class Resource 
{
       public int SelectedResource
       {
              get { return SelectedResources.Count; }
              set { }
       }
}

The key is to use @bind-Value with a model property which will hook up the Blazor validation.

Thanks @korchev for the idea. We switched to a multi select dropdown which enabled the RequiredValidator to work normally, as well as improve the user experience. =)