In MVC 5, How do I remove the required attribute depending on another dropdown value?












1















Currently, the form is working as intended until you submit the form with the
Usage_Status dropdown selection with value a of '2' and then switch to any other value and submit again. The javascript validation message 'The Assigned User field is required.' appears and does not go away after switching values on the dropdown and submitting again. Is it possible to remove the required attribute, or set it on the condition that '2' is selected?



HTML:



<div class="row">
<div class="form-group col-sm-4">
@Html.LabelFor(model => model.Usage_Status, new { @class = "control-label col-md-12" })
<div class="col-md-10">
@Html.DropDownList("Usage_Status", new SelectList(statuses, "ID", "Name"), new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Usage_Status)
</div>
</div>

<div class="form-group col-sm-4">
@Html.LabelFor(model => model.Assigned_User, new { @class = "control-label col-md-12" })
<div class="col-md-10">
@Html.EditorFor(model => model.Assigned_User, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Assigned_User)
</div>
</div>

<div class="form-group col-sm-4">
@Html.LabelFor(model => model.Department, new { @class = "control-label col-md-12" })
<div class="col-md-10">
@Html.DropDownList("Department", new SelectList(departments, "ID", "Name"), string.Empty, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Department)
</div>
</div>
</div>


JavaScript:



$(document).ready(function () {
$('#Usage_Status').change(function (e) {
//If item is being used, require a user and department. Otherwise, disabled those fields.
if ($('#Usage_Status').val() == "2") {
$('#Assigned_User').rules('add', 'required');
$('#Assigned_User').removeAttr('disabled');
} else {
$('#Assigned_User').rules('remove', 'required');
$('#Assigned_User').removeAttr('required');
$('#Assigned_User').attr('disabled', 'disabled');
}

});









share|improve this question























  • Is it marked as required in your model?

    – Sean T
    Nov 28 '18 at 17:10











  • Yes, it is marked in the model.

    – sjohn285
    Nov 28 '18 at 17:16











  • So when you submit it, the server side will bounce it too. client & server side validation are separate processes.

    – Sean T
    Nov 28 '18 at 17:18











  • I just tried removing the attribute in the model, and it has the same issue.

    – sjohn285
    Nov 28 '18 at 17:18
















1















Currently, the form is working as intended until you submit the form with the
Usage_Status dropdown selection with value a of '2' and then switch to any other value and submit again. The javascript validation message 'The Assigned User field is required.' appears and does not go away after switching values on the dropdown and submitting again. Is it possible to remove the required attribute, or set it on the condition that '2' is selected?



HTML:



<div class="row">
<div class="form-group col-sm-4">
@Html.LabelFor(model => model.Usage_Status, new { @class = "control-label col-md-12" })
<div class="col-md-10">
@Html.DropDownList("Usage_Status", new SelectList(statuses, "ID", "Name"), new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Usage_Status)
</div>
</div>

<div class="form-group col-sm-4">
@Html.LabelFor(model => model.Assigned_User, new { @class = "control-label col-md-12" })
<div class="col-md-10">
@Html.EditorFor(model => model.Assigned_User, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Assigned_User)
</div>
</div>

<div class="form-group col-sm-4">
@Html.LabelFor(model => model.Department, new { @class = "control-label col-md-12" })
<div class="col-md-10">
@Html.DropDownList("Department", new SelectList(departments, "ID", "Name"), string.Empty, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Department)
</div>
</div>
</div>


JavaScript:



$(document).ready(function () {
$('#Usage_Status').change(function (e) {
//If item is being used, require a user and department. Otherwise, disabled those fields.
if ($('#Usage_Status').val() == "2") {
$('#Assigned_User').rules('add', 'required');
$('#Assigned_User').removeAttr('disabled');
} else {
$('#Assigned_User').rules('remove', 'required');
$('#Assigned_User').removeAttr('required');
$('#Assigned_User').attr('disabled', 'disabled');
}

});









share|improve this question























  • Is it marked as required in your model?

    – Sean T
    Nov 28 '18 at 17:10











  • Yes, it is marked in the model.

    – sjohn285
    Nov 28 '18 at 17:16











  • So when you submit it, the server side will bounce it too. client & server side validation are separate processes.

    – Sean T
    Nov 28 '18 at 17:18











  • I just tried removing the attribute in the model, and it has the same issue.

    – sjohn285
    Nov 28 '18 at 17:18














1












1








1








Currently, the form is working as intended until you submit the form with the
Usage_Status dropdown selection with value a of '2' and then switch to any other value and submit again. The javascript validation message 'The Assigned User field is required.' appears and does not go away after switching values on the dropdown and submitting again. Is it possible to remove the required attribute, or set it on the condition that '2' is selected?



HTML:



<div class="row">
<div class="form-group col-sm-4">
@Html.LabelFor(model => model.Usage_Status, new { @class = "control-label col-md-12" })
<div class="col-md-10">
@Html.DropDownList("Usage_Status", new SelectList(statuses, "ID", "Name"), new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Usage_Status)
</div>
</div>

<div class="form-group col-sm-4">
@Html.LabelFor(model => model.Assigned_User, new { @class = "control-label col-md-12" })
<div class="col-md-10">
@Html.EditorFor(model => model.Assigned_User, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Assigned_User)
</div>
</div>

<div class="form-group col-sm-4">
@Html.LabelFor(model => model.Department, new { @class = "control-label col-md-12" })
<div class="col-md-10">
@Html.DropDownList("Department", new SelectList(departments, "ID", "Name"), string.Empty, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Department)
</div>
</div>
</div>


JavaScript:



$(document).ready(function () {
$('#Usage_Status').change(function (e) {
//If item is being used, require a user and department. Otherwise, disabled those fields.
if ($('#Usage_Status').val() == "2") {
$('#Assigned_User').rules('add', 'required');
$('#Assigned_User').removeAttr('disabled');
} else {
$('#Assigned_User').rules('remove', 'required');
$('#Assigned_User').removeAttr('required');
$('#Assigned_User').attr('disabled', 'disabled');
}

});









share|improve this question














Currently, the form is working as intended until you submit the form with the
Usage_Status dropdown selection with value a of '2' and then switch to any other value and submit again. The javascript validation message 'The Assigned User field is required.' appears and does not go away after switching values on the dropdown and submitting again. Is it possible to remove the required attribute, or set it on the condition that '2' is selected?



HTML:



<div class="row">
<div class="form-group col-sm-4">
@Html.LabelFor(model => model.Usage_Status, new { @class = "control-label col-md-12" })
<div class="col-md-10">
@Html.DropDownList("Usage_Status", new SelectList(statuses, "ID", "Name"), new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Usage_Status)
</div>
</div>

<div class="form-group col-sm-4">
@Html.LabelFor(model => model.Assigned_User, new { @class = "control-label col-md-12" })
<div class="col-md-10">
@Html.EditorFor(model => model.Assigned_User, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Assigned_User)
</div>
</div>

<div class="form-group col-sm-4">
@Html.LabelFor(model => model.Department, new { @class = "control-label col-md-12" })
<div class="col-md-10">
@Html.DropDownList("Department", new SelectList(departments, "ID", "Name"), string.Empty, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Department)
</div>
</div>
</div>


JavaScript:



$(document).ready(function () {
$('#Usage_Status').change(function (e) {
//If item is being used, require a user and department. Otherwise, disabled those fields.
if ($('#Usage_Status').val() == "2") {
$('#Assigned_User').rules('add', 'required');
$('#Assigned_User').removeAttr('disabled');
} else {
$('#Assigned_User').rules('remove', 'required');
$('#Assigned_User').removeAttr('required');
$('#Assigned_User').attr('disabled', 'disabled');
}

});






jquery asp.net-mvc data-annotations






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 28 '18 at 17:01









sjohn285sjohn285

12211




12211













  • Is it marked as required in your model?

    – Sean T
    Nov 28 '18 at 17:10











  • Yes, it is marked in the model.

    – sjohn285
    Nov 28 '18 at 17:16











  • So when you submit it, the server side will bounce it too. client & server side validation are separate processes.

    – Sean T
    Nov 28 '18 at 17:18











  • I just tried removing the attribute in the model, and it has the same issue.

    – sjohn285
    Nov 28 '18 at 17:18



















  • Is it marked as required in your model?

    – Sean T
    Nov 28 '18 at 17:10











  • Yes, it is marked in the model.

    – sjohn285
    Nov 28 '18 at 17:16











  • So when you submit it, the server side will bounce it too. client & server side validation are separate processes.

    – Sean T
    Nov 28 '18 at 17:18











  • I just tried removing the attribute in the model, and it has the same issue.

    – sjohn285
    Nov 28 '18 at 17:18

















Is it marked as required in your model?

– Sean T
Nov 28 '18 at 17:10





Is it marked as required in your model?

– Sean T
Nov 28 '18 at 17:10













Yes, it is marked in the model.

– sjohn285
Nov 28 '18 at 17:16





Yes, it is marked in the model.

– sjohn285
Nov 28 '18 at 17:16













So when you submit it, the server side will bounce it too. client & server side validation are separate processes.

– Sean T
Nov 28 '18 at 17:18





So when you submit it, the server side will bounce it too. client & server side validation are separate processes.

– Sean T
Nov 28 '18 at 17:18













I just tried removing the attribute in the model, and it has the same issue.

– sjohn285
Nov 28 '18 at 17:18





I just tried removing the attribute in the model, and it has the same issue.

– sjohn285
Nov 28 '18 at 17:18












1 Answer
1






active

oldest

votes


















1














If you're doing conditional validation, e.g one field's value in your form defines validation for another field you need an additional library. I use expressive annotations






share|improve this answer
























  • I should probably make another question for this, but I'm hoping you can help me quickly. Is this what I would put into the model? [RequiredIf("Usage_Status == '2'")] [Display(Name = "Assigned User")] public string Assigned_User { get; set; }

    – sjohn285
    Nov 28 '18 at 17:40













  • @sjohn285 Yep, just pass the conditions in as a string

    – Sean T
    Nov 29 '18 at 9:37











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53524569%2fin-mvc-5-how-do-i-remove-the-required-attribute-depending-on-another-dropdown-v%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














If you're doing conditional validation, e.g one field's value in your form defines validation for another field you need an additional library. I use expressive annotations






share|improve this answer
























  • I should probably make another question for this, but I'm hoping you can help me quickly. Is this what I would put into the model? [RequiredIf("Usage_Status == '2'")] [Display(Name = "Assigned User")] public string Assigned_User { get; set; }

    – sjohn285
    Nov 28 '18 at 17:40













  • @sjohn285 Yep, just pass the conditions in as a string

    – Sean T
    Nov 29 '18 at 9:37
















1














If you're doing conditional validation, e.g one field's value in your form defines validation for another field you need an additional library. I use expressive annotations






share|improve this answer
























  • I should probably make another question for this, but I'm hoping you can help me quickly. Is this what I would put into the model? [RequiredIf("Usage_Status == '2'")] [Display(Name = "Assigned User")] public string Assigned_User { get; set; }

    – sjohn285
    Nov 28 '18 at 17:40













  • @sjohn285 Yep, just pass the conditions in as a string

    – Sean T
    Nov 29 '18 at 9:37














1












1








1







If you're doing conditional validation, e.g one field's value in your form defines validation for another field you need an additional library. I use expressive annotations






share|improve this answer













If you're doing conditional validation, e.g one field's value in your form defines validation for another field you need an additional library. I use expressive annotations







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 28 '18 at 17:19









Sean TSean T

1,356715




1,356715













  • I should probably make another question for this, but I'm hoping you can help me quickly. Is this what I would put into the model? [RequiredIf("Usage_Status == '2'")] [Display(Name = "Assigned User")] public string Assigned_User { get; set; }

    – sjohn285
    Nov 28 '18 at 17:40













  • @sjohn285 Yep, just pass the conditions in as a string

    – Sean T
    Nov 29 '18 at 9:37



















  • I should probably make another question for this, but I'm hoping you can help me quickly. Is this what I would put into the model? [RequiredIf("Usage_Status == '2'")] [Display(Name = "Assigned User")] public string Assigned_User { get; set; }

    – sjohn285
    Nov 28 '18 at 17:40













  • @sjohn285 Yep, just pass the conditions in as a string

    – Sean T
    Nov 29 '18 at 9:37

















I should probably make another question for this, but I'm hoping you can help me quickly. Is this what I would put into the model? [RequiredIf("Usage_Status == '2'")] [Display(Name = "Assigned User")] public string Assigned_User { get; set; }

– sjohn285
Nov 28 '18 at 17:40







I should probably make another question for this, but I'm hoping you can help me quickly. Is this what I would put into the model? [RequiredIf("Usage_Status == '2'")] [Display(Name = "Assigned User")] public string Assigned_User { get; set; }

– sjohn285
Nov 28 '18 at 17:40















@sjohn285 Yep, just pass the conditions in as a string

– Sean T
Nov 29 '18 at 9:37





@sjohn285 Yep, just pass the conditions in as a string

– Sean T
Nov 29 '18 at 9:37




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53524569%2fin-mvc-5-how-do-i-remove-the-required-attribute-depending-on-another-dropdown-v%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)