asked on
VB .NET Codes:
------------------
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.ComponentModel.DataAnnotations
Imports site.Helpers
Namespace site.Helpers
Public Class RequiredIfValidator
Inherits DataAnnotationsModelValidator(Of RequiredIfAttribute)
Public Sub New(ByVal metaData As ModelMetadata, ByVal context As ControllerContext, ByVal attribute As RequiredIfAttribute)
MyBase.New(metaData, context, attribute)
End Sub
Public Overrides Function GetClientValidationRules() As IEnumerable(Of ModelClientValidationRule)
Return New ModelClientValidationRule()
End Function
Public Overrides Function Validate(ByVal container As Object) As IEnumerable(Of ModelValidationResult)
Dim field = Metadata.ContainerType.GetProperty(Attribute.DependentProperty)
'= Metadata.ContainerType.GetProperty(Attribute.DependentProperty
If field <> Nothing Then
Dim value As Object = field.GetValue(container, Nothing)
If value <> Nothing And Attribute.TargetValue = Nothing Or (value.Equals(Attribute.TargetValue)) Then
If Not Attribute.IsValid(Metadata.Model) Then
'how to translate the below C# .NET line in VB .NET?
' yield return new ModelValidationResult { Message = ErrorMessage };
End If
End If
End If
Return container
End Function
End Class
End Namespace
VB .NET Codes
-----------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace ConditionalValidation.Validation
{
public class RequiredIfAttribute : ValidationAttribute
{
// Note: we don't inherit from RequiredAttribute as some elements of the MVC
// framework specifically look for it and choose not to add a RequiredValidator
// for non-nullable fields if one is found. This would be invalid if we inherited
// from it as obviously our RequiredIf only applies if a condition is satisfied.
// Therefore we're using a private instance of one just so we can reuse the IsValid
// logic, and don't need to rewrite it.
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);
}
}
}
----------------------------------------
ViewModel in VB .NET
<Required(ErrorMessage:="Specify first Name.")> _
Public Property firstName() As String
Get
Return m_firstName
End Get
Set(ByVal value As String)
m_firstName = value
End Set
End Property
Private m_firstName As String
<RequiredIf("firstName", "Anna")> _
Public Property lastName() As String
Get
Return m_lastName
End Get
Set(ByVal value As String)
m_lastName = value
End Set
End Property
Private m_lastName As String
// So if the last name is Anna, first name is required
------------------------------------------------
The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications
TRUSTED BY
ASKER