如何验证EditForm中的第二个数据位?

huangapple go评论55阅读模式
英文:

How can I validate my second bit of data in EditForm?

问题

以下是您要翻译的内容:

"这个编辑表单允许用户编辑我的产品表中的产品。然后,数据将被更新到数据库中。一切都正常运作。然后我添加了我的最终 '评论' 框。我有一个审计表,它将记录对产品进行任何编辑的时间。审计记录包括:一个ID,正在更改的产品的ID,进行更改的用户的ID,审计日期和评论。其他字段的验证,例如产品名称,都有效,但现在我有一个评论框,其中验证未被检查,并引发错误。我认为这与传递模型以便可以检查模型的验证有关,但当我尝试添加第二个模型来进行审计时,一切都变成了红色。"

请注意,代码部分已被保留为原文,未进行翻译。

英文:

This editform allows the user to edit a product from my products table. The data will then be updated to the database. This worked fine. Then I added my final 'comment' box. See I have an audit table which will record when any edits are made to the products. The audit record contains: an ID, the ID of the product being changed, the ID of the user making the change, the audit date and a comment. The validation of the other fields e.g. product.productname works but now I have a comment box where the validation is not being checked and is causing an error. I think it has to do with the fact that a Model is passed so that the models validation can be checked but when I tried to add a second model for the audit everything went red.

<EditForm Model=@selectedProduct OnValidSubmit="HandleSubmit">
                <DataAnnotationsValidator />
                <div class="form-group">
                                                                      
                        <label for="productName">Name:</label>
                        <InputText name="productName" @bind-Value="selectedProduct .ProductName"></InputText>
                        <ValidationMessage For="@(() => selectedProduct.ProductName)" />

                        <label for="productDescription">Description:</label>
                        <InputText name="productDescription" @bind-Value="selectedProduct .ProductDescription"></InputText>
                        <ValidationMessage For="@(() => selectedProduct.ProductDescription)" />

                        
                        <label for="additionalInformation">Additional Information:</label>
                        <InputText name="additionalInformation" @bind-Value="selectedProduct.AdditionalInformation"></InputText>
                        <ValidationMessage For="@(() => selectedProduct.AdditionalInformation)" />
                     
                        <label for="price">Price:</label>
                        <InputText name="price" @bind-Value="selectedProduct.ProductPrice"></InputText>
                        <ValidationMessage For="@(() => selectedProduct.ProductPrice)" />                   

                        <label for="auditComments">Comments:</label>
                        <InputText name="auditComments" @bind-Value="auditProduct.Comments"></InputText>
                        <ValidationMessage For="@(() => auditProduct.Comments)" />
              
                    <button type="submit" class="btn-primary">Save</button>
                    <button type="submit" class="btn-secondary" @onclick="HandleCancel">Cancel</button>

                </div>
        </EditForm>

答案1

得分: 1

以下是您要翻译的内容:

"comments" 列的验证,但验证未生效。似乎您还希望使用 DataAnnotationsValidator 组件验证 auditProduct 模型。您必须分别创建一个 DataAnnotationsValidator 组件,并将 auditProduct 模型传递给 EditForm 组件,以便为其添加验证。

<EditForm Model=@selectedProduct AuditModel=@auditProduct OnValidSubmit="HandleSubmit">
  <DataAnnotationsValidator />
  <DataAnnotationsValidator Model="auditProduct" />
  <!-- 其余表单字段 -->
</EditForm>
英文:

It appears that you are attempting to add validation to your audit table's "comments" column, however the validation is not operating. It appears that you also wish to validate the auditProduct model while using the DataAnnotationsValidator component to validate the selectedProduct model.

You must create a DataAnnotationsValidator component and give the auditProduct model to the EditForm component separately in order to add validation for it.

&lt;EditForm Model=@selectedProduct AuditModel=@auditProduct OnValidSubmit=&quot;HandleSubmit&quot;&gt;
  &lt;DataAnnotationsValidator /&gt;
  &lt;DataAnnotationsValidator Model=&quot;auditProduct&quot; /&gt;
  &lt;!-- rest of the form fields --&gt;
&lt;/EditForm&gt;

huangapple
  • 本文由 发表于 2023年2月8日 22:25:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387191.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定