Skip to content
This repository has been archived by the owner on May 11, 2023. It is now read-only.

Commit

Permalink
updated codeblock formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
speudusa committed Oct 20, 2022
1 parent a645140 commit cfeee98
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 78 deletions.
97 changes: 49 additions & 48 deletions src/chapters/aspdotnet-model-validation/validation-attributes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ We'll use only a few of these attributes, but you can find a full list in the `d
.. sourcecode:: csharp
:linenos:

[Required]
[StringLength(12, MinimumLength = 3)]
public string Username { get; set; }
[Required]
[StringLength(12, MinimumLength = 3)]
public string Username { get; set; }

[Reguired]
[StringLength(20, MinimumLength=6)]
public string Password { get; set; }
[Reguired]
[StringLength(20, MinimumLength=6)]
public string Password { get; set; }

Defining Validation Messages
----------------------------
Expand All @@ -64,13 +64,13 @@ Each of these attributes takes an optional ``ErrorMessage`` parameter that allow
.. sourcecode:: csharp
:linenos:

[Required(ErrorMessage = "Username is required")]
[StringLength(12, MinimumLength = 3, ErrorMessage = "Username must be between 3 and 12 characters long")]
public string Username { get; set; }
[Required(ErrorMessage = "Username is required")]
[StringLength(12, MinimumLength = 3, ErrorMessage = "Username must be between 3 and 12 characters long")]
public string Username { get; set; }

[Required(ErrorMessage = "Password is required")]
[StringLength(20, MinimumLength = 6, ErrorMessage = "Sorry, but the given password is too short. Passwords must be at least 6 characters long.")]
public string Password { get; set; }
[Required(ErrorMessage = "Password is required")]
[StringLength(20, MinimumLength = 6, ErrorMessage = "Sorry, but the given password is too short. Passwords must be at least 6 characters long.")]
public string Password { get; set; }

We will see how to ensure these error messages are properly displayed in the next section, :ref:`validating-models`.

