empty string handling

Hey, 


I like to use the TextArea component, but I have a problem with empty strings. the textArea always returns null, even if my binded field is a non nullable string.


I tried to catch the new value and transform every null into an empty string, but this leads to an infinite loop in my component structure.

Is there a way to tell the textarea to use emty.string instead of null?


regards



3 Replies

PK Priyanka Karthikeyan Syncfusion Team March 3, 2025 01:46 PM UTC

Hi Alexander Kuhlmann,

 

Thank you for reaching out to us with your query. We truly appreciate your interest in understanding how to handle the scenario where you’d like to return an empty string instead of null. To achieve this, we can leverage the @oninput event and the value property in Blazor. 

 

To ensure that the value is treated as an empty string instead of null, we can handle this by using the @oninput event to capture user input and update the bound property accordingly. Here’s how it can be implemented:

 

 


<SfTextArea Value="@TextValue"  Placeholder="Enter the Address" FloatLabelType="@FloatLabelType.Auto" @oninput="HandleInput"></SfTextArea>

@code {
   private string _textValue = string.Empty;

   private string TextValue
   {
       get => _textValue;
       set
       {
           var newValue = value ?? string.Empty;
           if (_textValue != newValue)
           {
               _textValue = newValue;
           }
       }
   }

   private void HandleInput(ChangeEventArgs e)
   {
       TextValue = e.Value?.ToString();
   }
}

 

Sample: https://blazorplayground.syncfusion.com/hDhyZgNAfYSgkvEH

 

Regards,

Priyanka K



AK Alexander Kuhlmann March 11, 2025 08:43 AM UTC

Hi  Priyanka,


thank you for your help.

This works for me.


But it would be great if the TextArea could handle empty strings correctly. The TextBox for example is doing it right.


Regards

Alex 



PK Priyanka Karthikeyan Syncfusion Team March 12, 2025 01:25 PM UTC

Hi Alexander Kuhlmann,

 

Thank you for your update. We understand that you need to receive an empty string instead of null in the textarea. However, the current behavior returns null because the float label type is functioning as intended and is also required for other features to work correctly. As a result, we have maintained the null value for consistency.


To address your specific requirement, you can use the previously shared solution to convert the null value to an empty string programmatically. We appreciate your understanding and cooperation in this matter.

If you have any further questions or need additional assistance, please feel free to reach out.


Regards,

Priyanka K


Loader.
Up arrow icon