c# - MVC RegEx Triggers Validation Before page Load -
my applicationcontroller
:
public class applicationcontroller : controller { private iuxrepository _repository; public applicationcontroller(iuxrepository repository) { _repository = repository; } public actionresult create() { return view("shape", new applicationviewmodel()); } }
my applicationviewmodel
public class applicationviewmodel : viewmodelbase { public applicationviewmodel() { application = new application(); } public application application {get;set;} }
my application
model:
public class application : dbentity { public string name {get;set;} [display(name = "proposed release date"), regularexpression(@"(^q[1-4])\s(2\d{3})", errormessage = "date needs in format q{1-4}{space}20{yy} e.g. q4 2013 or q1 2014")] public string proposedreleasedate {get;set;} }
extract shape
view:
<div class="editor-label"> @html.displayfor(model => model.proposedreleasedate) </div> <div class="editor-field"> @html.editorfor(model => model.proposedreleasedate) @html.validationmessagefor(model => model.proposedreleasedate) </div>
when try , load shape
view, reason comes validation error saying proposedreleasedate
doesn't meet required regularexpression
.
of course doesn't because it's new entity waiting input, why validating before page has loaded up. validating early. how bypass this, or turn feature off, counter intuitive/productive imho.
the errormessage format not correct. instead of
errormessage = "date needs in format q{1-4}{space}20{yy} e.g. q4 2013 or q1 2014"
make it
errormessage = "date needs in format q{{1-4}}{{space}}20{{yy}} e.g. q4 2013 or q1 2014"
note "{" "{{" , "}" "}}"
edit: related link how escape braces (curly brackets) in format string in .net
Comments
Post a Comment