2

I've wrote a Custom Validation for Guid Type Like this:

 public class ValidGuidAttribute : ValidationAttribute
{

    public override bool IsValid(object value)
    {
        string msg = "please select a device";
        ErrorMessage = msg;
        var input = Convert.ToString(value);
        if (string.IsNullOrEmpty(input) )
        {

            return false;
        }

        Guid guid;
        if(!Guid.TryParse(input,out guid))
        {
            return false;
        }
        if (guid == Guid.Empty)
        {
            return false;
        }
        return true;
    }
}

and when I inspect element on the browser I have this :

<span class="field-validation-valid" data-valmsg-for="DeviceGroupId" data-valmsg-replace="true"></span>

and when I don't fill that filed and post form , I've got this error Message :

The value 'please select a device' is not valid for device group.

I want just get this :

please select a device

and In ViewModel I have :

        [DisplayName("device group"),ValidGuid(ErrorMessage = "please enter")]
    public Guid DeviceGroupId { get; set; }

2 Answers 2

1

It seems that custom validation has some problem with the type Guid, when I change the type to string, your current implementation works:

enter image description here

It's weird, in that validation for the validity of Guid type is performed automatically, because when I remove the custom validation attibute and check for validity, I get the same error you get:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

this is default validation message which i think mvc is validating before checking ValidGuid and to override this message you can try

 public class ValidGuidAttribute : ValidationAttribute
    {
        public override bool IsValid(object value)
        {           
            ErrorMessage = "please select a device";
            var input = Convert.ToString(value);
            if (string.IsNullOrEmpty(input))
            {

                return false;
            }

            Guid guid;
            if (!Guid.TryParse(input, out guid))
            {
                return false;
            }
            if (guid == Guid.Empty)
            {
                return false;
            }
            return true;
        }

        public class ValidGuid : DataAnnotationsModelValidator<ValidGuidAttribute>
        {
            public ValidGuid(ModelMetadata metadata, ControllerContext context, ValidGuidAttribute attribute)
                : base(metadata, context, attribute)
            {
                if (!attribute.IsValid(context.HttpContext.Request.Form[metadata.PropertyName]))
                {

                        var propertyName = metadata.PropertyName;


                 if (context.Controller.ViewData.ModelState[propertyName] != null)
                    {
                           context.Controller.ViewData.ModelState[propertyName].Errors.Clear();
                            context.Controller.ViewData.ModelState[propertyName].Errors.Add(attribute.ErrorMessage);
                        }

                }
            }
        }
    }

and add this line to your Global.asax.cs file

DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(ValidGuidAttribute), typeof(ValidGuidAttribute.ValidGuid));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.