How to disable tooltip when ValidationMessage is empty

Hello,
Here's my snippet :

[...]
SfTooltip Target="#adresse-1" OpensOn="Hover">
TooltipTemplates>
Content>
                    ValidationMessage For="@(() => Adresse.Adresse1)" />
/Content>
/TooltipTemplates>
SfTextBox ID="adresse-1" Placeholder='Adresse 1' @bind-Value="@Adresse.Adresse1" FloatLabelType='@FloatLabelType.Auto'>
/SfTooltip>
[...]

The tooltip appears as expected but I want it to not appears when there is no Validation Message.
Any Tips ?
Brice.

6 Replies 1 reply marked as answer

PM Ponmani Murugaiyan Syncfusion Team June 19, 2020 01:06 PM UTC

Hi Brice, 

Greetings from Syncfusion support. 

As per your requirement, we have prepared sample for your reference. Please find the code snippet and test sample below. 

[Index.razor] 

@using Syncfusion.Blazor.Buttons 
@using Syncfusion.Blazor.Popups 
@using Syncfusion.Blazor.Inputs 
@using System.ComponentModel.DataAnnotations; 
 
<div class="row"> 
    <div style="width:100%;margin:20px;"> 
        <EditForm EditContext="@editContext"> 
            <DataAnnotationsValidator /> 
            <div class="form-group"> 
                <SfTooltip @ref="tooltipObj" OpensOn="custom" > 
                    <SfTextBox Placeholder="Test Property" FloatLabelType='@FloatLabelType.Always' @bind-Value="model.TestProperty" CssClass="@cssClass" Blur="TestPropertyBlurEvent"></SfTextBox> 
                    <TooltipTemplates> 
                        <Content> 
                            <ValidationMessage For="@(() => model.TestProperty)"></ValidationMessage> 
                        </Content> 
                    </TooltipTemplates> 
                </SfTooltip> 
            </div> 
            <SfButton IsPrimary="true" HtmlAttributes="@(new Dictionary<string, object> { { "type", "submit" } })">Save</SfButton> 
        </EditForm> 
    </div> 
</div> 
 
@code{ 
 
        SfTooltip tooltipObj; 
private Test model; 
    private EditContext editContext; 
    private string temp { get; set; } 
    private double tipx { get; set; } 
    private double tipy { get; set; } 
    private string cssClass { get; set; } 
 
    protected override void OnInitialized() 
    { 
        model = new Test(); 
        editContext = new EditContext(model); 
    } 
 
    public class Test 
    { 
        [Required] 
        public string TestProperty { get; set; } 
    } 
 
    public void TestPropertyBlurEvent(FocusOutEventArgs args) 
    { 
        if (!editContext.Validate()) 
        { 
            cssClass = "e-error"; 
            tooltipObj.Open(); 
       } 
        else 
        { 
            cssClass = "e-success"; 
            tooltipObj.Close(); 
        } 
    } 
} 


Kindly check with the above sample. Please get back us, if you need further assistance. 

Regards, 
Ponmani M 




BF Brice FROMENTIN June 20, 2020 09:36 AM UTC

Thanks for the answer but with hundreds of Textbox like this, this solution is hard to implement.

Is it possible to have a boolean option on SfTooltip that allow to not display the tootip if it's empty ?

Regards.

Brice.


BF Brice FROMENTIN June 20, 2020 11:21 AM UTC

I tried to "componentized" the answer :


@using Syncfusion.Blazor.Inputs
@using Syncfusion.Blazor.Popups

<SfTooltip @ref="@tooltip"
           Target="@("#" + ID)" OpensOn="Custom">
    <TooltipTemplates>
        <Content>
            @ValidationTemplate
        </Content>
    </TooltipTemplates>
    <SfTextBox @ref="@textbox"
               ID="@ID" Placeholder="@Placeholder" @bind-Value="@Value" Readonly="@Readonly" FloatLabelType='@FloatLabelType.Auto'
               CssClass="e-error"
               @onmouseover="OnMouseOver" @onmouseout="OnMouseOut"></SfTextBox>
</SfTooltip>

@code
{
    SfTooltip tooltip;
    SfTextBox textbox;

    [Parameter]
    public string ID { get; set; }

    [Parameter]
    public string Placeholder { get; set; }

    [Parameter]
    public bool Readonly { get; set; }

    [Parameter]
    public RenderFragment ValidationTemplate { get; set; }

    [Parameter]
    public EditContext EditContext { get; set; }

    private string _value;

    [Parameter]
    public string Value
    {
        get
        {
            return _value;
        }
        set
        {
            if (_value != value)
            {
                ValueChanged.InvokeAsync(value);
            }

            _value = value;
        }
    }

    [Parameter]
    public EventCallback<string> ValueChanged { get; set; }

    void OnMouseOver(MouseEventArgs args)
    {
        EditContext.Validate();

        if (textbox.CssClass == "e-error")
        {
            tooltip.Open();
        }
    }

    void OnMouseOut(MouseEventArgs args)
    {
        tooltip.Close();
    } }



And called it liked this :

            <KTextBox ID="adresse" Placeholder="Adresse" @bind-Value="@Adresse.Adresse1" Readonly="@Parameters.Wizard.IsReadOnly" EditContext="editContextHeader">
                <ValidationTemplate>
                    <ValidationMessage For="@(() => Adresse.Adresse1)" />
                </ValidationTemplate>
            </KTextBox>

