How to bind to SfTextBox in a child component?

I'm sure what I want to do is possible, and it's probably not even hard, but I can't figure it out...

I have a lot of text boxes that all look like this:

            <div class="col-4">
                <SfTextBox Placeholder="Surname" FloatLabelType="@FloatLabelType.Always"
                @bind-Value="@objNewPerson.surname">
                </SfTextBox>
                <ValidationMessage For="@(() => objNewPerson.surname)" />
            </div>

To simplify my code, reduce the bulk, and ensure consistency, I'd like to move that into a component, so that (in the parent)  I could do something like:
     <UseSFText Placeholder="Surname" @thisObject="@objNewPerson.surname" cssClass="col-4" />

Using a (child) component that looks something like:

     <div class="@cssClass">
         <SfTextBox Placeholder="@Placeholder" 
             FloatLabelType="@FloatLabelType.Always"
             @bind-Value="@thisObject">
         </SfTextBox>
         <ValidationMessage For="@(() => thisObject )" />
     </div>

     @code {
         [Parameter] public string cssClass { get; set; }  = String.Empty;
         [Parameter] public string Placeholder { get; set; } = "Enter Text";
         [Parameter] public string thisObject { get; set; } 
     }

However, I'm not getting the binding right. :(  (I've tried a few combinations of where to put quotes and @-symbols.)

Can someone help point me in the right direction?

Thank you. 




3 Replies 1 reply marked as answer

SN Sevvandhi Nagulan Syncfusion Team March 30, 2021 06:44 AM UTC

Hi Byron, 


Greetings from Syncfusion support. 


We checked your query.  Using the ValueChange event, we need to pass data from the parent component to the child component in order to get the current value in the child component. Please see the code below for passing data to the child component. 


[Parent component] 


<div class="@cssClass"> 
    <SfTextBox Placeholder="@Placeholder" 
               FloatLabelType="@FloatLabelType.Always" 
               @bind-Value="@thisObject" ValueChange="@OnValueChanged"> 
    </SfTextBox> 
</div> 
@code { 
    [Parameter] public string cssClass { get; set; } = String.Empty; 
    [Parameter] public string Placeholder { get; set; } = "Enter Text"; 
    [Parameter] public string thisObject { get; set; } 
    [Parameter] 
    public EventCallback<string> thisObjectChanged { get; set; } 
 
    private void OnValueChanged(ChangedEventArgs e) 
    { 
        thisObject = e.Value.ToString(); 
        thisObjectChanged.InvokeAsync(thisObject); 
    } 
} 
     

 
[Child component] 

<UseSFText Placeholder="Surname" @bind-thisObject="@objNewPerson.surname" cssClass="col-4" /> 
 
 
@code { 
 
    public class Object 
    { 
        public string surname { get; set; } 
    } 
 
    public Object objNewPerson = new Object(); 
} 


Please find the sample below. 



 


Please check the above sample and get back to us if you need further assistance. 


Regards, 
Sevvandhi N 



BY Byron March 30, 2021 07:12 AM UTC

That is awesome, thank you very much. The databinding works now, but...

When I do it this way, I get the validation message:
            <div class="col-4">
                <SfTextBox Placeholder="Surname" FloatLabelType="@FloatLabelType.Always"
                @bind-Value="@objNewPerson.surname">
                </SfTextBox>
                <ValidationMessage For="@(() => objNewPerson.surname)" />
            </div>

But when I do it this way, the validation doesn't show it (the validation occurs, it shows in the summary, but the "validationMessage" doesn't show, and the sfTextbox doesn't pick up the red or green border. Would you have any suggestions about that?

[Parent - NewPerson.razor]
<UseSFText Placeholder="Surname" @bind-thisObject="@objNewPerson.surname" cssClass="col-4" />
            
[Child - UseSFText.razor ]
<div class="@cssClass">
    <SfTextBox Placeholder="@Placeholder" 
        FloatLabelType="@FloatLabelType.Always"
        @bind-Value="@thisObject"
        ValueChange="@OnValueChanged">
    </SfTextBox>
    <ValidationMessage For="@(() => thisObject )" />
</div>

@code {
    [Parameter] public string cssClass { get; set; }  = String.Empty;
    [Parameter] public string Placeholder { get; set; } = "Enter Text";
    [Parameter] public string thisObject { get; set; } = null!;
    [Parameter] public EventCallback<String> thisObjectChanged { get; set; }

    private void OnValueChanged(ChangedEventArgs e)
    {
        thisObject = e.Value.ToString();
        thisObjectChanged.InvokeAsync(thisObject);
    }
}

Thanks again






SN Sevvandhi Nagulan Syncfusion Team March 31, 2021 03:14 PM UTC

Hi Byron, 
 
 
We checked your query. We suggest that you to validate the edit form in the child component. Kindly refer the following code. 
 
 
[Index.razor] 
 
 
<EditForm Model="@objNewPerson"> 
    <DataAnnotationsValidator /> 
    <UseSFText Placeholder="Surname" @bind-thisObject="@objNewPerson.surname" cssClass="col-4" /> 
    <ValidationMessage For="@(() => objNewPerson.surname )" /> 
    <button type="submit">Submit</button> 
</EditForm> 
 
 
 
@code { 
 
    public class Object 
    { 
        [Required] 
        public string surname { get; set; } 
    } 
 
    public Object objNewPerson = new Object(); 
 
} 
 
 
Please find the sample below. 
 
 
 
 
Please check the above sample and get back to us if you need further assistance. 
 
 
Regards, 
Sevvandhi N 


Marked as answer
Loader.
Up arrow icon