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?
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:
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.
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.
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.
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.
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"),
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 :)
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.
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
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
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:
<script src="##CDN PATH##/jquery.validate.js" type="text/javascript"></script>
<script src="##CDN PATH##/jquery.validate.unobtrusive.js" type="text/javascript"></script>
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
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
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
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.
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
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?
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.
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 :)
Ismail,
Im curious, did you manage to solve the problem?
Mads,
Still no joy!
Regards
Ismail
Mads,
I think its related to this http://issues.umbraco.org/issue/U4-1339
Regards
Ismail
Hmm interesting .. :)
Mads,
Gets even better if my controller when model is not valid i do
Instead of
Then my validation messages set by
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
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
Or something like that?
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
is working on a reply...