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>
6
  • 1
    You are asking this because you are seeing "This field is required" instead of "Please insert database name", right? All the answers seem to have missed this Commented Feb 21, 2014 at 10:05
  • Yes, that's exactly what I'm asking. Commented Feb 21, 2014 at 10:14
  • 1
    Can you show us the rest of your View? Commented Feb 21, 2014 at 10:16
  • I've edited the question and included the whole partial view. Commented Feb 21, 2014 at 10:18
  • Do you really use InstallViewModel in View? Maybe you pass InstallModel to the partial view. Commented Feb 21, 2014 at 10:22

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, the problem is that two messages are showing. One default and one defined in ErrorMessage.
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'
});

6 Comments

Can you tell me how to disable the default value ?
@BranislavB. You probably should only be using one form of validation, why did you need to use jquery validation manually?
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; }
@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.
I've added a blank function() {} as showErrors and now it is not showing anything
|
-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.

1 Comment

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?
-2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.