So, I have a form that works well with MVC 3, DataAnnotations, and Unobtrusive javascript. However, I want to put watermarks in the input fields, so that, for example, the Name text box is filled with the Name value by default. When the user clicks on it, the value disappears, and if they leave the field without entering anything, "Name" appears again. In addition, it is implemented for me and works well.
My question is related to the [Required] attribute in the FirstName property of my view model. If the user submits the form, by default this field has a "Name" in it, so it passes the "Required" check.
What is the best way to handle this ... I am thinking of several options:
1) Put some jQuery on fire before an unobtrusive JS check that clears watermarks so that when the check fires, those fields that have default values ββin them are empty. I am not sure if this is possible. Can you somehow introduce a function before unobtrusive JS checks?
2) Change the [Required] attribute or create a new one to accept the default value, and then compare it to see if they match. This causes several problems, since now I have the default value specified in several places: in the user interface and in the code, and this is wrong.
3) Create a new attribute "Watermark", which I decorate with a property that indicates the default value for this field. Create a new HTML helper (instead of TextBoxFor) that searches for this attribute and returns the appropriate attributes to the tag. Change or create a new [Required] attribute that looks for [Watermark] in the same field. This saves the default value in one place and supports the principles of DRY, but it seems to me that I put the UI elements in the code (the watermarks are purely visual) and it also seems like an overly complex solution for what should be simple a problem.
Any thoughts?
Scott source share