c# - Give RequiredAttribute a custom message -


i have required message , want give custom message because default 1 not great. message must come resource file , parametrized, message in resource file like

"the {0} field required." 

i need able replace {0} supply. know default message similar problem end things

"the requiredreason field required." 

when want is

"the required reason field required". 

any idea how can done?

you make own requiredattribute, , override formaterrormessage method use parameter name or pass in parameters want format class through constructor.

something like:

public class parameterisedrequiredattribute : requiredattribute     {         private string[] _replacements { get; set; }          public parameterisedrequiredattribute(params string[] replacements)         {             _replacements = replacements;              errormessageresourcename = errormessageserrors.specificfieldrequired;             errormessageresourcetype = typeof(errormessages);         }          public override string formaterrormessage(string name)         {             return string.format(errormessagestring, (object[])_replacements);         }     } 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -