This is a migrated thread and some comments may be shown as answers.

[Solved] Register Custom ValidationAttribute on clientside

2 Answers 68 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
JULIA
Top achievements
Rank 1
JULIA asked on 07 Sep 2010, 10:54 AM
Hello,

i have a custom ValidationAttribute:
public sealed class DateCompareAttribute : ValidationAttribute
{
    private const string DefaultErrorMessage = "...";
  
    public DateCompareAttribute( string loweredOrEqualDateTimeProperty, bool includeEqual ) : base( DefaultErrorMessage )
    {
        LoweredOrEqualDateTimeProperty = loweredOrEqualDateTimeProperty;
        IncludeEqual = includeEqual;
    }
    // ...
}

I register the validator on clientside:
<script type="text/javascript">
    Sys.Mvc.ValidatorRegistry.validators.dateCompare = function (rule) {
        var p = rule.ValidationParameters.loweredOrEqualDateTimeProperty;
        var ie = rule.ValidationParameters.includeEqual;
        return function (value, context) {
            return false;
        };
    };
</script>

The validation on clientside will never run. in a "normal" form without a grid everything works fine.
Which is the way to run this validation inside the grid? Extend jquery.validate?

Regards,
Timo

2 Answers, 1 is accepted

Sort by
0
JULIA
Top achievements
Rank 1
answered on 08 Sep 2010, 07:03 AM
Hello,

i have a custom DataAnnotationsModelValidator.
public class RequiredIfValidator : DataAnnotationsModelValidator<RequiredIfAttribute>
{
    public RequiredIfValidator( ModelMetadata metadata, ControllerContext context, RequiredIfAttribute attribute )
            : base( metadata, context, attribute ) {}
  
    public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
    {
        return base.GetClientValidationRules();
    }
  
    public override IEnumerable<ModelValidationResult> Validate( object container )
    {
        var field = Metadata.ContainerType.GetProperty( Attribute.DependentProperty );
  
        if( field != null )
        {
            var value = field.GetValue( container, null );
            if( ( value == null && Attribute.TargetValue == null ) || ( value.Equals( Attribute.TargetValue ) ) )
            {
                if( !Attribute.IsValid( Metadata.Model ) )
                    yield return new ModelValidationResult { Message = ErrorMessage };
            }
        }
    }
}

Metadata.Model is always null. But in a "normal" form it works. Is this an issue with the grid?
0
Hristo Germanov
Telerik team
answered on 10 Sep 2010, 02:31 PM
Hello Timo,

Thank you for contacting us.

I have attached a simple project showing how to validate HTML input elements on client-side with jQuery. (Use haacked project as a base).

The problem is that the grid does not render initially input HTML elements (which is expected - grid is not in edit mode) and ASP.NET MVC framework will not serialize validation rules. Hence grid, which binds on client-side, will not post page when goes in edit mode. Thus validation rules will never be serialized.

I am glad to inform you that we were able to overcome this issue and the fix is included to our latest internal build. For your convenience it is attached along with the test project.

Regards,

Hristo Germanov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
JULIA
Top achievements
Rank 1
Answers by
JULIA
Top achievements
Rank 1
Hristo Germanov
Telerik team
Share this question
or