I am using MVC 3 + unobstrusive validation.
For some field I am using remote validation too; inside remote validation, I do some checks whose can return errors or just warnings (I would like to take advantage of ajax validation just to give warnings too, not just blocking errors).
I distinct the warnings by the validation errors with the “Info” prefix inside de description text.
So, does exist a way to cycle all validation errors, maintaining just warnings displayed and set errors off according the displayed text?
I was thinking of using an ActionFilterAttribute, or to force the ModelState.Valid = true after cycling and checking all the validation errors…
Here’s an extract of my remote validation routine, with a WarningCheck attribute:
[WarningCheck]
public JsonResult CheckMyField(string myfield)
{
//....some check...if ok I do `return Json(true, JsonRequestBehavior.AllowGet);`
//...if just a warning, I do the follow...
string warn = String.Format(CultureInfo.InvariantCulture,
"Info: some info....");
ModelState.AddModelError(TicketHD, esiste);
return Json(warn, JsonRequestBehavior.AllowGet);
}
[AttributeUsage(AttributeTargets.All)]
public class WarningCheckAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//.... here I'd like to cycle my warnings and if possible maintaining just the text display and set errors off...is it possible?
}
}
EDIT (how to disable client side validation for specific warnings)
Following @CDSmith (@Alfalfastrange) suggestions for server side, I succeed to disable the client side validation too, when a specific text is contained inside each validation error. In particular, my needs were to disable both client & server side validation error just when the errors contained “Info:” text.
Here’s the code I am using for client side behaviour:
....
$("#ticketfrm").submit();
var isvalid = true;
var errmark = $("#ticketfrm .field-validation-error span");
$(errmark).each(function () {
var tst = $(this).text();
if (!(tst.indexOf("Info") != -1))
isvalid = false; //if the val error is not a warn, the it must be a real error!
});
if (isvalid) {
var form = $('#ticketfrm').get(0); //because I'm inside a jquery dialog
$.removeData(form, 'validator');
jQuery('#ticketfrm').unbind('submit').submit();
}
$("#ticketfrm").submit();
}
.....
I hope this may help many people…I worked many hours to get this working! I do not think it must be the more elegant solution, but IT WORKS! 🙂
While for server side validation, please read the marked solution.
If you find this useful, please mark it. Thank you!
I’m not sure I understand the question as well as you’re explaining but what I hear you saying is you need a way of looping thru the ModelState errors and then maintaining the text values of the errors but not show them as errors?? Is that what you are saying?
Well for starters, ModelState is nothing more than a DictionaryList which you can iterate through with no problem.
Something like this can do that:
ModelError contains an ErrorMessage property and Exception property
Not sure if this helps or not but if it points you in the right direction then cool 🙂
Update
Ok here is what I came up with and it works for me with my test app.
First let me lay out what I have so you can copy and replicate
Here’s my model
Here’s a simple view that uses this model
Here’s the controller and actions that I used
You can write GetRealErrors as LINQ instead if you prefer:
I hope that gives you what you were looking for, it works the way I think I understand that you wanted. Let me know