Conditional Formatting in Forms based on Enum property

1 Answer 90 Views
DropDownList Form MultiSelect
Joe
Top achievements
Rank 1
Joe asked on 19 Jul 2023, 05:41 PM

Hello. I'm building a TelerikForm, and I need to show a TextField if 'Other' is selected.

The conditional rendering seems to work perfectly when the 'parent' item is a TelerikMultiSelect, but does not work with the auto-generated Enum field. 

I have verified that the field in question is indeed being set to CompanyType.Other, yet the "Other Legal Entity Type" field is not showing.

What am I missing?

 <FormGroup LabelText="Legal Information" Columns="2" ColumnSpacing="15px">
            <FormItem Field="@nameof(formInfo.LegalEntity)"></FormItem>
            @if (formInfo.LegalEntity == CompanyType.Other) // this does not work
            {
                <FormItem LabelText="Other Legal Entity Type" Field="@nameof(formInfo.OtherLegalEntityType)"></FormItem>
            }
        </FormGroup>
        <FormGroup LabelText="Operations" Columns="2" ColumnSpacing="15px">
            <FormItem Field="@nameof(formInfo.ProductDelivery)">
                <Template>
                    <TelerikMultiSelect Data="formInfo.productDeliveryOptions" @bind-Value="formInfo.ProductDelivery" />
                </Template>
            </FormItem>
            @if (formInfo.ProductDelivery.Contains("Other")) // this works as expected
            {
                <FormItem LabelText="Other Product Delivery Type" Field="@nameof(formInfo.OtherProductDeliveryType)"></FormItem>
            }
        </FormGroup>

1 Answer, 1 is accepted

Sort by
0
Hristian Stefanov
Telerik team
answered on 20 Jul 2023, 05:28 PM

Hi Joe,

I'm pasting here the answer I gave you in the private ticket so the community can benefit from it.

=======

The described behavior arises when changes occur in an autogenerated enum field, as the Form component doesn't automatically re-render itself due to performance optimizations in its implementation. Therefore, in such a scenario, you need to manually re-render/refresh the Form using the StateHasChanged method inside the OnUpdate event.

I've created an example for you in this REPL link, demonstrating how to apply this approach. Please go ahead and run it to observe the result.

If you encounter any difficulties during the testing phase, feel free to let me know.

=======

Regards,
Hristian Stefanov
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Telerik family, check out our getting started resources!
Tags
DropDownList Form MultiSelect
Asked by
Joe
Top achievements
Rank 1
Answers by
Hristian Stefanov
Telerik team
Share this question
or