The tooltip never opens.

Thx.




SA Shameer Ali Baig Sulaiman Ali Baig Syncfusion Team June 23, 2020 10:51 AM UTC

Hi Brice, 
 
Greetings from Syncfusion support. 
 
We have looked into your requirement on displaying validation Tooltip for textbox, only when the textbox value is invalid. We have updated our previous shared solution for showing Tooltip only on validating the textboxes.  
 
In the below solution, when you focus out on any textbox, validation will take place and if textbox is empty then Tooltip will open with validation message and it will remain open, until validate the textbox or focus on other textboxes.  
 
Please, find the solution below. 
 
[index.razor] 
@page "/" 
 
@using Syncfusion.Blazor.Buttons 
@using Syncfusion.Blazor.Popups 
@using Syncfusion.Blazor.Inputs 
@using System.ComponentModel.DataAnnotations; 
@inject IJSRuntime JsRuntime 
@using Newtonsoft.Json 
 
<div class="row"> 
    <div style="width:100%;margin:20px;"> 
        <EditForm EditContext="@editContext"> 
            <DataAnnotationsValidator /> 
            <div class="form-group"> 
                <SfTooltip @ref="tooltipObj" ID="tooltip" OpensOn="Custom"> 
                    <SfTextBox ID="textbox1" @bind-Value="model.TestProperty" Focus="focused" Blur="@(e => TestPropertyBlurEvent(e, "textbox1"))"></SfTextBox> 
                    <br> 
                    <br> 
                    <br> 
                    <SfTextBox ID="textbox2" @bind-Value="model.TestProperty" Focus="focused" Blur="@(e => TestPropertyBlurEvent(e, "textbox2"))"></SfTextBox> 
                    <br> 
                    <br> 
                    <br> 
                    <SfTextBox ID="textbox3" @bind-Value="model.TestProperty" Focus="focused" Blur="@(e => TestPropertyBlurEvent(e, "textbox3"))"></SfTextBox> 
                    <TooltipTemplates> 
                        <Content> 
                            <ValidationMessage For="@(() => model.TestProperty)"></ValidationMessage> 
                        </Content> 
                    </TooltipTemplates> 
                </SfTooltip> 
            </div> 
            <SfButton IsPrimary="true" HtmlAttributes="@(new Dictionary<string, object> { { "type", "submit" } })">Save</SfButton> 
        </EditForm> 
    </div> 
</div> 
 
@code{ 
 
    [Inject] 
    public IJSRuntime JSRuntime { get; set; } 
    SfTooltip tooltipObj; 
    private Test model; 
    private EditContext editContext; 
 
 
    protected override void OnInitialized() 
    { 
        model = new Test(); 
        editContext = new EditContext(model); 
    } 
 
    public class Test 
    { 
        [Required] 
        public string TestProperty { get; set; } 
    } 
 
    public void focused(FocusInEventArgs args) 
    { 
        tooltipObj.Close(); 
    } 
 
    public async Task TestPropertyBlurEvent(FocusOutEventArgs args, string ID) 
    { 
        if (!editContext.Validate()) 
        { 
            // Pass the id of the focused out of textbox to the client side to open the tooltip targetted to that textbox. 
            var value1 = await JSRuntime.InvokeAsync<string>("AddPanel", ID); 
        } 
        else 
        { 
            tooltipObj.Close(); 
        } 
    } 
} 
 
To open the tooltip targeted to the last focused out textboxes, we need to pass the target element as argument of open method of Tooltip, this can be achievable by using JS Interop.  
 
[_Host.cshtml] 
    <script> 
        function AddPanel(args) { 
document.getElementById("tooltip").ej2_instances[0].open(document.getElementById(args)); 
        } 
    </script> 
 
Also, validation can be performed by binding the ValidationMessage component with textbox component. This component will DOM element with validation message, if the input field is invalid. And when using TooltipTemplates component to add content of the Tooltip, we cannot check whether tooltip content has any value or not as the component will update the DOM directly and it will not be updated in Content property. So, we open tooltip, when the validation fails in the given solution. 
 
Please, download the sample with above solution from the following link. 
 
 
Please, let us know if you need any further assistance. 
 
Regards, 
Shameer Ali Baig S. 


Marked as answer

BF Brice FROMENTIN June 24, 2020 09:26 AM UTC

Thanks for your solution. I use this one instead :

TypeScript

       function hasContent(id) {

            var message = document.querySelector("#" + id + "_content > .e-tip-content > .validation-message");

            return (message !== null);

        }


 Razor

    void OnOpen(TooltipEventArgs args)

    {

        var jsRuntimeSync = (IJSInProcessRuntime)jsRuntime;

        var hasMessage = jsRuntimeSync.Invoke<bool>("kairos.layout.hasContent", tooltip.ID);


        args.Cancel = !hasMessage;

    }

Do you think, it is a good way to achieve my requirement ?



SP Sowmiya Padmanaban Syncfusion Team June 25, 2020 09:00 AM UTC

Hi Brice,  
 
Thanks for your update. 
 
We are happy to know that you found the solution to meet your requirement with Tooltip. If your proposed solution meets your requirement with Tooltip, then you use your proposed solution.  
 
If you face any issue with your solution, then share us the issue replicating sample with us. We will check the problem and provide the solution at the earliest. 
 
Regards,  
Sowmiya.P 


Loader.
Up arrow icon