// 投稿フォームを出力します。 // 引数はコメント先となる親の Article です。 protected XmlNode GetForm(Article a, PostErrorCollection errors){ string formTitle = ""; if(a.Parent == 0 && a.CommentTo == null){ formTitle = "新規投稿フォーム"; } else { formTitle = "コメントフォーム"; } XmlElement result = Html.Form(null, "post"); result.SetAttribute("enctype", MultipartContentType); result.SetAttribute("accept-charset", FormCharset); XmlElement fs = Html.Fieldset(formTitle); fs.AppendChild(Html.P("note", FormNotice)); fs.AppendChild(GetTextField(InputSubjectName, a.Subject, InputSubjectLabel, errors)); fs.AppendChild(GetTextField(InputSenderName, a.Name, InputSenderLabel, errors)); fs.AppendChild(GetTextArea(InputBodyName, a.Message, InputBodyLabel, errors)); fs.AppendChild(Html.P("submit", Html.Submit("投稿内容確認"), Html.Hidden(InputCharsetName, InputCharsetValue))); result.AppendChild(fs); return result; }
// 投稿された Article にエラーがないかどうかチェックします。 private PostErrorCollection ArticleCheck(Article a, Article latestArticle){ PostErrorCollection errors = new PostErrorCollection(); if(string.IsNullOrEmpty(a.Subject)){ errors.Add(InputSubjectName, "題名が入力されていません。"); } else if(a.Subject.Length > SubjectMaxLength){ errors.Add(InputSubjectName, String.Format("題名は{0}字ありますが、最大{1}字までしか入力できません。あと{2}字減らしてください。", a.Subject.Length, SubjectMaxLength, SubjectMaxLength - a.Subject.Length)); } if(string.IsNullOrEmpty(a.Name)){ errors.Add(InputSenderName, "投稿者の名前が入力されていません。"); } else if(a.Name.Length > NameMaxLength){ errors.Add(InputSenderName, String.Format("投稿者の名前は{0}字ありますが、最大{1}字までしか入力できません。あと{2}字減らしてください。", a.Name.Length, NameMaxLength, NameMaxLength - a.Name.Length)); } if(String.IsNullOrEmpty(a.Message)){ errors.Add(InputBodyName, "本文が入力されていません。空白のみの投稿は出来ません。"); } else { // 本文を調査 1. 長すぎないか? 2. 引用のみでないか? int messageLine = 0; int QorELine = 0; // 引用or空行の数 Regex crlf = new BakeraReg.CrLf(); foreach(string s in crlf.Split(a.Message)){ if(s.StartsWith(">") || s == "") QorELine++; messageLine++; } if(a.Message.Length > MessageMaxLength){ errors.Add(InputBodyName, String.Format("本文は{0}字ありますが、最大{1}字までしか入力できません。あと{2}字減らしてください。", a.Message.Length, MessageMaxLength, a.Message.Length - MessageMaxLength)); } if(QorELine == messageLine){ errors.Add(InputBodyName, "本文に引用部と空行しか含まれていません。引用でない本文を記入してください。"); } } // コメント先の実在チェック if(a.CommentTo != null){ HatomaruResponse parentResponse = myModel.Manager.GetResponse(a.CommentTo); if(parentResponse == null) errors.Add(null, "コメント先のコンテンツが取得できませんでした。コメント先が削除されているか、URLが変更になっている可能性があります。"); } // 二重投稿チェック if(latestArticle != null && a.Message == latestArticle.Message){ errors.Add(null, string.Format("二重投稿です。本文が同一の記事が #{0} として既に投稿されています (同じ文面での連続投稿はできません)。", latestArticle.Id)); } return errors; }
private XmlNode GetTextArea(string name, string val, string label, PostErrorCollection errors){ XmlElement result = Html.P(); if(errors != null && errors.IsError(name)) result.SetAttribute("class", "error"); result.AppendChild(Html.TextArea(name, val, label)); return result; }
// エラーメッセージを ul 要素として返します。 private XmlNode GetErrors(PostErrorCollection errors){ if(errors.Count == 0) return Html.CreateDocumentFragment(); XmlNode result = Html.Create("ul"); foreach(PostError e in errors){ if(string.IsNullOrEmpty(e.Name)){ result.AppendChild(Html.Create("li", null, e.Message)); } else { XmlElement label = Html.Create("label"); label.InnerText = e.Message; label.SetAttribute("for", e.Name); result.AppendChild(Html.Create("li", null, label)); } } return result; }