Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Jul 26, 2013 @ 14:19
    Ismail Mayat
    0

    self validating model no error message

    Umbraco mvc i have model which is self validating

            public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)

            {

                if (Weight.ContainsValue(string.Empty) &&

                    Thickness.ContainsValue(string.Empty) &&

                    MinTemp.ContainsValue(string.Empty) &&

                    MaxTemp.ContainsValue(string.Empty) &&

                    ChemicalResistance.ContainsValue(string.Empty) &&

                    TensileStrength.ContainsValue(string.Empty))

                {

                    yield return new ValidationResult(umbraco.library.GetDictionaryItem("SearchError"),

                    new[] { "Weight", "Thickness", "MinTemp", "MaxTemp", "ChemicalResistance", "TensileStrength" });

                }

            }

    in my controller i have

                if (ModelState.IsValid)

    {         

                    //do a search add results to tempdata

     

    return CurrentUmbracoPage();

    }

     

                ModelState.AddModelError("SearchError", umbraco.library.GetDictionaryItem("SearchError"));

     

                return RedirectToCurrentUmbracoPage();

    and in my view i have

    @using(Html.BeginUmbracoForm<SearchFormSurfaceController>("HandleSearchForm", null, new { @class = "form-horizontal" }))

            {

                <fieldset>

                    <legend>Select your requirements</legend>            

                    <div class="control-group first">

                        @Html.LabelFor(model => model.Weight, new { @class = "control-label" })

                        <div class="controls">

                            @Html.DropDownListFor(model => model.Weight.Keys,

                                                  new SelectList(

                                                      Model.Weight, 

                                                      "Key", 

                                                      "Value"),Umbraco.GetDictionaryValue("Select"))

     

                            @Html.ValidationMessageFor(model => model.Weight)

                        </div>

                    </div>

     

                    <div class="control-group">

                        @Html.LabelFor(model => model.Thickness, new { @class = "control-label" })

                        <div class="controls">

                            @Html.DropDownListFor(model => model.Thickness.Keys,

                                                  new SelectList(

                                                      Model.Thickness, 

                                                      "Key", 

                                                      "Value"),Umbraco.GetDictionaryValue("Select"))

                            @Html.ValidationMessageFor(model => model.Thickness)

                        </div>

                    </div>

     

                    <div class="control-group">

                        @Html.LabelFor(model => model.MinTemp, new { @class = "control-label" })

                        <div class="controls">

                            @Html.DropDownListFor(model => model.MinTemp.Keys,

                                                  new SelectList(

                                                      Model.MinTemp, 

                                                      "Key", 

                                                      "Value"),Umbraco.GetDictionaryValue("Select"))

                            @Html.ValidationMessageFor(model => model.MinTemp)

                        </div>

                    </div>

     

                    <div class="control-group">

                        @Html.LabelFor(model => model.MaxTemp, new { @class = "control-label" })

                    <div class="controls">

                            @Html.DropDownListFor(model => model.MaxTemp.Keys,

                                                  new SelectList(

                                                      Model.MaxTemp, 

                                                      "Key", 

                                                      "Value"),Umbraco.GetDictionaryValue("Select"))

                            @Html.ValidationMessageFor(model => model.MaxTemp)

                        </div>

                    </div>

     

                    <div class="control-group">

                        @Html.LabelFor(model => model.ChemicalResistance, new { @class = "control-label" })

                        <div class="controls">

                            @Html.DropDownListFor(model => model.ChemicalResistance.Keys,

                                                  new SelectList(

                                                      Model.ChemicalResistance, 

                                                      "Key", 

                                                      "Value"),Umbraco.GetDictionaryValue("Select"))

                        </div>

                        @Html.ValidationMessageFor(model => model.ChemicalResistance)

                    </div>

     

                    <div class="control-group">

                        @Html.LabelFor(model => model.TensileStrength, new { @class = "control-label" })

                        <div class="controls">

                            @Html.DropDownListFor(model => model.TensileStrength.Keys,

                                                  new SelectList(

                                                      Model.TensileStrength, 

                                                      "Key", 

                                                      "Value"),Umbraco.GetDictionaryValue("Select"))

                            @Html.ValidationMessageFor(model => model.TensileStrength)

                        </div>

                    </div>

     

                    <div class="control-group">

                        <div class="controls">

                            <input class="submit" type="submit" value="@Umbraco.GetDictionaryValue("Submit")" />

                            @Html.ValidationMessage("SearchError")

                            @Html.ValidationSummary()

                        </div>

                    </div>

                </fieldset>

            }

        </div>

    when you do not select anything in the dropdowns no search happens which is correct becuase the model is not valid however my error message is not displaying. Am I missing something? Also do i need to do something extra to do client side validation?

    Regards

     

    Ismail

  • Tim 174 posts 398 karma points
    Jul 26, 2013 @ 14:21
    Tim
    0

    Hi Ismail,

    I've not checked your code but in regards "Also do i need to do something extra to do client side validation?" -yes, you'll need to enable unobtrusive validation in the web.config and add the JavaScript files:

     

     <add key="ClientValidationEnabled" value="true"/> 
        <add key="UnobtrusiveJavaScriptEnabled" value="true"/> 
    <script src="##CDN PATH##/jquery" type="text/javascript"></script>
    <script src="##CDN PATH##/jquery.validate.js" type="text/javascript"></script>
    <script src="##CDN PATH##/jquery.validate.unobtrusive.js" type="text/javascript"></script>
    MS have both on their site:
    Tim
  • Tim 174 posts 398 karma points
    Jul 26, 2013 @ 14:23
    Tim
    0

    Oh and you'll also need to add Client Side validation to your validator by implementing IClientValidatable.

    Personally I prefer validation attributes rather than making the models validate themselves as it's re-usable and offers nicer seperation of concerns.

    Tim

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Jul 26, 2013 @ 15:23
    Ismail Mayat
    0

    Tim,

    I have the keys in my web.config and i have all the js files.  I also prefer attributes but could not see another way of validating this model. The view renders bunch of drop downs user must select at least one don't care which.  This was the only way i could do it.  The validation works however I cannot see the error message not even server side validation.

    Regards

    Ismail

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Jul 26, 2013 @ 15:41
    Ismail Mayat
    0

    Tim,

    Ok so you cannot by default do clientside validation with IValidatableObject aka self validating model http://stackoverflow.com/questions/4748703/ivalidatableobject-in-mvc3-client-side-validation/4826678#4826678 which is fine bit of jquery will sort that out. Still no joy on why my server side message wont show!!

    Regards

    Ismail

  • Mads Krohn 211 posts 504 karma points c-trib
    Jul 26, 2013 @ 16:17
    Mads Krohn
    0

    As far as I can see, you are only validating for empty strings? I think, that when you don't have a value selected, the given field will simply not be part of the post, thus the model value will be NULL. Could be wrong, but that's my best guess.

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Jul 26, 2013 @ 16:31
    Ismail Mayat
    0

    Mads,

    The model is validating server side because if nothing is selected you get no search results. When you select something then search you get search results so code

    if (ModelState.IsValid)

    {         

                    //do a search add results to tempdata

     

    return CurrentUmbracoPage();

    }

    is executing and the model is being validated. With regards to cilent side i just added some jquery.

    Regards

    Ismail

  • Mads Krohn 211 posts 504 karma points c-trib
    Jul 26, 2013 @ 16:42
    Mads Krohn
    0

    Correct me if I'm wrong, but your validation logic says, that all properties must contain an empty string for the error message to be displayed, right? So, unless all properties are an empty string, the validation won't kick in. So, my logic would say, that one of the properties, if not selected on the client, is returning NULL instead of an empty string thus byparsing the validation.

    Or am I misundestading you, and the

    ModelState.AddModelError("SearchError", umbraco.library.GetDictionaryItem("SearchError"));

    is triggered, though not showed on the client?

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Jul 26, 2013 @ 16:47
    Ismail Mayat
    0

    Mads,

    All my properties on model are dictionaries. If nothing is selected all the dictionaries will contain one item with key and no value. So if all the dictionaries only have empty values the model is not valid. All that is working fine as the search code is not being hit however the error message i am sending back from the model

     {

                    yield return new ValidationResult(umbraco.library.GetDictionaryItem("SearchError"),

                    new[] { "Weight", "Thickness", "MinTemp", "MaxTemp", "ChemicalResistance", "TensileStrength" });

                }

    is not showing i added additional error to model state so i could do

      @Html.ValidationMessage("SearchError")

    I also have labels for all my dropdowns 

     @Html.ValidationMessageFor(model => model.TensileStrength)

    but they dont show either.

     

  • Mads Krohn 211 posts 504 karma points c-trib
    Jul 26, 2013 @ 17:03
    Mads Krohn
    0

    Ahh ok, didn't realize it was dictionaries, mistook them for simple string properties. Can't help out more atm then, as I'm not on a "coding" computer, sorry :)

  • Mads Krohn 211 posts 504 karma points c-trib
    Jul 29, 2013 @ 11:49
    Mads Krohn
    0

    Ismail,

    Im curious, did you manage to solve the problem?

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Jul 29, 2013 @ 12:29
    Ismail Mayat
    0

    Mads,

    Still no joy!

    Regards

    Ismail

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Aug 01, 2013 @ 10:01
    Ismail Mayat
    0

    Mads,

    I think its related to this http://issues.umbraco.org/issue/U4-1339

    Regards

    Ismail

  • Mads Krohn 211 posts 504 karma points c-trib
    Aug 01, 2013 @ 10:09
    Mads Krohn
    0

    Hmm interesting .. :)

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Aug 01, 2013 @ 10:14
    Ismail Mayat
    0

    Mads,

    Gets even better if my controller when model is not valid i do

    return PartialView("SearchForm", new SearchFormModel(CurrentPage));
    

    Instead of

    return RedirectToCurrentUmbracoPage();
    

    Then my validation messages set by

     @Html.ValidationMessageFor(model => model.ChemicalResistance)
    

    All fire and I can see the messages. However becuase I am sending back partial view I just get the html for that view and not the whole page so all the css etc is missing. So it is definately that issue on the issue tracker.

    Regards

    Ismail

  • Mads Krohn 211 posts 504 karma points c-trib
    Aug 01, 2013 @ 10:56
    Mads Krohn
    0

    Yes, I guess that makes sense. The validation messages are stored in ViewData, and if you are redirecting, then as Shannon says, they are not kept. Did you try to simply do

    return CurrentUmbracoPage();
    

    Or something like that?

  • Tim 174 posts 398 karma points
    Aug 01, 2013 @ 11:12
    Tim
    0

    If it is modelstate causing the issue then  you can use the ModelStateToTempData attribute on both your controllers and all will be good:

    http://blogs.thesitedoctor.co.uk/tim/2013/06/24/Using+ModelStateToTempData+In+Umbraco.aspx

Please Sign in or register to post replies

Write your reply to:

Draft