1

In this thread I got the advice to use FoolProof to solve my problem. Thats what Im trying to do right now. But yet, I could not find the reason Foolproof is not working in my project. So I will give you the view and how I included Foolproof there, the model and how I added Foolproof as a bundle, so maybe someone of you sees my mistake.

View:

 @section Scripts {
         @Scripts.Render("~/bundles/jqueryval")
         @Scripts.Render("~/bundles/mvcfoolproof")
            }

BundleConfig:

public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.unobtrusive*",
                        "~/Scripts/jquery.validate*"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
                        "~/Scripts/jquery-ui-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/datatables").Include(
                        "~/Scripts/DataTables/jquery.dataTables.js"));

            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                        "~/Scripts/modernizr-*"));

            bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                      "~/Scripts/bootstrap.js",
                      "~/Scripts/respond.js"));

            bundles.Add(new StyleBundle("~/Content/css").Include(
                      "~/Content/bootstrap.css",
                      "~/Content/site.css",
                      "~/Content/jquery-ui.css",
                      "~/Content/dataTables.css"));

            bundles.Add(new ScriptBundle("~/bundles/mvcfoolproof").Include(
                    "~/Scripts/MicrosoftAjax.js",
                    "~/Scripts/MicrosoftMvcAjax.js",
                    "~/Scripts/MicrosoftMvcValidation.js",
                    "~/Scripts/MvcFoolproofJQueryValidation.min.js",
                    "~/Scripts/MvcFoolproofValidation.min.js",
                    "~/Scripts/mvcfoolproof.unobtrusive.min.js"));

        }

Model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using Foolproof;

public int CustomerID {get; set;}
public bool ValRequired {get; set;}
[RequiredIfTrue("ValRequired")]
public string NameofCustomer { get; set; }

So, the problem is, if I apply like that the applications runs without errors, but no validation jumps in at this point, so I would be thankful for any kind of help! :)

Community
  • 1
  • 1
RawMVC
  • 123
  • 1
  • 15
  • Please post the controller code. – Mads... Oct 06 '16 at 09:03
  • 1
    Its just `` - delete all the others from you `mvcfoolproof` bundle –  Oct 06 '16 at 09:03
  • Hi, thanks for your quick response. Do you mean, it has to look like this: bundles.Add(new ScriptBundle("~/bundles/mvcfoolproof").Include( "~/Client Scripts/mvcfoolproof.unobtrusive.min")); ? – RawMVC Oct 06 '16 at 09:30
  • Yes, that's all you need - the other files are either now obsolete or not for use with MVC and including them would cause conflicts –  Oct 06 '16 at 09:52
  • I updated my code above, I did as you said before, but no validation jumps in, if I set it just to [Required], it works. What else could I have done wrong? – RawMVC Oct 06 '16 at 09:58
  • Do **not** edit your question and invalidate all the comments/answers that have been added. –  Oct 06 '16 at 10:05
  • If you have included only `unobtrusive.min.js` in the bundle, and the scripts are being loaded correctly, then the code you have shown will work fine. If its not working, then its due to something you have not shown us. Are you getting any errors in the browser console? –  Oct 06 '16 at 10:07
  • Sorry, I did not know this. I just wanted to show you how the code now looks like and still there seems to be a problem with foolproof. – RawMVC Oct 06 '16 at 10:07
  • There is no problem. If I copy the code you have shown to my project, it works fine (if `ValRequired` is checked and `NameofCustomer` is empty, a client side error is shown - and I assume you are generating you r controls correctly using `CheckBoxFor()` and `TextBoxFor()` and inside a form) –  Oct 06 '16 at 10:13
  • Yeah, thats what im trying to do since my idea with the 2 butons didnt work that well as we all know ;) I assume Im doing something wrong with the checkbox, would you mind just to show a snippet how you work with the value of the textbox to turn validation on? – RawMVC Oct 06 '16 at 10:39
  • You need to show your view code so we can determine what your doing wrong :) (and you still have not confirmed if you getting any errors in the browser console) –  Oct 06 '16 at 10:53
  • So, here Iam again it was a logic mistake in my controller, the procedure you described to include foolproof is completely correct, thanks again for your great help! :) – RawMVC Oct 06 '16 at 12:51

1 Answers1

1

In the BundleConfig, replace the last bundle you´ve added by

bundles.Add(new ScriptBundle("~/bundles/mvcfoolproof").Include(
            "~/Client Scripts/mvcfoolproof.unobtrusive.js",
            "~/Client Scripts/mvcfoolproof.unobtrusive.min.js",
            "~/Client Scripts/MvcFoolproofJQueryValidation.js",
            "~/Client Scripts/MvcFoolproofJQueryValidation.min.js",
            "~/Client Scripts/MvcFoolproofValidation.js",
            "~/Client Scripts/MvcFoolproofValidation.min.js"));

When you install the Foolproof package the js files are located in Client Scripts folder, not in Scripts folder.

Franco Dipre
  • 371
  • 3
  • 9
  • 2
    Its worth noting, that if you include all the files, you will include both the full and the minified versions of them all. That will result in duplicated code, and a much larger bundle. You should only include either all the .min.js files, or the ones without. – SuneRadich Mar 30 '17 at 09:50
  • You only need one file, too. If you're not using jQuery, use `MvcFoolproofValidation.*`; if you're using jQuery but not unobtrusive validation, use `MvcFoolproofJQueryValidation.*`; and if you're using unobtrusive validation (which itself requires jQuery), use `mvcfoolproof.unobtrusive.*` (the * wildcard selector includes both minified and unminified files, but the bundler is smart enough to include one or the other based on whether your build configuration is Debug or Release). – Extragorey Jun 04 '19 at 01:02