Permalink
Browse files

Add `[Remote(routeName : "name")]` tests

- relates to #4445
- also relates to #4309 / fb07fee though restored and renamed files are slightly different
 - latest Unobtrusive version; copy just one file under `wwwroot`
- add JavaScript file and CDN links to enable manual testing of `[Remote]` tests in `BasicWebSite`
 - include renamed files for recreating the minimized JavaScript file manually
  • Loading branch information...
1 parent 742a9e3 commit e3db547011eff97f273ecaf7dcc7ac559caec8a1 @dougbu dougbu committed Apr 26, 2016
View
@@ -12,7 +12,6 @@ PublishProfiles/
.vs/
bower_components/
node_modules/
-**/wwwroot/lib/
debugSettings.json
project.lock.json
*.user
@@ -54,7 +54,7 @@ public RemoteAttributeValidationTest(MvcTestFixture<BasicWebSite.Startup> fixtur
}
[Theory]
- [InlineData("", "\"/RemoteAttribute_Verify/IsIdAvailable rejects Joe1.\"")]
+ [InlineData("", "\"/RemoteAttribute_Verify/IsIdAvailable rejects UserId1: 'Joe1'.\"")]
[InlineData("/Area1", "false")]
[InlineData("/Area2",
"\"/Area2/RemoteAttribute_Verify/IsIdAvailable rejects 'Joe4' with 'Joe1', 'Joe2', and 'Joe3'.\"")]
@@ -78,7 +78,7 @@ public RemoteAttributeValidationTest(MvcTestFixture<BasicWebSite.Startup> fixtur
}
[Theory]
- [InlineData("", "\"/RemoteAttribute_Verify/IsIdAvailable rejects Jane1.\"")]
+ [InlineData("", "\"/RemoteAttribute_Verify/IsIdAvailable rejects UserId1: 'Jane1'.\"")]
[InlineData("/Area1", "false")]
public async Task RemoteAttribute_VerificationAction_PostReturnsExpectedJson(
string pathSegment,
@@ -1,4 +1,4 @@
-<!DOCTYPE html>
+<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
@@ -40,36 +40,44 @@
<div class="form-group">
<label class="control-label col-md-2" for="UserId1">UserId1</label>
<div class="col-md-10">
- <input class="form-control text-box single-line" data-val="true" data-val-remote="&#x27;UserId1&#x27; is invalid." data-val-remote-additionalfields="*.UserId1" data-val-remote-url="/Area1/RemoteAttribute_Verify/IsIdAvailable" id="UserId1" name="UserId1" type="text" value="" />
+ <input class="form-control text-box single-line" data-val="true" data-val-remote="&#x27;UserId1&#x27; is invalid." data-val-remote-additionalfields="*.UserId1" data-val-remote-url="/Area1/RemoteAttribute_Verify/IsIdAvailable" data-val-required="UserId1 is required" id="UserId1" name="UserId1" type="text" value="" />
<span class="field-validation-valid text-danger" data-valmsg-for="UserId1" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="UserId2">UserId2</label>
<div class="col-md-10">
- <input class="form-control text-box single-line" data-val="true" data-val-remote="&#x27;UserId2&#x27; is invalid." data-val-remote-additionalfields="*.UserId2" data-val-remote-type="Post" data-val-remote-url="/RemoteAttribute_Verify/IsIdAvailable" id="UserId2" name="UserId2" type="text" value="" />
+ <input class="form-control text-box single-line" data-val="true" data-val-remote="&#x27;UserId2&#x27; is invalid." data-val-remote-additionalfields="*.UserId2" data-val-remote-type="Post" data-val-remote-url="/RemoteAttribute_Verify/IsIdAvailable" data-val-required="UserId2 is required" id="UserId2" name="UserId2" type="text" value="" />
<span class="field-validation-valid text-danger" data-valmsg-for="UserId2" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="UserId3">UserId3</label>
<div class="col-md-10">
- <input class="form-control text-box single-line" data-val="true" data-val-remote="/Area1/RemoteAttribute_Verify/IsIdAvailable rejects you." data-val-remote-additionalfields="*.UserId3" data-val-remote-url="/Area1/RemoteAttribute_Verify/IsIdAvailable" id="UserId3" name="UserId3" type="text" value="" />
+ <input class="form-control text-box single-line" data-val="true" data-val-remote="/Area1/RemoteAttribute_Verify/IsIdAvailable rejects you." data-val-remote-additionalfields="*.UserId3" data-val-remote-url="/Area1/RemoteAttribute_Verify/IsIdAvailable" data-val-required="UserId3 is required" id="UserId3" name="UserId3" type="text" value="" />
<span class="field-validation-valid text-danger" data-valmsg-for="UserId3" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="UserId4">UserId4</label>
<div class="col-md-10">
- <input class="form-control text-box single-line" data-val="true" data-val-remote="&#x27;UserId4&#x27; is invalid." data-val-remote-additionalfields="*.UserId4,*.UserId1,*.UserId2,*.UserId3" data-val-remote-url="/Area2/RemoteAttribute_Verify/IsIdAvailable" id="UserId4" name="UserId4" type="text" value="" />
+ <input class="form-control text-box single-line" data-val="true" data-val-remote="&#x27;UserId4&#x27; is invalid." data-val-remote-additionalfields="*.UserId4,*.UserId1,*.UserId2,*.UserId3" data-val-remote-url="/Area2/RemoteAttribute_Verify/IsIdAvailable" data-val-required="UserId4 is required" id="UserId4" name="UserId4" type="text" value="" />
<span class="field-validation-valid text-danger" data-valmsg-for="UserId4" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
+ <label class="control-label col-md-2" for="UserId5">UserId5</label>
+ <div class="col-md-10">
+ <input class="form-control text-box single-line" data-val="true" data-val-remote="&#x27;UserId5&#x27; is invalid." data-val-remote-additionalfields="*.UserId5" data-val-remote-type="Post" data-val-remote-url="/RemoteAttribute_Verify/IsIdAvailable" data-val-required="UserId5 is required" id="UserId5" name="UserId5" type="text" value="" />
+ <span class="field-validation-valid text-danger" data-valmsg-for="UserId5" data-valmsg-replace="true"></span>
+ </div>
+ </div>
+
+ <div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
@@ -83,10 +91,10 @@
</div>
- <script src="/lib/jquery/jquery.js"></script>
+ <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.4.min.js"></script>
- <script src="/lib/jquery-validation/jquery.validate.js"></script>
- <script src="/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
+ <script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.14.0/jquery.validate.min.js"></script>
+ <script src="/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
</body>
</html>
@@ -1,4 +1,4 @@
-<!DOCTYPE html>
+<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
@@ -40,36 +40,44 @@
<div class="form-group">
<label class="control-label col-md-2" for="UserId1">UserId1</label>
<div class="col-md-10">
- <input class="form-control text-box single-line" data-val="true" data-val-remote="&#x27;UserId1&#x27; is invalid." data-val-remote-additionalfields="*.UserId1" data-val-remote-url="/RemoteAttribute_Verify/IsIdAvailable" id="UserId1" name="UserId1" type="text" value="" />
+ <input class="form-control text-box single-line" data-val="true" data-val-remote="&#x27;UserId1&#x27; is invalid." data-val-remote-additionalfields="*.UserId1" data-val-remote-url="/RemoteAttribute_Verify/IsIdAvailable" data-val-required="UserId1 is required" id="UserId1" name="UserId1" type="text" value="" />
<span class="field-validation-valid text-danger" data-valmsg-for="UserId1" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="UserId2">UserId2</label>
<div class="col-md-10">
- <input class="form-control text-box single-line" data-val="true" data-val-remote="&#x27;UserId2&#x27; is invalid." data-val-remote-additionalfields="*.UserId2" data-val-remote-type="Post" data-val-remote-url="/RemoteAttribute_Verify/IsIdAvailable" id="UserId2" name="UserId2" type="text" value="" />
+ <input class="form-control text-box single-line" data-val="true" data-val-remote="&#x27;UserId2&#x27; is invalid." data-val-remote-additionalfields="*.UserId2" data-val-remote-type="Post" data-val-remote-url="/RemoteAttribute_Verify/IsIdAvailable" data-val-required="UserId2 is required" id="UserId2" name="UserId2" type="text" value="" />
<span class="field-validation-valid text-danger" data-valmsg-for="UserId2" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="UserId3">UserId3</label>
<div class="col-md-10">
- <input class="form-control text-box single-line" data-val="true" data-val-remote="/Area1/RemoteAttribute_Verify/IsIdAvailable rejects you." data-val-remote-additionalfields="*.UserId3" data-val-remote-url="/Area1/RemoteAttribute_Verify/IsIdAvailable" id="UserId3" name="UserId3" type="text" value="" />
+ <input class="form-control text-box single-line" data-val="true" data-val-remote="/Area1/RemoteAttribute_Verify/IsIdAvailable rejects you." data-val-remote-additionalfields="*.UserId3" data-val-remote-url="/Area1/RemoteAttribute_Verify/IsIdAvailable" data-val-required="UserId3 is required" id="UserId3" name="UserId3" type="text" value="" />
<span class="field-validation-valid text-danger" data-valmsg-for="UserId3" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="UserId4">UserId4</label>
<div class="col-md-10">
- <input class="form-control text-box single-line" data-val="true" data-val-remote="&#x27;UserId4&#x27; is invalid." data-val-remote-additionalfields="*.UserId4,*.UserId1,*.UserId2,*.UserId3" data-val-remote-url="/Area2/RemoteAttribute_Verify/IsIdAvailable" id="UserId4" name="UserId4" type="text" value="" />
+ <input class="form-control text-box single-line" data-val="true" data-val-remote="&#x27;UserId4&#x27; is invalid." data-val-remote-additionalfields="*.UserId4,*.UserId1,*.UserId2,*.UserId3" data-val-remote-url="/Area2/RemoteAttribute_Verify/IsIdAvailable" data-val-required="UserId4 is required" id="UserId4" name="UserId4" type="text" value="" />
<span class="field-validation-valid text-danger" data-valmsg-for="UserId4" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
+ <label class="control-label col-md-2" for="UserId5">UserId5</label>
+ <div class="col-md-10">
+ <input class="form-control text-box single-line" data-val="true" data-val-remote="&#x27;UserId5&#x27; is invalid." data-val-remote-additionalfields="*.UserId5" data-val-remote-type="Post" data-val-remote-url="/RemoteAttribute_Verify/IsIdAvailable" data-val-required="UserId5 is required" id="UserId5" name="UserId5" type="text" value="" />
+ <span class="field-validation-valid text-danger" data-valmsg-for="UserId5" data-valmsg-replace="true"></span>
+ </div>
+ </div>
+
+ <div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
@@ -83,10 +91,10 @@
</div>
- <script src="/lib/jquery/jquery.js"></script>
+ <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.4.min.js"></script>
- <script src="/lib/jquery-validation/jquery.validate.js"></script>
- <script src="/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
+ <script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.14.0/jquery.validate.min.js"></script>
+ <script src="/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
</body>
</html>
@@ -56,6 +56,14 @@
</div>
<div class="form-group">
+ @Html.LabelFor(model => model.UserId5, htmlAttributes: new { @class = "control-label col-md-2" })
+ <div class="col-md-10">
+ @Html.EditorFor(model => model.UserId5, new { htmlAttributes = new { @class = "form-control" } })
+ @Html.ValidationMessageFor(model => model.UserId5, "", new { @class = "text-danger" })
+ </div>
+ </div>
+
+ <div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
@@ -68,8 +76,6 @@
</div>
@section Scripts {
- @* Until script helpers are available, add script references manually. *@
- @* @Scripts.Render("~/bundles/jqueryval") *@
- <script src="@Url.Content("~/lib/jquery-validation/jquery.validate.js")"></script>
- <script src="@Url.Content("~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js")"></script>
+ <script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.14.0/jquery.validate.min.js"></script>
+ <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
}
@@ -61,6 +61,16 @@
@Html.DisplayFor(model => model.UserId4)
</dd>
</dl>
+
+ <dl class="dl-horizontal">
+ <dt>
+ @Html.DisplayNameFor(model => model.UserId5)
+ </dt>
+
+ <dd>
+ @Html.DisplayFor(model => model.UserId5)
+ </dd>
+ </dl>
</div>
<p>
@@ -25,7 +25,7 @@
@RenderBody()
</div>
- <script src="~/lib/jquery/jquery.js"></script>
+ <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.4.min.js"></script>
@RenderSection("scripts", required: false)
</body>
</html>
@@ -20,7 +20,6 @@ public IActionResult Create()
[HttpPost]
public IActionResult Create(RemoteAttributeUser user)
{
- ModelState.Remove("id");
if (!ModelState.IsValid)
{
return View(user);
@@ -1,18 +1,44 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+using BasicWebSite.Models;
using Microsoft.AspNetCore.Mvc;
namespace BasicWebSite.Controllers
{
- [Route("[controller]/[action]")]
public class RemoteAttribute_VerifyController : Controller
{
- // This action is overloaded and may receive requests to validate either UserId1 or UserId2.
+ // This action is overloaded and may receive requests to validate UserId1, UserId2 or UserId5.
[AcceptVerbs("Get", "Post")]
- public IActionResult IsIdAvailable(string userId1, string userId2)
+ [Route("[controller]/[action]", Name = "VerifyRoute")]
+ public IActionResult IsIdAvailable(string userId1, string userId2, string userId5)
{
- return Json(data: string.Format("/RemoteAttribute_Verify/IsIdAvailable rejects {0}.", userId1 ?? userId2));
+ string name;
+ string value;
+ if (userId1 != null)
+ {
+ name = nameof(RemoteAttributeUser.UserId1);
+ value = userId1;
+ }
+ else if (userId2 != null)
+ {
+
+ name = nameof(RemoteAttributeUser.UserId2);
+ value = userId2;
+ }
+ else if (userId5 != null)
+ {
+
+ name = nameof(RemoteAttributeUser.UserId5);
+ value = userId5;
+ }
+ else
+ {
+ name = "unknown";
+ value = string.Empty;
+ }
+
+ return Json(data: $"/RemoteAttribute_Verify/IsIdAvailable rejects {name}: '{value}'.");
}
}
}
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
namespace BasicWebSite.Models
@@ -10,25 +11,33 @@ public class RemoteAttributeUser
public int Id { get; set; }
// Controller in current area.
+ [Required(ErrorMessage = "UserId1 is required")]
[Remote(action: "IsIdAvailable", controller: "RemoteAttribute_Verify")]
public string UserId1 { get; set; }
// Controller in root area.
+ [Required(ErrorMessage = "UserId2 is required")]
[Remote(action: "IsIdAvailable", controller: "RemoteAttribute_Verify", areaName: null, HttpMethod = "Post")]
public string UserId2 { get; set; }
+ [Required(ErrorMessage = "UserId3 is required")]
[Remote(
action: "IsIdAvailable",
controller: "RemoteAttribute_Verify",
- areaName:"Area1",
+ areaName: "Area1",
ErrorMessage = "/Area1/RemoteAttribute_Verify/IsIdAvailable rejects you.")]
public string UserId3 { get; set; }
+ [Required(ErrorMessage = "UserId4 is required")]
[Remote(
action:"IsIdAvailable",
controller: "RemoteAttribute_Verify",
areaName: "Area2",
AdditionalFields = "UserId1, UserId2, UserId3")]
public string UserId4 { get; set; }
+
+ [Required(ErrorMessage = "UserId5 is required")]
+ [Remote(routeName: "VerifyRoute", HttpMethod = "Post")]
+ public string UserId5 { get; set; }
}
}
@@ -56,6 +56,14 @@
</div>
<div class="form-group">
+ @Html.LabelFor(model => model.UserId5, htmlAttributes: new { @class = "control-label col-md-2" })
+ <div class="col-md-10">
+ @Html.EditorFor(model => model.UserId5, new { htmlAttributes = new { @class = "form-control" } })
+ @Html.ValidationMessageFor(model => model.UserId5, "", new { @class = "text-danger" })
+ </div>
+ </div>
+
+ <div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
@@ -68,8 +76,6 @@
</div>
@section Scripts {
- @* Until script helpers are available, add script references manually. *@
- @* @Scripts.Render("~/bundles/jqueryval") *@
- <script src="@Url.Content("~/lib/jquery-validation/jquery.validate.js")"></script>
- <script src="@Url.Content("~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js")"></script>
+ <script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.14.0/jquery.validate.min.js"></script>
+ <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
}
@@ -61,6 +61,16 @@
@Html.DisplayFor(model => model.UserId4)
</dd>
</dl>
+
+ <dl class="dl-horizontal">
+ <dt>
+ @Html.DisplayNameFor(model => model.UserId5)
+ </dt>
+
+ <dd>
+ @Html.DisplayFor(model => model.UserId5)
+ </dd>
+ </dl>
</div>
<p>
@@ -25,7 +25,7 @@
@RenderBody()
</div>
- <script src="~/lib/jquery/jquery.js"></script>
+ <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.4.min.js"></script>
@RenderSection("scripts", required: false)
</body>
</html>
@@ -0,0 +1,14 @@
+{
+ "name": "BasicWebSite",
+ "description": "Web site demonstrating various validations.",
+ "private": true,
+ "dependencies": {
+ "jquery-validation-unobtrusive": "3.2.6"
+ },
+ "exportsOverride": {
+ "jquery-validation-unobtrusive": {
+ "": "jquery.validate.unobtrusive.min.js"
+ },
+ "*": { }
+ }
+}
Oops, something went wrong.

0 comments on commit e3db547

Please sign in to comment.