Skip to content

Commit ea85268

Browse files
committed
Validation added to BlogRoll widget (3.2.1.8)
1 parent 817c7f6 commit ea85268

File tree

3 files changed

+86
-57
lines changed

3 files changed

+86
-57
lines changed

BlogEngine/BlogEngine.Core/Data/ViewModels/BlogRollVM.cs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,25 @@
88

99
namespace BlogEngine.Core.Data.ViewModels
1010
{
11+
/// <summary>
12+
/// Blogroll VM
13+
/// </summary>
1114
public class BlogRollVM
1215
{
16+
/// <summary>
17+
/// Blogroll items
18+
/// </summary>
1319
public List<BlogRollItem> BlogRolls
1420
{
1521
get
1622
{
1723
return Providers.BlogService.FillBlogRolls();
1824
}
1925
}
20-
//public XfnList Xfn { get; set; }
26+
/// <summary>
27+
/// Add blogroll
28+
/// </summary>
29+
/// <param name="form">submitted form</param>
2130
public void Add(NameValueCollection form)
2231
{
2332
var br = new BlogRollItem();
@@ -28,6 +37,11 @@ public void Add(NameValueCollection form)
2837
br.Xfn = GetXfn(form);
2938
Providers.BlogService.InsertBlogRoll(br);
3039
}
40+
/// <summary>
41+
/// Update blogroll
42+
/// </summary>
43+
/// <param name="form">Submitter form</param>
44+
/// <param name="id">Blogroll id</param>
3145
public void Update(NameValueCollection form, string id)
3246
{
3347
Guid gId;
@@ -46,6 +60,10 @@ public void Update(NameValueCollection form, string id)
4660
Utils.Log("Can not update BlogRoll " + id);
4761
}
4862
}
63+
/// <summary>
64+
/// Delete blogroll
65+
/// </summary>
66+
/// <param name="id">Blogroll ID</param>
4967
public void Delete(string id)
5068
{
5169
Guid gId;
@@ -59,15 +77,11 @@ public void Delete(string id)
5977
Utils.Log("Can not delete BlogRoll " + id);
6078
}
6179
}
62-
63-
public void SaveForm(NameValueCollection form)
64-
{
65-
//Title = form["txtTitle"];
66-
//Description = form["txtDesc"];
67-
//WebSite = form["txtWebsite"];
68-
//FeedUrl = form["txtUrl"];
69-
}
70-
80+
/// <summary>
81+
/// Get blogroll feeds
82+
/// </summary>
83+
/// <param name="feedUrl">URL</param>
84+
/// <returns>List of posts</returns>
7185
public List<SyndicationItem> GetFeeds(string feedUrl)
7286
{
7387
var lastNews = new List<SyndicationItem>();
@@ -81,12 +95,16 @@ public List<SyndicationItem> GetFeeds(string feedUrl)
8195

8296
return lastNews;
8397
}
84-
98+
/// <summary>
99+
/// Shorten post title
100+
/// </summary>
101+
/// <param name="textToShorten">Title</param>
102+
/// <returns>Shorten title</returns>
85103
public string Shorten(string textToShorten)
86104
{
87-
return HttpUtility.HtmlEncode(textToShorten.Length > BlogSettings.Instance.BlogrollMaxLength
105+
return textToShorten.Length > BlogSettings.Instance.BlogrollMaxLength
88106
? string.Format("{0}...", textToShorten.Substring(0, BlogSettings.Instance.BlogrollMaxLength).Trim())
89-
: textToShorten);
107+
: textToShorten;
90108
}
91109

92110
#region Methods

BlogEngine/BlogEngine.Core/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919
[assembly: CLSCompliant(false)]
2020
[assembly: ComVisible(false)]
2121
[assembly: AllowPartiallyTrustedCallers]
22-
[assembly: AssemblyVersion("3.2.1.7")]
22+
[assembly: AssemblyVersion("3.2.1.8")]
2323
[assembly: SecurityRules(SecurityRuleSet.Level1)]

BlogEngine/BlogEngine.NET/Custom/Widgets/BlogRoll/edit.cshtml

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,26 @@
44
if (Request.HttpMethod == "HEAD") { return; }
55
var vm = new BlogEngine.Core.Data.ViewModels.BlogRollVM();
66

7-
if (Request.Form["btnAdd"] != null || Request.Form["btnUpdate"] != null)
8-
{
9-
Validation.Add("txtTitle", Validator.Required("Field is required"));
10-
Validation.Add("txtDesc", Validator.Required("Field is required"));
11-
Validation.Add("txtWebsite", Validator.Required("Field is required"), Validator.Url("Field is URL"));
12-
Validation.Add("txtUrl", Validator.Required("Field is required"), Validator.Url("Field is URL"));
13-
}
147
var id = Request.Form["hdnId"];
8+
var action = Request.Form["hdnAction"];
159
var br = new BlogRollItem();
1610

1711
if (IsPost)
1812
{
19-
if (Request.Form["btnAdd"] != null)
13+
if (action == "add")
2014
{
21-
if (Validation.IsValid())
22-
{
23-
vm.Add(Request.Form);
24-
@:
25-
<script type="text/javascript">window.parent.toastr.success("Completed");</script>
26-
}
27-
else
28-
{
29-
vm.SaveForm(Request.Form);
30-
}
15+
vm.Add(Request.Form);
16+
@:<script type="text/javascript">window.parent.toastr.success("Completed");</script>
3117
}
32-
else if (Request.Form["btnUpdate"] != null)
18+
else if (action == "update")
3319
{
34-
if (Validation.IsValid())
35-
{
36-
vm.Update(Request.Form, id);
37-
@:
38-
<script type="text/javascript">window.parent.toastr.success("Completed");</script>
39-
}
40-
else
41-
{
42-
vm.SaveForm(Request.Form);
43-
}
20+
vm.Update(Request.Form, id);
21+
@:<script type="text/javascript">window.parent.toastr.success("Completed");</script>
4422
}
45-
else if (Request.Form["btnDelete"] != null)
23+
else if (action == "delete")
4624
{
4725
vm.Delete(id);
48-
@:
49-
<script type="text/javascript">window.parent.toastr.success("Completed");</script>
26+
@:<script type="text/javascript">window.parent.toastr.success("Completed");</script>
5027
}
5128
else
5229
{
@@ -69,26 +46,26 @@
6946
<body class="widget-edit">
7047
<script src="~/Scripts/jquery-2.1.4.min.js"></script>
7148
<div class="widget-edit-blogroll">
72-
<form method="post" onsubmit="clearValidation()" id="frm" name="frm">
49+
<form method="post" id="frm" name="frm">
7350
<div class="form-group">
7451
<label>@Resources.labels.title</label>
7552
<input type="text" class="form-control" id="txtTitle" name="txtTitle" value="@br.Title" />
76-
@Html.ValidationMessage("txtTitle")
53+
<span id="lblTitle" name="lblTitle" class="field-validation-error">Field is required</span>
7754
</div>
7855
<div class="form-group">
7956
<label>@Resources.labels.website</label>
8057
<input type="text" class="form-control" id="txtWebsite" name="txtWebsite" value="@br.BlogUrl" />
81-
@Html.ValidationMessage("txtWebsite")
58+
<span id="lblWebsite" name="lblWebsite" class="field-validation-error">Field is required and must be URL</span>
8259
</div>
8360
<div class="form-group">
8461
<label>@Resources.labels.description</label>
8562
<input type="text" class="form-control" id="txtDesc" name="txtDesc" value="@br.Description" />
86-
@Html.ValidationMessage("txtDesc")
63+
<span id="lblDesc" name="lblDesc" class="field-validation-error">Field is required</span>
8764
</div>
8865
<div class="form-group">
8966
<label>@Resources.labels.url</label>
9067
<input type="text" class="form-control" id="txtUrl" name="txtUrl" value="@br.FeedUrl" />
91-
@Html.ValidationMessage("txtUrl")
68+
<span id="lblUrl" name="lblUrl" class="field-validation-error">Field is required and must be URL</span>
9269
</div>
9370
<label class="control-label">XFN tag</label>
9471
<table id="cblXfn" cellspacing="0" cellpadding="0" border="0">
@@ -125,10 +102,10 @@
125102
<div class="form-group">
126103
@if (!string.IsNullOrEmpty(Request.Form["hdnId"]))
127104
{
128-
<button type="submit" id="btnUpdate" onclick="submitForm('@id')" name="btnUpdate" class="btn btn-success">
105+
<button type="button" id="btnUpdate" onclick="submitForm('@id', 'update')" name="btnUpdate" class="btn btn-success">
129106
<i class="fa fa-check"></i>
130107
</button>
131-
<button type="submit" id="btnDelete" onclick="submitForm('@id')" name="btnDelete" class="btn btn-danger">
108+
<button type="submit" id="btnDelete" onclick="submitForm('@id', 'delete')" name="btnDelete" class="btn btn-danger">
132109
<i class="fa fa-trash"></i>
133110
</button>
134111
<button type="submit" id="btnCancel" name="btnCancel" class="btn btn-default">
@@ -137,7 +114,7 @@
137114
}
138115
else
139116
{
140-
<button type="submit" id="btnAdd" name="btnAdd" class="btn btn-success">
117+
<button type="button" id="btnAdd" onclick="submitForm('','add')" name="btnAdd" class="btn btn-success">
141118
@Resources.labels.add
142119
</button>
143120
}
@@ -162,7 +139,7 @@
162139
{
163140
<tr>
164141
<td class="item-title">
165-
<a title="@roll.Title" href="#" onclick="submitForm('@roll.Id')" class="text-ellipsis pull-left">@roll.Title</a>
142+
<a title="@roll.Title" href="#" onclick="submitForm('@roll.Id', 'load')" class="text-ellipsis pull-left">@roll.Title</a>
166143
<a title="@roll.FeedUrl" class="external-link pull-right" target="_blank" href="@roll.FeedUrl"><i class="fa fa-external-link"></i></a>
167144
</td>
168145
<td>@roll.Description</td>
@@ -173,16 +150,50 @@
173150
</tbody>
174151
</table>
175152
<input type="hidden" id="hdnId" name="hdnId" value="" />
153+
<input type="hidden" id="hdnAction" name="hdnAction" value="" />
176154
</form>
177155
</div>
178156
<script>
179-
var submitForm = function (id) {
157+
var submitForm = function (id, action) {
180158
$("#hdnId").val(id);
181-
$("#frm").submit();
159+
$("#hdnAction").val(action);
160+
161+
if (action == 'add' || action == 'update') {
162+
clearValidation();
163+
if (checkValidation() == true) {
164+
$("#frm").submit();
165+
}
166+
}
167+
else {
168+
$("#frm").submit();
169+
}
170+
}
171+
var checkValidation = function () {
172+
if ($("#txtTitle").val().length < 1) {
173+
$("#lblTitle").show();
174+
return false;
175+
}
176+
if ($("#txtWebsite").val().length < 1 || validateURL($("#txtWebsite").val()) == false) {
177+
$("#lblWebsite").show();
178+
return false;
179+
}
180+
if ($("#txtDesc").val().length < 1) {
181+
$("#lblDesc").show();
182+
return false;
183+
}
184+
if ($("#txtUrl").val().length < 1 || validateURL($("#txtUrl").val()) == false) {
185+
$("#lblUrl").show();
186+
return false;
187+
}
188+
return true;
182189
}
183190
var clearValidation = function () {
184191
$('.field-validation-error').hide();
185192
}
193+
function validateURL(value) {
194+
return /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@@)|\/|\?)*)?$/i.test(value);
195+
}
196+
clearValidation();
186197
</script>
187198
</body>
188199
</html>

0 commit comments

Comments
 (0)