1

我正在实现一个RequiredIf 验证属性,并且传递给 IsValid 方法的值始终为空。

必需的IfAttribute 类

    public class RequiredIfAttribute : ValidationAttribute
{
    private RequiredAttribute innerAttribute = new RequiredAttribute();
    public string DependentProperty { get; set; }
    public object TargetValue { get; set; }

    public RequiredIfAttribute(string dependentProperty, object targetValue)
    {
        this.DependentProperty = dependentProperty;
        this.TargetValue = targetValue;
    }

    public override bool IsValid(object value)
    {
        return innerAttribute.IsValid(value);
    }
}

视图模型

        [Required]
    [Display(Name = "Are You A Student?")]
    public bool? IsStudent { get; set; }

    [RequiredIf("IsStudent", true, ErrorMessage = "You must upload a photo of your student ID if you wish to register as a student.")]
    [Display(Name = "Student ID")]
    [FileExtensions("jpg|jpeg|png|pdf", ErrorMessage = "File is not in the correct format.")]
    [MaxFileSize(2 * 1024 * 1024, ErrorMessage = "You may not upload files larger than 2 MB.")]
    public HttpPostedFileBase StudentId { get; set; }

编辑器模板

    @model bool?
@using System.Web.Mvc;
@{
    var options = new List<SelectListItem>
    {
        new SelectListItem { Text = "Yes", Value = "true", Selected =  Model.HasValue && Model.Value },
        new SelectListItem { Text = "No", Value = "false", Selected =  Model.HasValue && Model.Value }
    };

    string defaultOption = null;

    if (ViewData.ModelMetadata.IsNullableValueType)
    {
        defaultOption = "(Select)";
    }
}

@Html.DropDownListFor(m => m, options, defaultOption)

每次提交表单时,都会抛出RequiredIf 错误消息,我感觉它与我最初描述的空值有关。我究竟做错了什么?谢谢!

注意:HTML 似乎可以正确呈现,所以我认为这不是问题。

    <select data-val="true" data-val-required="The Are You A Student? field is required." id="IsStudent" name="IsStudent"><option value="">(Select)</option>
<option value="true">Yes</option>
<option value="false">No</option>
</select>
4

2 回答 2

2

因为这是您的代码 -

public class RequiredIfAttribute : ValidationAttribute
{
    private RequiredAttribute innerAttribute = new RequiredAttribute();
    public string DependentProperty { get; set; }
    public object TargetValue { get; set; }

    public RequiredIfAttribute(string dependentProperty, object targetValue)
    {
        this.DependentProperty = dependentProperty;
        this.TargetValue = targetValue;
    }

    public override bool IsValid(object value)
    {
        return innerAttribute.IsValid(value);
    }
}

您正在使用RequriedAtrribute. 因此,要模拟它的行为,RequiredIf您必须实现一些逻辑来检查目标属性值是真还是假。但你并没有这样做,只是从内在属性返回。所以这只是一个RequiredRequiredIf-

public override bool IsValid(object value)
{
    return innerAttribute.IsValid(value);
}

修改此函数以进行一些检查,例如 -

public override bool IsValid(object value)
{
    //if the referred property is true then
        return innerAttribute.IsValid(value);
    //else
    return True
}
于 2014-06-03T17:17:57.843 回答
0

我使用以下代码:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public abstract class StefAttribute : ValidationAttribute
{
    public WDCIAttribute()
        : base()
    {
        this.ErrorMessageResourceType = typeof(GlobalResources);
    }
}


[AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = true)]
public class StefRequiredIfAttribute : StefAttribute
{
    private RequiredAttribute innerAttribute = new RequiredAttribute();
    public string DependentProperty { get; set; }
    public object TargetValue { get; set; }

    public WDCIRequiredIfAttribute()
    {
    }

    public WDCIRequiredIfAttribute(string dependentProperty, object targetValue)
        : base()
    {
        this.DependentProperty = dependentProperty;
        this.TargetValue = targetValue;
    }

    public override bool IsValid(object value)
    {
        return innerAttribute.IsValid(value);
    }
}

public class RequiredIfValidator : DataAnnotationsModelValidator<StefRequiredIfAttribute>
{
    public RequiredIfValidator(ModelMetadata metadata, ControllerContext context, StefRequiredIfAttribute attribute)
        : base(metadata, context, attribute)
    {
    }

    public override IEnumerable<ModelValidationResult> Validate(object container)
    {
        // get a reference to the property this validation depends upon
        var field = Metadata.ContainerType.GetProperty(Attribute.DependentProperty);

        if (field != null)
        {
            // get the value of the dependent property
            object value = field.GetValue(container, null);

            // compare the value against the target value
            if (IsEqual(value) || (value == null && Attribute.TargetValue == null))
            {
                // match => means we should try validating this field
                if (!Attribute.IsValid(Metadata.Model))
                {
                    // validation failed - return an error
                    yield return new ModelValidationResult { Message = ErrorMessage };
                }
            }
        }
    }

    private bool IsEqual(object dependentPropertyValue)
    {
        bool isEqual = false;

        if (Attribute.TargetValue != null && Attribute.TargetValue.GetType().IsArray)
        {
            foreach (object o in (Array)Attribute.TargetValue)
            {
                isEqual = o.Equals(dependentPropertyValue);
                if (isEqual)
                {
                    break;
                }
            }
        }
        else
        {
            if (Attribute.TargetValue != null)
            {
                isEqual = Attribute.TargetValue.Equals(dependentPropertyValue);
            }
        }

        return isEqual;
    }
}

可以在模型中使用如下:

public class PersonnelVM : EntityVM
{
    // . . .

    [DisplayName("Name")]
    [StefRequiredIf("IndividualOrBulk", PersonnelType.Bulk, ErrorMessageResourceName = GlobalResourceLiterals.Name_Required)]
    public string Name { get; set; }

    [DisplayName("PersonnelType")]
    public PersonnelType IndividualOrBulk { get; set; }

    // . . .
}
于 2014-06-03T17:28:08.550 回答