仅在字段可见时才需要验证

时间:2022-12-01 08:21:01

I am using the [Required] attribute for the client-side validation in ASP.NET MVC 3.

我在ASP.NET MVC 3中使用[Required]属性进行客户端验证。

The class looks as:

该课程看起来像:

public class User
{
    [Required(ErrorMessage = "First Name is required")]
    public string FirstName { get; set; }
}

I want the field FirstName to be validated only if it's visible, which will be shown only on certain conditions. How can I do that?

我希望仅在可见的情况下验证字段FirstName,该字段仅在某些条件下显示。我怎样才能做到这一点?

I have used the following, but still it looks to validate for the required field of that hidden field.

我使用了以下内容,但它仍然希望验证该隐藏字段的必填字段。

$('#registerForm').validate({ ignore: ":not(:visible)" });

4 个解决方案

#1


14  

With some useful hints from @Josiah, i am able to get to my requirement.

通过@Josiah的一些有用提示,我能够达到我的要求。

Add the RequiredIfAttribute class and the required javascript. Refer Conditional Validation in ASP.NET MVC 3

添加RequiredIfAttribute类和所需的javascript。请参阅ASP.NET MVC 3中的条件验证

And in the class add the RequiredIf attribute as:

并在类中添加RequiredIf属性:

public class User
{
[RequiredIf("hFirstName", "true", ErrorMessage = "First Name is required")]
public string FirstName { get; set; }

and in aspx:

在aspx中:

@Html.TextBoxFor(model => Model.FirstName, new { @style = "height:auto;" })
@Html.ValidationMessageFor(model => Model.FirstName)
@Html.Hidden("hFirstName")

Set the value of hFirstName to 'true' if the FirstName field is hidden and 'false', if visible.

如果隐藏了FirstName字段,则将hFirstName的值设置为“true”,如果可见,则将其设置为“false”。

The magic works with these changes. Thanks to @Josiah Ruddell for his answer

神奇适用于这些变化。感谢@Josiah Ruddell的回答

#2


7  

I would create a conditionally required attribute. There is a good article on creating one with jQuery validation here.

我会创建一个有条件的必需属性。这里有一篇关于使用jQuery验证创建一篇文章的好文章。

Another option: you could reference a project like Foolproof validation (codeplex) that provides this functionality and the client scripts.

另一种选择:您可以引用提供此功能和客户端脚本的项目,如Foolproof validation(codeplex)。

Additionally you could utilize ajax to load your partial views so that they are never on the page when hidden. This would avoid conditional validation altogether.

此外,您可以使用ajax加载部分视图,以便在隐藏时它们永远不会出现在页面上。这将完全避免条件验证。

#3


1  

Try my custom validation attribute:

尝试我的自定义验证属性:

[ConditionalRequired("hFirstName==true")]
public string FirstName {get, set};

It supports multiple conditions.

它支持多种条件。

#4


0  

A bit late,

有点晚了,

namespace System.ComponentModel.DataAnnotations
{
    public class RequiredIfVisibleAttribute : RequiredAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext context)
        {
            if (HttpContext.Current.Request.Form.AllKeys.Contains(context.MemberName))
                return base.IsValid(value, context);

            return ValidationResult.Success;
        }
    }
}

But here's my solution.

但这是我的解决方案。

Just an inheritance of Required that will act the same way, except that it will only activate if the field if included in the posted keys.

只是必需的继承将以相同的方式起作用,除非它仅在字段包含在已发布的键中时才会激活。

#1


14  

With some useful hints from @Josiah, i am able to get to my requirement.

通过@Josiah的一些有用提示,我能够达到我的要求。

Add the RequiredIfAttribute class and the required javascript. Refer Conditional Validation in ASP.NET MVC 3

添加RequiredIfAttribute类和所需的javascript。请参阅ASP.NET MVC 3中的条件验证

And in the class add the RequiredIf attribute as:

并在类中添加RequiredIf属性:

public class User
{
[RequiredIf("hFirstName", "true", ErrorMessage = "First Name is required")]
public string FirstName { get; set; }

and in aspx:

在aspx中:

@Html.TextBoxFor(model => Model.FirstName, new { @style = "height:auto;" })
@Html.ValidationMessageFor(model => Model.FirstName)
@Html.Hidden("hFirstName")

Set the value of hFirstName to 'true' if the FirstName field is hidden and 'false', if visible.

如果隐藏了FirstName字段,则将hFirstName的值设置为“true”,如果可见,则将其设置为“false”。

The magic works with these changes. Thanks to @Josiah Ruddell for his answer

神奇适用于这些变化。感谢@Josiah Ruddell的回答

#2


7  

I would create a conditionally required attribute. There is a good article on creating one with jQuery validation here.

我会创建一个有条件的必需属性。这里有一篇关于使用jQuery验证创建一篇文章的好文章。

Another option: you could reference a project like Foolproof validation (codeplex) that provides this functionality and the client scripts.

另一种选择:您可以引用提供此功能和客户端脚本的项目,如Foolproof validation(codeplex)。

Additionally you could utilize ajax to load your partial views so that they are never on the page when hidden. This would avoid conditional validation altogether.

此外,您可以使用ajax加载部分视图,以便在隐藏时它们永远不会出现在页面上。这将完全避免条件验证。

#3


1  

Try my custom validation attribute:

尝试我的自定义验证属性:

[ConditionalRequired("hFirstName==true")]
public string FirstName {get, set};

It supports multiple conditions.

它支持多种条件。

#4


0  

A bit late,

有点晚了,

namespace System.ComponentModel.DataAnnotations
{
    public class RequiredIfVisibleAttribute : RequiredAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext context)
        {
            if (HttpContext.Current.Request.Form.AllKeys.Contains(context.MemberName))
                return base.IsValid(value, context);

            return ValidationResult.Success;
        }
    }
}

But here's my solution.

但这是我的解决方案。

Just an inheritance of Required that will act the same way, except that it will only activate if the field if included in the posted keys.

只是必需的继承将以相同的方式起作用,除非它仅在字段包含在已发布的键中时才会激活。