Declined
Last Updated: 20 Mar 2020 08:01 by ADMIN
Fernando
Created on: 19 Mar 2020 20:22
Category: DatePicker
Type: Bug Report
1
What should be the default Format for DatePicker when an empty string and null doesn't give the same result?

I'm making a wrapper on top of TelerikDatePicker and this is how my component is working

<TelerikDatePicker T="TDate"
                   Value="@_value"
                   ValueChanged="@ValueChanged"
                   ValueExpression="@ValueExpression"
                   Enabled="@Enabled"
                   Class="@ClassMapper.AsString()"
                   Format="@Format" />

@code {

    // ... the other properties

    [Parameter]
    public virtual string Format { get; set; }

}

But here is the problem. This component will give me the error

Error: System.AggregateException: One or more errors occurred. (Value cannot be null. (Parameter 'input'))
 ---> System.ArgumentNullException: Value cannot be null. (Parameter 'input')
   at System.Text.RegularExpressions.Regex.Replace(String input, String replacement)
   at System.Text.RegularExpressions.Regex.Replace(String input, String pattern, String replacement)
   at Telerik.Blazor.Common.DateHelpers.FormatHelper.ExpandFormat(String format)
   at Telerik.Blazor.Common.DateHelpers.FormatHelper.ConvertToKendoIntl(String format)
   at Telerik.Blazor.Components.TelerikDateInputBase`1.GetDateInputOptions()
   at Telerik.Blazor.Components.TelerikDateInputBase`1.OnAfterRenderAsync(Boolean firstRender)

 

So another solution would be set it to string.Empty, but than it will have a different result when I don't pass the Format property.

 

Here are the cases:

    [Parameter]
    public virtual string Format { get; set; } // gives the error

 

    [Parameter]
    public virtual string Format { get; set; } = ""; // doesn't give the default result

 

I had to make a walkaround to not pass Format to the component for it to work

@if (!string.IsNullOrEmpty(Format))
{
    <TelerikDatePicker T="TDate"
                       Value="@_value"
                       ValueChanged="@ValueChanged"
                       ValueExpression="@ValueExpression"
                       Enabled="@Enabled"
                       Class="@ClassMapper.AsString()"
                       Format="@Format" />
}
else
{
    <TelerikDatePicker T="TDate"
                       Value="@_value"
                       ValueChanged="@ValueChanged"
                       ValueExpression="@ValueExpression"
                       Enabled="@Enabled"
                       Class="@ClassMapper.AsString()" />
}

 

What should I set as a default value to my wrapper's Format property so it doesn't give me the error and also get's the default value of Format?

 

Please don't say that I should keep the @if (!string.IsNullOrEmpty(Format)), it's so bad and I don't want to use telerik and still have to make walkarounds.

1 comment
ADMIN
Marin Bratanov
Posted on: 20 Mar 2020 08:01

Hi Fernando, 

When you wrap components like that, null checks become the responsibility of the wrapping code. When you use our component directly, we have a default value for the Format parameter, which should now come from the component wrapper. A string is, by default, null,  so the error you describe is expected - you can't pass a null string for formatting in .NET without errors.

So, the wrapper needs a default value and you can choose one that suits your preferences. One possible value could be CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.

 

Regards,
Marin Bratanov
Progress Telerik

 UI for Blazor