提问人:hfbpc 提问时间:11/6/2023 更新时间:11/6/2023 访问量:14
表单单步执行不同的组件。对象引用未设置为对象的实例。在 Telerik.Blazor.Components.TelerikForm.IsValid()
Forms step into different components. Object reference not set to an instance of an object. at Telerik.Blazor.Components.TelerikForm.IsValid()
问:
我正在尝试将表单验证的不同步骤分离到不同的组件中。因此,例如,用户在一个步骤中填写个人信息,而用户必须在下一个步骤中填写有关教育的信息。
你将如何解决这个问题?
现在,我在 Telerik 有一个试用期,所以我遵循他们的 Wizard 组件进行表单集成。
错误
错误:System.NullReferenceException:对象引用未设置为对象的实例。 在 Telerik.Blazor.Components.TelerikForm.IsValid() 在 C:\source\ApplicantSln\ApplicantTemp\Pages\Wizard\MainComponent.razor:第 50 行中的 ApplicantTemp.Pages.Wizard.MainComponent.OnRegistrationStepChange (WizardStepChangeEventArgs args) 在 System.RuntimeMethodHandle.InvokeMethod (对象目标、 Void** 参数、 签名 sig、 布尔值 isConstructor) 在 System.Reflection.MethodInvoker.Invoke (对象 obj、 IntPtr* 参数、 BindingFlags invokeAttr) --- 从上一个位置---结束堆栈跟踪 在 Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(任务任务) 在 Telerik.Blazor.Components.Common.BaseComponent.InvokeEvent[T] (EventCallback'1 eventCallback,T args) 在 Telerik.Blazor.Components.TelerikWizard.ChangeStep (Int32 newStepIndex) 在 Telerik.Blazor.Components.TelerikWizard.GoToNextPage() 在 Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(任务任务) 在 Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(任务任务ToHandle,组件状态owningComponentState)
public void OnRegistrationStepChange(WizardStepChangeEventArgs args)
{
// Add debugging code to inspect the state of RegisterForm and other variables
Console.WriteLine("OnRegistrationStepChange is called.");
Console.WriteLine($"RegisterForm is {RegisterForm}");
var isFormValid = RegisterForm.IsValid(); //ERROR FROM HERE!!!!!!!!
if (!isFormValid)
{
IsRegistrationValid = false;
args.IsCancelled = true;
}
else
{
IsRegistrationValid = true;
}
}
MainComponent.razor
@page "/reg"
@using System.ComponentModel.DataAnnotations
<TelerikWizard @bind-Value="@Value" OnFinish="@OnFinishHandler" Width="700px">
<WizardSteps>
<WizardStep Label="Registration" Icon="@FontIcon.User" OnChange="@OnRegistrationStepChange" Valid="@IsRegistrationValid">
<Content>
<RegistrationForm UserModel="@UserModel" RegisterForm="@RegisterForm" />
</Content>
</WizardStep>
<WizardStep Label="Shipping address" Icon="@FontIcon.MapMarkerTarget" OnChange="@OnShippingStepChange" Valid="@IsShippingValid">
<Content>
<ShippingForm ShippingModel="@ShippingModel" ShippingForm="@ShippingForm" />
</Content>
</WizardStep>
<WizardStep Label="Confirmation">
<Content>
<h2>Registration completed.</h2>
</Content>
</WizardStep>
</WizardSteps>
</TelerikWizard>
@code {
async Task OnFinishHandler()
{
ShowWizard = false;
await Dialog.AlertAsync("The Registration was submitted successfully", "Done");
}
protected override void OnInitialized()
{
// Add debugging code to inspect the state of RegisterForm
Console.WriteLine("MainComponent is being initialized.");
Console.WriteLine($"RegisterForm is {RegisterForm}");
}
public bool? IsRegistrationValid { get; set; }
public bool? IsShippingValid { get; set; }
[CascadingParameter]
public DialogFactory Dialog { get; set; }
public bool ShowWizard { get; set; } = true;
public int Value { get; set; }
public TelerikForm RegisterForm { get; set; } = new TelerikForm();
public UserPerson UserModel { get; set; } = new UserPerson();
public TelerikForm ShippingForm { get; set; }
public ShippingDetails ShippingModel { get; set; } = new ShippingDetails();
public void OnRegistrationStepChange(WizardStepChangeEventArgs args)
{
// Add debugging code to inspect the state of RegisterForm and other variables
Console.WriteLine("OnRegistrationStepChange is called.");
Console.WriteLine($"RegisterForm is {RegisterForm}");
var isFormValid = RegisterForm.IsValid();
if (!isFormValid)
{
IsRegistrationValid = false;
args.IsCancelled = true;
}
else
{
IsRegistrationValid = true;
}
}
public void OnShippingStepChange(WizardStepChangeEventArgs args)
{
// Add debugging code to inspect the state of ShippingForm and other variables
Console.WriteLine("OnShippingStepChange is called.");
Console.WriteLine($"ShippingForm is {ShippingForm}");
if (Value < args.TargetIndex)
{
var isFormValid = ShippingForm.IsValid();
if (!isFormValid)
{
IsShippingValid = false;
args.IsCancelled = true;
}
else
{
IsShippingValid = true;
}
}
}
public class UserPerson
{
[Required]
public string FirstName { get; set; } = "John";
[Required]
public string LastName { get; set; } = "Smith";
[Required]
public string Email { get; set; } = "[email protected]";
[MinLength(3, ErrorMessage = "The password should be at least 3 characters.")]
[Required]
public string Password { get; set; }
public DateTime? BirthDate { get; set; }
[Range(typeof(bool), "true", "true", ErrorMessage = "You must agree with the terms.")]
[Display(Name = "Accept Terms and Conditions")]
public bool AcceptTerms { get; set; }
}
public class ShippingDetails
{
[Required]
public string Country { get; set; }
[Required]
public string City { get; set; }
[Required]
public string AddressLine { get; set; }
public string AddressLine2 { get; set; }
}
}
RegistrationForm.razor
@using static ApplicantTemp.Pages.Wizard.MainComponent;
<!-- RegistrationForm.razor -->
@inherits UserFormBase
<TelerikForm Model="UserModel" @ref="RegisterForm">
<!-- Registration form content goes here -->
<FormValidation>
<DataAnnotationsValidator></DataAnnotationsValidator>
</FormValidation>
<FormItems>
<FormItem LabelText="First Name*:" Field="@nameof(UserModel.FirstName)"></FormItem>
<FormItem LabelText="Last Name*:" Field="@nameof(UserModel.LastName)"></FormItem>
<FormItem Field="@nameof(UserModel.Email)">
<Template>
<label for="mail" class="k-label k-form-label">Email*:</label>
<TelerikTextBox Id="mail" @bind-Value="@UserModel.Email" InputMode="email" PlaceHolder="[email protected]"></TelerikTextBox>
<TelerikValidationMessage For="@(() => UserModel.Email)"></TelerikValidationMessage>
</Template>
</FormItem>
<FormItem Field="@nameof(UserModel.Password)">
<Template>
<label for="pass" class="k-label k-form-label">Password*:</label>
<TelerikTextBox Id="pass" @bind-Value="@UserModel.Password" Password="true"></TelerikTextBox>
<TelerikValidationMessage For="@(() => UserModel.Password)"></TelerikValidationMessage>
</Template>
</FormItem>
<FormItem Field="@nameof(UserModel.AcceptTerms)" />
</FormItems>
<FormButtons></FormButtons>
</TelerikForm>
UserFormBase.razor
@using static ApplicantTemp.Pages.Wizard.MainComponent;
<!-- UserFormBase.razor -->
@inherits ComponentBase
@code {
[Parameter]
public UserPerson UserModel { get; set; }
[Parameter]
public TelerikForm RegisterForm { get; set; }
}
答: 暂无答案
评论