Ejemplo n.º 1
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="message">Message to display</param>
 /// <param name="ex">Previous exception</param>
 public ValidationException(string message, Exception ex)
     : base(message, ex)
 {
     this._messages = new ErrorCollection();
     this._messages.Add(message);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Helper function for outputting HTML. Used by asTable(), asUl(), asP().
        /// </summary>
        /// <remarks>
        /// Normal Row formatting placeholders:
        ///     {0} - Label
        ///     {1} - Field
        ///     {2} - Help Text
        ///     {3} - Errors
        /// </remarks>
        /// <param name="normalRow">Format of a normal row.</param>
        /// <param name="errorRow">Format of a error row.</param>
        /// <param name="rowEnder">HTML at the end of a normal row.</param>
        /// <param name="helpTextHtml">Format of text for a help row.</param>
        /// <param name="errorsOnSeparateRow">Place errorDictionary on a seperate row.</param>
        /// <returns></returns>
        protected string RenderHtml(string normalRow, string errorRow, string rowEnder, string helpTextHtml, bool errorsOnSeparateRow)
        {
            const string HiddenFieldErrorFormat = "(Hidden field {0}) {1}";

            List <string>   output       = new List <string>(this._fields.Count); // Help the list be guessing size.
            ErrorCollection topErrors    = Errors.Get(NonFieldErrors, new ErrorCollection());
            List <string>   hiddenFields = new List <string>();

            foreach (BoundField bf in this)
            {
                // Get the errorDictionary
                ErrorCollection bfErrors = new ErrorCollection(bf.Errors);

                if (bf.IsHidden)
                {
                    // Append errorDictionary to top errorDictionary
                    foreach (var error in bfErrors)
                    {
                        topErrors.Add(string.Format(CultureInfo.CurrentUICulture,
                                                    HiddenFieldErrorFormat, bf.Name, error));
                    }
                    hiddenFields.Add(bf.ToString());
                }
                else
                {
                    if (errorsOnSeparateRow)
                    {
                        output.Add(string.Format(CultureInfo.CurrentUICulture, errorRow, bfErrors));
                    }

                    // Format label
                    string label = bf.Label;
                    if (!string.IsNullOrEmpty(label))
                    {
                        label = HttpUtility.HtmlEncode(label);

                        // Only add the suffix if the label does not end in punctuation.
                        if (!string.IsNullOrEmpty(this.LabelSuffix))
                        {
                            if (Punctuation.Contains(label[label.Length - 1]))
                            {
                                label = string.Concat(label, this.LabelSuffix);
                            }
                        }

                        label = bf.ApplyLabel(label, (ElementAttributesDictionary)null);
                    }
                    else
                    {
                        label = string.Empty;
                    }

                    // Format help text
                    string helpText = string.Empty;
                    if (!string.IsNullOrEmpty(bf.HelpText))
                    {
                        helpText = string.Format(CultureInfo.CurrentUICulture,
                                                 helpTextHtml, HttpUtility.HtmlEncode(bf.HelpText));
                    }

                    // Append result
                    output.Add(string.Format(CultureInfo.CurrentUICulture,
                                             normalRow, label, bf, helpText, bfErrors));
                }
            }

            // Render errorDictionary
            if (topErrors.Count > 0)
            {
                output.Insert(0, topErrors.ToString());
            }

            // Render hidden fields
            if (hiddenFields.Count > 0)
            {
                if (output.Count > 0)
                {
                    int    lastRowIndex = output.Count - 1;
                    string lastRow      = output[lastRowIndex];

                    // Chop off the trailing rowEnder (e.g. "</td></tr>") and
                    // insert the hidden fields.
                    if (!lastRow.EndsWith(rowEnder, StringComparison.OrdinalIgnoreCase))
                    {
                        // This can happen in the AsP() case (and possibly other
                        // user defined cases): if there are only top errorDictionary, we may
                        // not be able to conscript the last row for our purposes,
                        // so insert a new, empty row.
                        lastRow = string.Format(CultureInfo.CurrentUICulture,
                                                normalRow, string.Empty, string.Empty, string.Empty, string.Empty);
                        output.Add(lastRow);
                        lastRowIndex++;
                    }
                    // Update last row with new row including hidden fields
                    output[lastRowIndex] = string.Concat(
                        lastRow.Remove(lastRow.Length - rowEnder.Length),
                        string.Join("\n", hiddenFields.ToArray()),
                        rowEnder
                        );
                }
                else
                {
                    // If there are not any rows in the output, just append the
                    // hidden fields.
                    output.Add(string.Join("\n", hiddenFields.ToArray()));
                }
            }

            return(string.Join("\n", output.ToArray()));
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Constructor
 /// </summary>
 public ValidationException()
     : base()
 {
     this._messages = new ErrorCollection();
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Copy constructor
 /// </summary>
 /// <param name="value">Errors to copy.</param>
 public ErrorCollection(ErrorCollection value) : base(value)
 {
 }