1

I am new to this and created a custom attribute for string length validation on the server side. For example i have this

 [StringLength(5, MinimumLength = 2, ErrorMessageResourceName = "StringLength", ErrormessageResourceType = typeof(Resource))]
 public string LastName { get; set; }

and i want to create a custom attribute, so i can reduce the duplicates everytime i need to validate the string length

[MyStringLength(5)]
public string LastName { get; set; }

This is my view

@Html.TextBoxFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)

My code for custom attribute "MyStringLength"

 public class MyStringLengthAttribute : StringLengthAttribute
{

    public MyStringLengthAttribute(int maximumLength) : base(maximumLength)
    {         
        base.ErrorMessageResourceName = "StringLength";
        base.ErrorMessageResourceType = typeof(Resource);
    }
}

This helped me Modify default ErrorMessage for StringLength validation but after i post the form it just refreshes the page with the values and doesn't show any validation message

user8797814
  • 99
  • 1
  • 7
  • 1
    You should provide your code with your attempt instead of relating another question claiming it didn't work. Maybe is your way of implementing it that didn't work. – Guilherme Holtz Feb 12 '18 at 21:00
  • 1
    What is `MyStringLengthAttribute`? – Ron Beyer Feb 12 '18 at 21:22
  • @RonBeyer It is the name of the custom attribute i created. This is my code public class MyStringLengthAttribute : StringLengthAttribute { public MyStringLengthAttribute(int maximumLength) : base(maximumLength) { base.ErrorMessageResourceName = "StringLength"; base.ErrorMessageResourceType = typeof(Resource); } } – user8797814 Feb 12 '18 at 22:00
  • @Guilherme ty for taking the time. I edited my question to have the code of my custom attribute. – user8797814 Feb 12 '18 at 22:03
  • You need to register your attribute in `global.asax.cs` for it to be used. –  Feb 12 '18 at 22:30
  • You're missing the minimum length from your custom attribute, so you wouldn't see the error message if you were trying to trigger it with a 1 character string for example. – Nick Coad Feb 12 '18 at 22:54
  • @NickCoad ty. i am concerned as to why the validation message isn't showing up if i entered more than 5 characters. minimum length isn't a concern as of now. – user8797814 Feb 12 '18 at 22:59
  • @StephenMuecke That helped. Thank You. i also needed an adapter to provide client validation rules. – user8797814 Feb 13 '18 at 17:39

1 Answers1

1

I needed an adapter for client validation and register the custom attributes in my global.asax.cs. Please see Client-side validation for custom StringLength validation attribute

user8797814
  • 99
  • 1
  • 7
  • You do not need an adaptor - there is already one built in. Its just `DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(MyStringLength), typeof(StringLengthAttributeAdapter));`. And add the code in your answer, not a link –  Feb 13 '18 at 22:54
  • @StephenMuecke I don't know why i didn't think about it. Thank You. – user8797814 Feb 14 '18 at 00:01