|
| 1 | +--- |
| 2 | +title: ValidationMessage as Tooltip |
| 3 | +description: How to show validation errors and messages in a tooltip |
| 4 | +type: how-to |
| 5 | +page_title: Validation error in tooltip |
| 6 | +slug: common-kb-validation-error-in-tooltip |
| 7 | +position: |
| 8 | +tags: |
| 9 | +ticketid: 1454018 |
| 10 | +res_type: kb |
| 11 | +--- |
| 12 | + |
| 13 | + |
| 14 | +## Description |
| 15 | + |
| 16 | +Can you please advise me on how to display validation message as tooltip? |
| 17 | + |
| 18 | + |
| 19 | +## Solution |
| 20 | + |
| 21 | +There are several key aspects in implementing this: |
| 22 | + |
| 23 | +* 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. |
| 24 | +* An event that will show and hide the tooltip. In this sample, the `OnValidSubmit` and `OnInvalidSubmit` of the `EditForm` are used. |
| 25 | +* You need to place the validation summary component in the tooltip, which means the tooltip must be inside the form. |
| 26 | + |
| 27 | +>caption Validation Summary in a Tooltip |
| 28 | +
|
| 29 | +````CSHTML |
| 30 | +@* This sample shows one way to mock a tooltip and display the validation summary in it. *@ |
| 31 | +
|
| 32 | +@using System.ComponentModel.DataAnnotations |
| 33 | +@* this is for the validation model only *@ |
| 34 | +
|
| 35 | +<style> |
| 36 | + /* implement desired styling, here we just add white background so the default red text pops out */ |
| 37 | + .ValidationTooltip .k-tooltip-content { |
| 38 | + background: white; |
| 39 | + } |
| 40 | +</style> |
| 41 | +
|
| 42 | +<EditForm Model="@validationModel" OnValidSubmit="@HideTooltip" OnInvalidSubmit="@ShowTooltip"> |
| 43 | + <DataAnnotationsValidator /> |
| 44 | +
|
| 45 | + <TelerikAnimationContainer @ref="@AnimationContainer" Top=@TopStyle Left=@LeftStyle Width="300px" Height="100px" [email protected]> |
| 46 | + <div role="tooltip" |
| 47 | + class="ValidationTooltip k-widget k-tooltip k-tooltip-closable k-popup k-group k-reset k-state-border-up" |
| 48 | + style="margin-left: 12px;"> |
| 49 | + <div class="k-tooltip-content"> |
| 50 | + <div class="template-wrapper"> |
| 51 | + <ValidationSummary></ValidationSummary> |
| 52 | + </div> |
| 53 | + </div> |
| 54 | + <div class="k-callout k-callout-w"></div> |
| 55 | + </div> |
| 56 | + </TelerikAnimationContainer> |
| 57 | +
|
| 58 | + <TelerikTextBox @bind-Value="@validationModel.RequiredField"></TelerikTextBox> |
| 59 | + <br /><br /> |
| 60 | + <TelerikTextBox @bind-Value="@validationModel.LengthField"></TelerikTextBox> |
| 61 | + <br /><br /> |
| 62 | + <span @onmouseover="@StoreBtnPos" |
| 63 | + @onmouseout="@HideTooltip">@* you may want to remove the onmouseout handler, depending on the UX you are looking for *@ |
| 64 | + <TelerikButton Primary="true">Submit</TelerikButton> |
| 65 | + </span> |
| 66 | +</EditForm> |
| 67 | +
|
| 68 | +@code { |
| 69 | + //tooltip implementation |
| 70 | + public int Top { get; set; } |
| 71 | + public int Left { get; set; } |
| 72 | + public string TopStyle { get { return Top.ToString() + "px"; } } |
| 73 | + public string LeftStyle { get { return Left.ToString() + "px"; } } |
| 74 | + public int ButtonOffsetY { get; set; } = -50; |
| 75 | + public int ButtonOffsetX { get; set; } = 50; |
| 76 | + public TelerikAnimationContainer AnimationContainer { get; set; } |
| 77 | +
|
| 78 | + // TODO - get the desired Top and Left according to the location you want to show the tooltip |
| 79 | + // in this sample, we store the button coordinates if the user mouses over the button |
| 80 | + // if the user uses the keyboard only then there will be an issue and to handle that |
| 81 | + // you'd have to use some JS interop to get those coordinates beforehand |
| 82 | +
|
| 83 | + void StoreBtnPos(MouseEventArgs args) |
| 84 | + { |
| 85 | + Top = (int)args.ClientY + ButtonOffsetY; |
| 86 | + Left = (int)args.ClientX + ButtonOffsetX; |
| 87 | + } |
| 88 | +
|
| 89 | + //model and validation |
| 90 | + class TextValidationModel |
| 91 | + { |
| 92 | + [Required] |
| 93 | + public string RequiredField { get; set; } |
| 94 | +
|
| 95 | + [StringLength(10, ErrorMessage = "That text is too long")] |
| 96 | + public string LengthField { get; set; } |
| 97 | + } |
| 98 | +
|
| 99 | + TextValidationModel validationModel = new TextValidationModel() { LengthField = "Too long text" }; |
| 100 | +
|
| 101 | + async void HideTooltip() |
| 102 | + { |
| 103 | + await AnimationContainer.HideAsync(); |
| 104 | + // you may also want to create your own EditContext and hook to its OnValidationStateChanged event to hide the tooltip |
| 105 | + } |
| 106 | +
|
| 107 | + async void ShowTooltip() |
| 108 | + { |
| 109 | + await AnimationContainer.ShowAsync(); |
| 110 | + } |
| 111 | +} |
| 112 | +```` |
| 113 | + |
| 114 | +## Notes |
| 115 | + |
| 116 | +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. |
| 117 | + |
0 commit comments