Hello,
I have created a custom Textbox component using the example at: https://blazor.syncfusion.com/documentation/textbox/how-to/create-custom-component-with-validation
I have been trying to do the same but using a DropDownList, but I'm facing issues with persistence (data is not being saved) and Validation (field is required but if left empty, this fact is ignored).
I have searched for an example similar to the one using TextBox, but haven't been able to find any. Is there a suitable example available? If not, could you please advise?
Thanks in advance!
As per your requirement, we have prepared a sample of the Custom Component with Tooltip Validation Using the Blazor DropDownList component and shared it below for your reference.
The DropdownList component is defined in the razor page along with the required properties and event binding. The following code snippets are added in a razor file with the name as CustomComponent.
[ CustomComponent. razor]
@using System.Linq.Expressions; @using Syncfusion.Blazor.DropDowns
<SfDropDownList ID="@ID" TValue="string" TItem="Countries" Value="@Value" DataSource="@Country" Width="300px" ValueChanged="ValueChanged" ValueExpression="@ValueExpression" Placeholder="@Placeholder" Readonly="@Readonly" @ref="Box"> <DropDownListFieldSettings Value="Name">DropDownListFieldSettings> <ValidationMessage For="@ValidationMessage"> ValidationMessage> SfDropDownList> @code {
private string _value; [Parameter] public string Value { get => _value; set { if (!EqualityComparer<string>.Default.Equals(value, _value)) { _value = value; ValueChanged.InvokeAsync(value); } } }
[Parameter] public Expression<Func<string>> ValueExpression { get; set; }
[Parameter] public EventCallback<string> ValueChanged { get; set; }
private SfDropDownList<string, Countries> Box { get; set; }
private FieldIdentifier Field { get; set; }
[CascadingParameter] public EditContext EditContext { get; set; } = default!;
[Parameter] public string ID { get; set; }
[Parameter] public string Placeholder { get; set; }
[Parameter] public bool Readonly { get; set; }
[Parameter] public Expression<Func<string>> ValidationMessage { get; set; }
protected override void OnInitialized() { Field = EditContext.Field("Text"); EditContext.OnValidationStateChanged += HandleValidationStateChanged; }
private void HandleValidationStateChanged(object sender, ValidationStateChangedEventArgs e) { StateHasChanged(); bool _invalid = EditContext.GetValidationMessages(Field).Any(); Box?.UpdateParentClass("", ""); StateHasChanged(); } public class Countries { public string Name { get; set; }
public string Code { get; set; } }
List<Countries> Country = new List<Countries> { new Countries() { Name = "Australia", Code = "AU" }, new Countries() { Name = "Bermuda", Code = "BM" }, new Countries() { Name = "Canada", Code = "CA" }, new Countries() { Name = "Cameroon", Code = "CM" }, }; } |
The EditForm with the bound model is declared on the main razor page. Inside the EditForm, the DataAnnotationsValidator and CustomComponent with wrapped SfTooltip component are added. The DataAnnotationsValidator is added to enable form validation based on validation attributes declared in the model. The CustomComponent component is then bound with the “Text” property from the model and the “Text” property contains DataAnnotations attributes used for validation. To learn more about SfTooltip component refer here
[ Index.razor ]
@page "/"
@using System.ComponentModel.DataAnnotations; @using Syncfusion.Blazor.Popups @using Syncfusion.Blazor.Buttons
<div id="ControlRegion"> <div class="col-lg-12 control-section"> <div class="control_wrapper"> <EditForm Model="@_Records" OnValidSubmit="HandleSubmit"> <DataAnnotationsValidator /> <SfTooltip CloseDelay="0" OnOpen="ToolTipOpen" OpenDelay="0" OpensOn="Hover" Position="Position.TopCenter" Target="#text" WindowCollision="true"> <CustomComponent @bind-Value="@(_Records.Text)" ID="text" Placeholder="Select a Value" Readonly="false" ValidationMessage="@(() => _Records.Text)" @bind-Value:event="ValueChanged">CustomComponent> <TooltipTemplates> <Content> <ValidationMessage For="@(() => _Records.Text)">ValidationMessage> Content> TooltipTemplates> SfTooltip> <SfButton>SubmitSfButton> EditForm> div> div> div> @code { private ModalData _Records = new();
protected override void OnInitialized() { base.OnInitialized(); }
private static void ToolTipOpen(TooltipEventArgs args) { args.Cancel = !args.HasText; }
private void HandleSubmit() {
}
public class ModalData { [Required(ErrorMessage = "This field is required.")] public string Text { get; set; } }
} |
NOTE : It is necessary to bind Value, ValueExpression, and native ValueChanged event for accessing the value changes and triggering validation. To retrieve the validation message from the custom component, it is required to add the ValidationMessage tag in the DropdownList component.
[ Output ]
|
Thank you for the quick turnaround! We're still having issues with the initial value shown on the DDList field when editing an existing record.
Our code is almost exactly what you supplied, with the exception that, in our case, we want to show the "Names"on the DropDowList, but save the "Code" property of the Countries object. To do so, we have:
<DropDownListFieldSettings Text="Name" Value="Code"></DropDownListFieldSettings>
Our issues are
When Editing an existing record, although the field has a valid value, an error message is displayed. To fix it, the user must select a value that is different from the existent one or clear the field and reselect (please see: Edit Services - Google Chrome 2023-01-09 16-17-53.mp4)
When Adding a record, it is impossible to select a value (plese see: Edit Services - Google Chrome 2023-01-09 16-24-15.mp4)
Please notice that the field labeled "Unit" is the custom component. The other DD Lists (Frequency, Revenue Class and Status) are normal SfDropDownLists.
I hope this makes sense.
Thanks in advance!
Upon further testing, we noticed that if the value of the DD is set before anything else, the user is able to work normally. Please see: Edit Services - Google Chrome 2023-01-09 16-42-07.mp4
The "Service Code" and "Service Description" Textboxes are a custom component based on the
create-custom-component-with-validation with Textbox
Furio, We request that you provide us with some additional details about your query. Specifically, we would like to know if you are experiencing the issue in any particular scenario and if the component is being rendered within any other component or alone. Please also provide a runnable sample and steps for reproducing the issue. This will help us validate your query and provide you with a better solution.
I'll put together a sample solution with the relevant code.
Thanks!
We will wait to hear from you.
Hi,
I have attached a runnable sample. Here are the steps:
Case 1
- Run Application
- Click on "Go to Services"
- Click Add
- Enter V999 as service Code and any string as Service Description
- Using the mouse, try to choose a value for Unit. You won't be able to choose any value.
- Click on the Unit field, then using the keyboard (up/dn arrows) pick a value and click tab. The outline of the dropdownlist box will be green, but the value will not be displayed
- Click on the Unit field, then using the keyboard (up/dn arrows) pick a value and click Enter. The value of choice will be displayed correctly
Case 2
- Run Application
- Click on "Go to Services"
- Click Add
- Enter V999 as service Code and any string as Service Description
- Click on the Unit field, then using the keyboard (up/dn arrows) pick a value and click Enter. The value of choice will be displayed correctly
- Using the mouse, try to choose a value for Frequency. You won't be able to choose any value, plus, the value you entered for Unit will be gone.
- Click on Service Description and then press Enter: the values of the Unit and Frequency fields will be displayed...
Case 3
- Run Application
- Click on "Go to Services"
- Double click on the first row (to edit)
- Using the mouse, try to choose a different value for Unit. The value of choice will not be displayed. The pre-existing value will be displayed instead, no matter how many times you try
- Click on the Unit field, then using the keyboard (up/dn arrows) pick a value and click tab. The value of choice will not be displayed. The pre-existing value will be displayed instead, no matter how many times you try
- Click on the Unit field, then using the keyboard (up/dn arrows) pick a value and click Enter. The value of choice will be displayed correctly
- This same behaviour is observed with Frquency field
Thanks in advance
Furio Filoseta
Hi Furio,
We have attempted to run the shared sample on our end, but unfortunately, we were unable to do so. In order to assist you better, we would request that you please share a runnable sample that replicates the issue, along with the following details:
We will do our best to help you resolve the issue as soon as possible.
Regards,
Udhaya Kumar D
Hi,
I tried to make the sample as simple as possible, what issues are you facing with it?
In response to your questions:
- The problem with the DropDownList component takes place on a Grid's custom add/edit dialog
- We provided video in past posts:
Edit_Services__Google_Chrome_20230109_164207_b3660db1.zip and
Captures_1eada35.zip from 1/9
The more we test this and try to make it work, the more it looks like something is causing the SfDropDownList component fails no to update to display value of the underlying
Thanks!
Furio Filoseta
You can refer to the below shared Syncfusion forum for your requirement.
https://www.syncfusion.com/forums/169085/validation-of-child-component?reply=SoHuMh