3

Can somebody tell me why "This field is required" and "Please insert database name" are being displayed instead of just "Please insert database name"?

Screenshot

This is my model :

public class InstallViewModel
    {
        [Required(AllowEmptyStrings = false, ErrorMessage = "Please insert database name")]
        public string DatabaseName { get; set; }

and this is my view :

<div class="input-group">
    <span class="input-group-addon">Database</span>
    @Html.TextBoxFor(w => w.DatabaseName, new { @class = "form-control", placeholder = "Database name" })
</div>
@Html.ValidationMessageFor(w=> w.DatabaseName)

Thank you.

EDIT:

Can you see the image attached ? I have some problems uploading images.

The view is a partial view and this is the whole partial view:

@Html.ValidationMessageFor(w => w.DatabaseName)
    <div class="input-group">
        <span class="input-group-addon">Database</span>
        @Html.TextBoxFor(w => w.DatabaseName, new { @class = "form-control", placeholder = "Database name" })
    </div> 
    <br />
    @Html.CheckBoxFor(w => w.UseWindowsAuthentication, new { @checked = "checked" }) Use Windows Authentication<br /><br />
    <div class="wizard-sqlauth" style="display: none">
        <div class="input-group">
            <span class="input-group-addon">User name</span>
            @Html.TextBoxFor(w => w.UserName, new { @class = "form-control", placeholder = "User name" })
        </div>
        @Html.ValidationMessageFor(w => w.UserName)<br />
        <div class="input-group">
            <span class="input-group-addon">Password</span>
            @Html.PasswordFor(w => w.Password, new { @class = "form-control" })
        </div>
        @Html.ValidationMessageFor(w => w.Password)
    </div>
Branislav B.
  • 509
  • 7
  • 21

4 Answers4

2

DatabaseName is "Required" and your input is empty. (There is only placeholder text)

Krahu
  • 183
  • 7
1

Are you calling jquery validation "manually" anywhere in javascript, i.e.

$('#myform').valid() ?

That would trigger the default value for the required rule ("This field is required."), and would append it as a label after the input, which is exactly the behavior your are experiencing.

If you really need to use both (MVC's Unobstrusive validation + jQuery validation) you can configure jquery validation to ignore certain fields, for example

$('#myform').validate({
  ignore: '#databasefieldId'
});
Rui
  • 4,847
  • 3
  • 29
  • 35
  • Can you tell me how to disable the default value ? – Branislav B. Feb 21 '14 at 10:30
  • @BranislavB. You probably should only be using one form of validation, why did you need to use jquery validation manually? – Rui Feb 21 '14 at 10:34
  • Because my viewmodel is displayed in multiple partial views in form of Wizard. I need to validate each page of the wizard so the user can click next button. Therefore i need to call validation on each element in each view. : function wizard_ValidateDataConnection() { var result = $('#wizard-step-database').validate().element('#DatabaseName') && $('#wizard-step-database').validate().element('#UserName') && $('#wizard-step-database').validate().element('#Password'); return result; } – Branislav B. Feb 21 '14 at 10:50
  • @BranislavB. What I would try then is to override the showErrors jquery validation option, i.e. `$('#myForm').validation({showErrors: function(){}});` so that it does nothing. This way you'd only get the MVC Unobtrusive validation errors. I've never actually tried this, let me know if it works and I'll update the answer with it. – Rui Feb 21 '14 at 11:00
  • I've added a blank function() {} as showErrors and now it is not showing anything – Branislav B. Feb 21 '14 at 11:40
  • @BranislavB. Well, you can try hidding the label that jquery validation adds [**with css**](http://stackoverflow.com/questions/3922628/jquery-validation-call-valid-without-displaying-errors), or you can try setting the errorLabelContainer option in validate to an element that doesn't exists (but this might break mvc validation as well). Give it a try – Rui Feb 21 '14 at 11:47
-1

You have applied the RequiredAttribute attribute to a property to the property DatabaseName which implies that the property must contain a value.

A validation exception is raised if the property is null, an empty string (""), or contains only white-space characters.

Cris
  • 12,799
  • 5
  • 35
  • 50
  • 1
    Isn't the question about the seemingly wrong error message? The OP set ErrorMessage to "Please insert database name", why ins't that shown instead? – Rui Feb 21 '14 at 10:06
-2

You just add @Html.ValidationMessageFor(w=> w.DatabaseName) in the top of div. This will show the summary.

Nagaraj S
  • 13,316
  • 6
  • 32
  • 53
Sathish
  • 159
  • 1
  • 4
  • 18