move code snippets around (#129)

pull/130/head
Rick Anderson 2023-03-02 14:27:19 -10:00 committed by GitHub
parent 75c749ddce
commit 2b54120292
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 10 deletions

View File

@ -28,18 +28,24 @@ namespace ModelStateError
{
// Attach Validation Error Message to the Model on validation failure.
// <snippet_5>
if (Contact.Name == Contact.ShortName)
{
ModelState.AddModelError("Contact.ShortName",
"Short name can't be the same as Name.");
}
// </snippet_5>
if (_context.Contact.Any(i => i.PhoneNumber == Contact.PhoneNumber))
{
ModelState.AddModelError("Contact.PhoneNumber", "The Phone number is already in use.");
ModelState.AddModelError("Contact.PhoneNumber",
"The Phone number is already in use.");
}
if (_context.Contact.Any(i => i.Email == Contact.Email))
{
ModelState.AddModelError("Contact.Email", "The Email is already in use.");
}
if (Contact.Name == Contact.ShortName)
{
ModelState.AddModelError("Contact.ShortName", "Short name can't be the same as Name.");
}
if (!ModelState.IsValid || _context.Contact == null || Contact == null)
{
// if model is invalid, return the page with the model state errors.

View File

@ -55,19 +55,24 @@ public class ContactsController : Controller
public async Task<IActionResult> Create([Bind("Id,Name,ShortName,Email,PhoneNumber")] Contact contact)
{
// Attach Validation Error Message to the Model on validation failure.
// <snippet_5>
if (contact.Name == contact.ShortName)
{
ModelState.AddModelError(nameof(contact.ShortName),
"Short name can't be the same as Name.");
}
// </snippet_5>
if (_context.Contact.Any(i => i.PhoneNumber == contact.PhoneNumber))
{
ModelState.AddModelError(nameof(contact.PhoneNumber), "The Phone number is already in use.");
ModelState.AddModelError(nameof(contact.PhoneNumber),
"The Phone number is already in use.");
}
if (_context.Contact.Any(i => i.Email == contact.Email))
{
ModelState.AddModelError(nameof(contact.Email), "The Email is already in use.");
}
if (contact.Name == contact.ShortName)
{
ModelState.AddModelError(nameof(contact.ShortName), "Short name can't be the same as Name.");
}
if (ModelState.IsValid)
{
_context.Add(contact);