In some of of my pages my viewmodels will already have values, so i would like the validation to run on page init, and correctly color the controls.
I need to be able to do this on most of the components.
See attached example (test.razor)
Open: http://localhost:44734/test
Notice for some reason the bottom component is instantly validating the user input, but none of the other component do.
If you input the same value in the 2 other components they will also validate and show green circle.
The SfTextBox has
ValidateOnInput, but does not show as valid.
SfDateTimePicker has
ValidateOnInput , and instantly shows valid status.
I would like all input/dropdown components to behave the same, idealy all components should have ValidateOnInput and behave the same.
<SfDateTimePicker TValue="DateTime" StrictMode=true ValidateOnInput=true @bind-Value="@_viewModel.Date" Min="@min" ShowTodayButton=true ShowClearButton=true FloatLabelType="FloatLabelType.Auto"> </SfDateTimePicker>
<ValidationMessage For="@(() => _viewModel.Date)" />
protected override async Task OnInitializedAsync()
{
GenerateFakeData();
_viewModel = new ViewModelValdiation();
_viewModel.Text = "Test";
_viewModel.DropdownSelected = _fakeDataList.FirstOrDefault().ID;
_viewModel.Date = new DateTime(2021, 12, 04);
}
public DateTime min { get; set; } = new DateTime(2021, 12, 04); |
How do i override this standard behavior?
Is there realy no way to force a validation?
<EditForm EditContext="@editContext">
<DataAnnotationsValidator />
<div class="row d-flex justify-content-center">
<div class="col-md-8">
<SfTextBox CssClass="@cssClass1" Multiline="true" ValidateOnInput=true FloatLabelType="@FloatLabelType.Auto" @bind-Value="_viewModel.Text"></SfTextBox>
<ValidationMessage For="@(() => _viewModel.Text)" />
</div>
</div>
<div class="row d-flex justify-content-center">
<div class="col-md-8">
<SfDropDownList TItem="DropdownItems" CssClass="@cssClass2" TValue="Guid" AllowFiltering="true" IgnoreCase="true" EnableVirtualization="true" PopupHeight="230px" @bind-Value="@_viewModel.DropdownSelected" DataSource="@_fakeDataList" FloatLabelType="FloatLabelType.Auto">
<DropDownListFieldSettings Text="Name" Value="ID"></DropDownListFieldSettings>
</SfDropDownList>
<ValidationMessage For="@(() => _viewModel.DropdownSelected)" />
</div>
</div>
<div class="row d-flex justify-content-center">
<div class="col-md-8">
<SfDateTimePicker TValue="DateTime" StrictMode=true CssClass="@cssClass3" ValidateOnInput=true @bind-Value="@_viewModel.Date" Min="@min" ShowTodayButton=true ShowClearButton=true FloatLabelType="FloatLabelType.Auto"> </SfDateTimePicker>
<ValidationMessage For="@(() => _viewModel.Date)" />
</div>
</div>
</EditForm>
@code {
public string cssClass1 { get; set; } = "e-outline e-custom";
public string cssClass2 { get; set; } = "e-custom1";
public string cssClass3 { get; set; } = "e-custom2";
private ViewModelValdiation _viewModel;
private EditContext editContext;
protected override async Task OnInitializedAsync()
{
_viewModel = new ViewModelValdiation();
editContext = new EditContext(_viewModel);
GenerateFakeData();
_viewModel.Text = "Test";
_viewModel.DropdownSelected = _fakeDataList.FirstOrDefault().ID;
_viewModel.Date = new DateTime(2021, 12, 04);
if (!editContext.Validate())
{
cssClass1 = "e-error";
cssClass2 = "e-error";
cssClass3 = "e-error";
}
else
{
cssClass1 = "e-success";
cssClass2 = "e-success";
cssClass3 = "e-success";
}
StateHasChanged();
} |