Expand All @@ -95,13 +95,13 @@ For our ``AddEventViewModel`` class, we add ``[StringLength]`` and ``[Required]`
.. sourcecode:: csharp
:lineno-start: 8

[Required(ErrorMessage = "Name is required.")]
[StringLength(50, MinimumLength = 3, ErrorMessage = "Name must be between 3 and 50 characters.")]
public string Name { get; set; }
[Required(ErrorMessage = "Name is required.")]
[StringLength(50, MinimumLength = 3, ErrorMessage = "Name must be between 3 and 50 characters.")]
public string Name { get; set; }

[Required(ErrorMessage = "Please enter a description for your event.")]
[StringLength(500, ErrorMessage = "Description is too long!")]
public string Description { get; set; }
[Required(ErrorMessage = "Please enter a description for your event.")]
[StringLength(500, ErrorMessage = "Description is too long!")]
public string Description { get; set; }

The required ``MaximumLength`` and optional ``MinimumLength`` parameters for ``[StringLength]`` specify the maximum and minimum number of allowed characters, respectively.
Omitting the minimum length requirement means that no min or max will be applied for the field.
Expand All @@ -118,49 +118,50 @@ Thankfully, there is an ``[EmailAddress]`` validation attribute that we can appl
.. sourcecode:: csharp
:lineno-start: 16

[EmailAddress]
public string ContactEmail { get; set; }
[EmailAddress]
public string ContactEmail { get; set; }

Before we can start up our application, we need to add a new input to our form in ``Events/Add.cshtml`` to take in the contact email for
an event organizer. While we don't demonstrate these items in the video above, we cover them on the next page before tackling validation in the controller.

.. sourcecode:: html
:lineno-start: 14

<div class="form-group">
<label asp-for="ContactEmail">Contact Email</label>
<input asp-for="ContactEmail" />
</div>
<div class="form-group">
<label asp-for="ContactEmail">Contact Email</label>
<input asp-for="ContactEmail" />
</div>

We also need to add a new column to the ``Events/Index.cshtml`` template to make ``ContactEmail`` visible.

.. sourcecode:: html
:lineno-start: 20

<table class="table">
<tr>
<th>
Id
</th>
<th>
Name
</th>
<th>
Description
</th>
<th>
Contact Email
</th>
</tr>
@foreach (var evt in Model)
{
<tr>
<td>@evt.Id</td>
<td>@evt.Name</td>
<td>@evt.Description</td>
<td>@evt.ContactEmail</td>
</tr>
}
<table class="table">
<tr>
<th>
Id
</th>
<th>
Name
</th>
<th>
Description
</th>
<th>
Contact Email
</th>
</tr>

@foreach (var evt in Model)
{
<tr>
<td>@evt.Id</td>
<td>@evt.Name</td>
<td>@evt.Description</td>
<td>@evt.ContactEmail</td>
</tr>
}
</table>

Now we can start up our application and test.
Expand Down
50 changes: 25 additions & 25 deletions src/chapters/aspdotnet-model-validation/validation-controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ objects from form submissions.
.. sourcecode:: csharp
:lineno-start: 32

[HttpPost]
public IActionResult Add(AddEventViewModel addEventViewModel)
{
Event newEvent = new Event
[HttpPost]
public IActionResult Add(AddEventViewModel addEventViewModel)
{
Name = addEventViewModel.Name,
Description = addEventViewModel.Description,
ContactEmail = addEventViewModel.ContactEmail
};
Event newEvent = new Event
{
Name = addEventViewModel.Name,
Description = addEventViewModel.Description,
ContactEmail = addEventViewModel.ContactEmail
};

EventData.Add(newEvent);
EventData.Add(newEvent);

return Redirect("/Events");
}
return Redirect("/Events");
}

The flow of this request can be described as follows:

Expand Down Expand Up @@ -110,25 +110,25 @@ Once we are done refactoring the ``Add()`` action method to use ``ModelState.IsV
.. sourcecode:: csharp
:lineno-start: 32

[HttpPost]
public IActionResult Add(AddEventViewModel addEventViewModel)
{
if (ModelState.IsValid)
[HttpPost]
public IActionResult Add(AddEventViewModel addEventViewModel)
{
Event newEvent = new Event
if (ModelState.IsValid)
{
Name = addEventViewModel.Name,
Description = addEventViewModel.Description,
ContactEmail = addEventViewModel.ContactEmail
};
Event newEvent = new Event
{
Name = addEventViewModel.Name,
Description = addEventViewModel.Description,
ContactEmail = addEventViewModel.ContactEmail
};

EventData.Add(newEvent);
EventData.Add(newEvent);

return Redirect("/Events");
}
return Redirect("/Events");
}

return View(addEventViewModel);
}
return View(addEventViewModel);
}

Now we have refactored our action method to handle any errors in form submission.
However, if you submit a value that doesn't meet our conditions, you won't see any error messages indicating what was wrong with your submission.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Now when we run our application and enter a bad event name or forget our descrip
.. admonition:: Warning

In the video, we note that the ``<span>`` element will only be materialized if the validation fails. This isn't true. The ``<span>`` element
to contain the error message is always created, it just doesn't contain any text if the validation rule it met.
to contain the error message is always created, it just doesn't contain any text if the validation rule it met.

Check Your Understanding
------------------------
Expand Down
8 changes: 4 additions & 4 deletions src/chapters/aspdotnet-model-validation/viewmodels.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,23 @@ First, in ``EventsController``, we want to convert the collection of Events we h
.. sourcecode:: csharp
:lineno-start: 19

List<Event> events = new List<Event>(EventData.GetAll());
List<Event> events = new List<Event>(EventData.GetAll());

Now that we are storing our items in a ``List``, we need to import the model into our ``Events/Index.cshtml`` view so we can use the new ``events`` collection.
We can add a small statement up on line 1 to do so:

.. sourcecode:: csharp

@model List<CodingEventsDemo.Models.Event>
@model List<CodingEventsDemo.Models.Event>

Or, as we write in the video:

.. sourcecode:: csharp
:linenos:

@using CodingEventsDemo.Models
@using CodingEventsDemo.Models

@model List<Event>
@model List<Event>

Wherever we used our ``ViewBag`` property, we can now use ``Model`` syntax.
Once the view has been updated, run the application!
Expand Down

0 comments on commit cfeee98

Please sign in to comment.