Ejemplo n.º 1
0
        protected override void Validate(object fieldValue, bool metadataValue, FieldMetadataValidatorContext context)
        {
            // Null value is always invalid.
            if (fieldValue == null)
            {
                context.ResultBuilder.Add(
                    new TextValidationMessage(
                        context.FieldDefinition.Identifier,
                        context.FieldDefinition.Metadata.Get(
                            "Required.ErrorMessage",
                            String.Format("Missing value for required field '{0}'!", context.FieldDefinition.Identifier)
                            )
                        )
                    );
                return;
            }

            // For string values, test whether empty strings are allowed.
            string targetValue = fieldValue.ToString();

            if (!context.FieldDefinition.Metadata.Get("Required.AllowEmptyStrings", false) && String.IsNullOrEmpty(targetValue))
            {
                context.ResultBuilder.Add(
                    new TextValidationMessage(
                        context.FieldDefinition.Identifier,
                        context.FieldDefinition.Metadata.Get(
                            "Required.ErrorMessage",
                            String.Format("Missing value for required field '{0}'!", context.FieldDefinition.Identifier)
                            )
                        )
                    );
            }
        }
        protected override void Validate(object fieldValue, string metadataValue, FieldMetadataValidatorContext context)
        {
            string errorMessage = context.FieldDefinition.Metadata.Get(
                "MatchProperty.ErrorMessage",
                String.Format("'{0}' and '{1}' must match!", context.FieldDefinition.Identifier, metadataValue)
                );

            object otherProperty = null;

            if (context.Getter.TryGetValue(metadataValue, out otherProperty))
            {
                if ((fieldValue == null && otherProperty != null) || !fieldValue.Equals(otherProperty))
                {
                    context.ResultBuilder.AddTextMessage(context.FieldDefinition.Identifier, errorMessage);
                }
            }
            else
            {
                if (fieldValue != null)
                {
                    context.ResultBuilder.AddTextMessage(context.FieldDefinition.Identifier, errorMessage);
                }
            }
        }