Pass Validation context to child component?
I'm trying to figure out how it would be possible to notify a child component in the Data Form that validation is happening and to apply the validation styling to that control.
Take for example this custom component called TestComp.razor
I want this to be a separate component on not a field directly on the form because this control along with fetching the data is used in several different places in the application and it seems silly to have to duplicate the same code over again instead of making it its own component.
And there is a page which contains the Form and the child component:
The way it is working right now, the data annotation validation message is appearing below the custom component on submit but the combo box is not being highlighted in red like other controls. Is there a way to trigger this within the child component?
Hi Joe Sperlak,
Thank you for bringing this concern to our attention. After investigating the validation issue you encountered, it appears that the @bind-value in the child component was directly linked to the parent component. Consequently, validation was restricted to the model property value, and the error class wasn't associated with the component due to the absence of a ValueExpression.
To address this, we suggest configuring the parent component with essential parameters, including ValueExpression. Additionally, instead of relying solely on the OnValueSelect event to refresh the parent component's value, you can manage this within the value property's getter and setter methods.
Below is an example code demonstrating these adjustments:
[CustomTextBox.razor]
|
<SfComboBox TValue="int?" TItem="Model" Placeholder="Model" Value="@ModelId" ValueChanged="ModelIdChanged" DataSource="@_models" ValueExpression="@ModelIdExpression"> <ComboBoxFieldSettings Value="Id" Text="Name"></ComboBoxFieldSettings>
</SfComboBox>
@code { private List<Model> _models = new List<Model>(); private int? _value { get; set; } [Parameter] public int? ModelId { get => _value; set { if (!EqualityComparer<int?>.Default.Equals(value, _value)) { _value = value; ModelIdChanged.InvokeAsync(value); } } }
[Parameter] public EventCallback<int?> ModelIdChanged { get; set; }
[Parameter] public Expression<Func<int?>> ModelIdExpression { get; set; }
protected override void OnInitialized() { _models.Add(new Model { Id = 1, Name = "Model 1" }); _models.Add(new Model { Id = 2, Name = "Model 2" }); _models.Add(new Model { Id = 3, Name = "Model 3" }); }
private class Model { public int Id { get; set; } public string Name { get; set; } = string.Empty; } } |
With these adjustments, the validation should now function as expected, and the error styling should be correctly applied to the child component.
If you have any further questions or need additional assistance, please feel free to reach out.
Attachment: BlazorApp1_e604c9d3.zip
hello Kokila Poovendran
I had the same problem and your solution solved my problem.
But I need to access its valuechange from outside when I use my component.
do u have any solution?
Hi Joe Sperlak,
Thank you for the update and your continued patience. To access the ValueChanged event of your SfComboBox (inside the CustomTextbox.razor component) from outside the component, you can pass an event callback (EventCallback) from the parent component and invoke it when the value changes. Here's how you can implement this:
CustomTextBox.razor
<SfComboBox TValue="int?" TItem="Model" ValueExpression="ModelIdExpression" Placeholder="Model" Value="@ModelId"
ValueChanged="OnModelIdChanged" DataSource="@_models">
@code {
// External event callback for ValueChanged
// External event callback to notify outside component about value change
private async Task OnModelIdChanged(int? value)
// Trigger the external event when the value changes |
Index.razor
<SfDataForm Model="@_asset">
@code {
// Handler for external value changes |
Regards,
Priyanka K
Attachment: BlazorApp1_3f52bcec.zip
- 4 Replies
- 4 Participants
-
JS Joe Sperlak
- Feb 23, 2024 08:20 AM UTC
- Oct 8, 2024 01:54 PM UTC