Ejemplo n.º 1
0
        /// <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);
            }
        }
Ejemplo n.º 2
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 }));
            }
        }