Ejemplo n.º 1
0
        /// <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 }));
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Do a full clean (validation) on all fields
 /// </summary>
 public virtual void FullClean()
 {
     this._errors = new ErrorDictionary();
 }