/// <summary> /// Do a full clean (validation) on all fields /// </summary> public override void FullClean() { base.FullClean(); // Creates an error instance if (!this.IsBound) { return; // No point continuing } // Do field by field validation var cleanedData = new NameObjectDictionary(); foreach (KeyValuePair <string, IField> pair in this._fields) { IField field = pair.Value; string fieldName = this.AddPrefix(pair.Key); object value = field.Widget.GetValueFromDataCollection(this.Data, this.Files, fieldName); try { cleanedData.Add(pair.Key, field.Clean(value)); // Do custom validation if (field.CustomClean != null) { cleanedData = field.CustomClean(cleanedData); } } catch (ValidationException vex) { this.Errors.Add(fieldName, vex.Messages); } } // Do form-wide validation try { this._cleanedData = this.Clean(cleanedData); } catch (ValidationException vex) { this.Errors.Add(NonFieldErrors, vex.Messages); } }
/// <summary> /// Validates form fields passed against a form and returns an Json result. /// </summary> /// <param name="formToValidate">Form instance to validate against.</param> /// <param name="data">Form values to validate.</param> /// <returns>Action result containing Json.</returns> protected ActionResult CleanForm(Form formToValidate, FormCollection data) { var fields = formToValidate.Fields; var errors = new ErrorDictionary(); var cleanedData = new NameObjectDictionary(); foreach (string fieldName in data.Keys) { IField field; // Check if supplied field is valid if (fields.TryGetValue(fieldName, out field)) { object value = field.Widget.GetValueFromDataCollection(data, null, fieldName); try { cleanedData.Add(fieldName, field.Clean(value)); // Do custom validation if (field.CustomClean != null) { cleanedData = field.CustomClean(cleanedData); } } catch (ValidationException vex) { errors.Add(fieldName, vex.Messages); } } } if (errors.Count == 0) { return(Json(new { isValid = true })); } else { return(Json(new { isValid = false, errors = errors })); } }
/// <summary> /// Virtual method for any form-wide cleaning after Field.clean() has /// been called on every field. Any ValidationException thrown by this /// method will not be associated with a particular field. /// </summary> /// <param name="cleanedData">Data that has been cleaned</param> /// <returns></returns> protected virtual NameObjectDictionary Clean(NameObjectDictionary cleanedData) { return(cleanedData); }