Commit e0839a9f authored by Yevgeniy Agrafenin's avatar Yevgeniy Agrafenin

#3 Добавил валидацию.

parent 1e11eb19
using CafeCritic.Context;
using Microsoft.AspNetCore.Mvc;
namespace CafeCritic.Controllers;
public class ValidationController : Controller
{
private readonly CafeDbContext _db;
public ValidationController(CafeDbContext db)
{
_db = db;
}
public bool CheckEmail(string email) => !_db.Users.Any(u => u.Email == email);
public bool CheckCafeTitle(string cafeTitle) => !_db.Cafes.Any(u => u.Title == cafeTitle);
}
\ No newline at end of file
...@@ -6,6 +6,7 @@ namespace CafeCritic.ViewModels; ...@@ -6,6 +6,7 @@ namespace CafeCritic.ViewModels;
public class AddCafeViewModel public class AddCafeViewModel
{ {
[Required(ErrorMessage = "Поле обязательно")] [Required(ErrorMessage = "Поле обязательно")]
[Remote("CheckCafeTitle", "Validation", ErrorMessage = "Заведение с такмим названием уже существуе")]
public required string Title { get; set; } public required string Title { get; set; }
[Required(ErrorMessage = "Поле обязательно")] [Required(ErrorMessage = "Поле обязательно")]
......
using CafeCritic.Models;
namespace CafeCritic.ViewModels;
public class IndexViewModel
{
public IEnumerable<Cafe> Cafes { get; set; }
public PageViewModel PageViewModel { get; set; }
public int Id { get; set; }
public string Title { get; set; }
public string Image { get; set; }
public string? Description { get; set; }
}
\ No newline at end of file
...@@ -7,6 +7,7 @@ public class RegisterViewModel ...@@ -7,6 +7,7 @@ public class RegisterViewModel
{ {
[Required(ErrorMessage = "Для регистрации введите Email")] [Required(ErrorMessage = "Для регистрации введите Email")]
[DataType(DataType.EmailAddress)] [DataType(DataType.EmailAddress)]
[Remote("CheckEmail", "Validation", ErrorMessage = "Пользователь с таким эл. адресом уже существует")]
public string Email { get; set; } public string Email { get; set; }
[Required(ErrorMessage = "Введите пароль")] [Required(ErrorMessage = "Введите пароль")]
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
<form asp-action="Register" method="post" asp-antiforgery="true"> <form asp-action="Register" method="post" asp-antiforgery="true">
<div class="validation" asp-validation-summary="All"></div> <div class="validation" asp-validation-summary="All"></div>
<div class="mb-3"> <div class="mb-3">
<input asp-for="Email" class="form-control" placeholder="Email"> <input id="email" asp-for="Email" class="form-control" placeholder="Email">
<span asp-validation-for="Email" class="text-danger"></span> <span asp-validation-for="Email" class="text-danger"></span>
</div> </div>
<div class="mb-3"> <div class="mb-3">
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
<div asp-validation-summary="ModelOnly"></div> <div asp-validation-summary="ModelOnly"></div>
<div> <div>
<span asp-validation-for="Title" class="text-danger"></span> <span asp-validation-for="Title" class="text-danger"></span>
<input id="restaurantTitle" asp-for="Title" class="form-control my-2 text-muted" placeholder="Название заведения"> <input id="cafeTitle" asp-for="Title" class="form-control my-2 text-muted" placeholder="Название заведения">
</div> </div>
<div> <div>
<span asp-validation-for="Image" class="text-danger"></span> <span asp-validation-for="Image" class="text-danger"></span>
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
</div> </div>
</div> </div>
</div> </div>
@section Scripts @section Scripts
{ {
@{ @{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment