Conditional Validation based on checkbox

direction please on how to make the dropdown and textbox both be required if the checkbox is checked

1 Like

You can set the Visible properties of the validator(s) to the same value as the On Hold checkbox.

1 Like

So I can't get the validation to work in the first place for the dropdown.

The below code only works if the field is not nullable in the database, since I need this dropdown to be nullable, I'm not sure why this validation isn't firing

<RadzenColumn SizeMD="9">
    <RadzenDropDown Data="@vehicleHoldReasonsForHoldReasonId" TextProperty="Value" ValueProperty="Id" AllowClear=true
                    Placeholder="Choose Hold Reason" style="display: block; width: 100%" @bind-Value="@vehicle.HoldReasonId" Name="HoldReasonId" />
    <RadzenCustomValidator Component="HoldReasonId" Validator="@(() => vehicle.HoldReasonId != 0)" Text="Hold reason is required" />
</RadzenColumn>

Got it, thank you.

Final answer below, for the dropdown custom validator, what you check for is dependent upon whether or not the db field is nullable. For nullable check for null, if not nullable, check for zero.

<RadzenRow style="margin-bottom: 1rem">
    <RadzenColumn SizeMD="3">
        <RadzenLabel Text="On Hold" Component="OnHold" style="width: 100%" />
    </RadzenColumn>
    <RadzenColumn SizeMD="9">
        <RadzenCheckBox @bind-Value="@vehicle.OnHold" Name="OnHold" />
    </RadzenColumn>
</RadzenRow>
<RadzenRow style="margin-bottom: 1rem">
    <RadzenColumn SizeMD="3">
        <RadzenLabel Text="Hold Reason" Component="VehicleHoldReason" style="width: 100%" />
    </RadzenColumn>
    <RadzenColumn SizeMD="9">
        <RadzenDropDown Data="@vehicleHoldReasonsForHoldReasonId" TextProperty="Value" ValueProperty="Id" AllowClear=true
                        Placeholder="Choose Hold Reason" style="display: block; width: 100%" @bind-Value="@vehicle.HoldReasonId" Name="HoldReasonId" />
        <RadzenCustomValidator Visible="@vehicle.OnHold" Component="HoldReasonId" Validator="@(() => vehicle.HoldReasonId != null)" Text="Hold reason is required" />
    </RadzenColumn>
</RadzenRow>
<RadzenRow style="margin-bottom: 1rem">
    <RadzenColumn SizeMD="3">
        <RadzenLabel Text="Hold Note" Component="HoldNote" style="width: 100%" />
    </RadzenColumn>
    <RadzenColumn SizeMD="9">
        <RadzenTextBox style="display: block; width: 100%" @bind-Value="@vehicle.HoldNote" Name="HoldNote" />
        <RadzenRequiredValidator Visible="@vehicle.OnHold" Component="HoldNote" Text="Hold note is required" />
    </RadzenColumn>
</RadzenRow>