Skip to content

Commit 612eb3e

Browse files
feat(kb): validation message in tooltip new example
1 parent 4a38b4a commit 612eb3e

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

knowledge-base/common-validation-error-in-tooltip.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ Can you please advise me on how to display validation message as tooltip?
1818

1919
## Solution
2020

21+
This article contains two different approaches for implementing validation notifications with popups:
22+
23+
* [Validation Summary in a Popup](#validation-summary-in-a-popup)
24+
25+
* [Per-Input Validation Popups](#per-input-validation-popups)
26+
27+
### Validation Summary in a Popup
28+
2129
There are several key aspects in implementing this:
2230

2331
* A tooltip component. See this page for a Telerik one: https://feedback.telerik.com/blazor/1406095-tooltip-support. When it gets implemented, this code will become simpler. The current mockup stores the button coordinates when the mouse enters it. See the code comments for more details.
@@ -111,6 +119,78 @@ There are several key aspects in implementing this:
111119
}
112120
````
113121

122+
### Per-Input Validation Popups
123+
124+
This sample uses a tooltip component and mimics clicks on its targets to make it show up. Comments in the code provide more details on the implementation approach and ideas for next steps in the implementation.
125+
126+
>caption Tooltips for validated inputs
127+
128+
````CSHTML
129+
@* This sample shows one way programatically show tooltips and display the validation summary in it. *@
130+
131+
@inject IJSRuntime JS
132+
133+
<script suppress-error="BL9992">
134+
// the scripts should be extracted in a separate js file
135+
window.triggerClick = (id) => {
136+
document.getElementById(id).click();
137+
}
138+
</script>
139+
140+
@using System.ComponentModel.DataAnnotations
141+
@* this is for the validation model only *@
142+
143+
<style>
144+
/* implement desired styling, here we just add white background so the default red text pops out */
145+
.ValidationTooltip .k-tooltip-content {
146+
background: white;
147+
}
148+
</style>
149+
150+
<EditForm Model="@validationModel" OnInvalidSubmit="@ShowTooltip">
151+
<DataAnnotationsValidator />
152+
153+
<TelerikTooltip TargetSelector="#required-field" ShowOn="@TooltipShowEvent.Click">
154+
<Template Context="RequiredContext">The field is required</Template>
155+
</TelerikTooltip>
156+
<TelerikTooltip TargetSelector="#length-field" ShowOn="@TooltipShowEvent.Click">
157+
<Template Context="Length">That text is too long</Template>
158+
</TelerikTooltip>
159+
160+
<TelerikTextBox @bind-Value="@validationModel.RequiredField" Id="required-field"></TelerikTextBox>
161+
<br /><br />
162+
<TelerikTextBox @bind-Value="@validationModel.LengthField" Id="length-field"></TelerikTextBox>
163+
<br /><br />
164+
<span @onmouseover="@(() => ShowTooltip())">
165+
<TelerikButton Primary="true">Submit</TelerikButton>
166+
</span>
167+
</EditForm>
168+
169+
@code {
170+
//model and validation
171+
class TextValidationModel
172+
{
173+
[Required]
174+
public string RequiredField { get; set; }
175+
176+
[StringLength(10, ErrorMessage = "That text is too long")]
177+
public string LengthField { get; set; }
178+
}
179+
180+
TextValidationModel validationModel = new TextValidationModel() { LengthField = "Too long text" };
181+
182+
async Task ShowTooltip()
183+
{
184+
// this will trigger manual click to the element with the passed id
185+
// you can filter and show the tooltips only for the fields that failed validation check
186+
await JS.InvokeVoidAsync("triggerClick", "required-field");
187+
await JS.InvokeVoidAsync("triggerClick", "length-field");
188+
}
189+
}
190+
191+
````
192+
193+
114194
## Notes
115195

116196
Showing individual `ValidationMessage` components for separate components and pointing the tooltip to them is beyond the scope of this article. While an `EditContext` will provide you with some events when validation and fields change, knowing whether a certain field is valid or not is not available out-of-the-box, and connecting this mockup to a particular location (that is, the `Top` and `Left` parameters) in the DOM will require JS Interop code that is highly dependent on the project, form and layout.

0 commit comments

Comments
 (0)