From 166db82ed3bfb65e04cb37031a382af11499b2eb Mon Sep 17 00:00:00 2001 From: Jean-Daniel Lamontagne Date: Mon, 12 Sep 2022 17:14:11 -0400 Subject: [PATCH] Ajout du formulaire --- .../Controllers/HomeController.cs | 24 ++++++--- .../GrossesMitaines/GrossesMitaines.csproj | 9 +--- .../Models/RequestViewModel.cs | 35 ++++++++++++ GrossesMitaines/GrossesMitaines/Startup.cs | 3 ++ .../GrossesMitaines/Views/Home/Index.cshtml | 53 ++++++++++++++++++- 5 files changed, 109 insertions(+), 15 deletions(-) create mode 100644 GrossesMitaines/GrossesMitaines/Models/RequestViewModel.cs diff --git a/GrossesMitaines/GrossesMitaines/Controllers/HomeController.cs b/GrossesMitaines/GrossesMitaines/Controllers/HomeController.cs index 5bfe2c8..d9dacac 100644 --- a/GrossesMitaines/GrossesMitaines/Controllers/HomeController.cs +++ b/GrossesMitaines/GrossesMitaines/Controllers/HomeController.cs @@ -15,10 +15,28 @@ namespace GrossesMitaines.Controllers { _logger = logger; } + [HttpGet] public IActionResult Index() { return View(); } + // POST : LogIn + [HttpPost] + public async Task Demande(RequestViewModel model, string returnurl) + { + + if (!ModelState.IsValid) + { + return LocalRedirect(returnurl); + } + + else + { + + return LocalRedirect(returnurl); + } + } + public IActionResult Privacy() { return View(); } @@ -31,12 +49,6 @@ namespace GrossesMitaines.Controllers { return View(); } - public class ContactModel { }; // À DELETER QUAND LA CLASSE/FORMULAIRE SERONT FAIT. - [HttpPost] - public IActionResult ContactUs(ContactModel model) { - return View(model); - } - [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); diff --git a/GrossesMitaines/GrossesMitaines/GrossesMitaines.csproj b/GrossesMitaines/GrossesMitaines/GrossesMitaines.csproj index 8c293f5..701660b 100644 --- a/GrossesMitaines/GrossesMitaines/GrossesMitaines.csproj +++ b/GrossesMitaines/GrossesMitaines/GrossesMitaines.csproj @@ -5,13 +5,8 @@ - - - - - - - + + diff --git a/GrossesMitaines/GrossesMitaines/Models/RequestViewModel.cs b/GrossesMitaines/GrossesMitaines/Models/RequestViewModel.cs new file mode 100644 index 0000000..c431dcf --- /dev/null +++ b/GrossesMitaines/GrossesMitaines/Models/RequestViewModel.cs @@ -0,0 +1,35 @@ +using FluentValidation; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Threading.Tasks; + +namespace GrossesMitaines.Models +{ + public class RequestViewModel + { + public string Name { get; set; } + public string Email { get; set; } + public string Phone { get; set; } + public string Message { get; set; } + } + + public class RequestValidator : AbstractValidator + { + public RequestValidator() + { + RuleFor(x => x.Name) + .NotEmpty().WithMessage("Vous devez entrer votre nom") + .MinimumLength(2).WithMessage("Votre nom doit être de 2 lettres minimum"); + + RuleFor(x => x.Email) + .NotEmpty().WithMessage("Un Email est requis") + .Matches("^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$").WithMessage("Format non valide!"); + + RuleFor(x => x.Phone) + .NotEmpty().WithMessage("Vous devez entrer votre téléphone") + .Matches("^[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]$").WithMessage("Téléphone non valide!"); + } + } +} diff --git a/GrossesMitaines/GrossesMitaines/Startup.cs b/GrossesMitaines/GrossesMitaines/Startup.cs index 790be94..c69f932 100644 --- a/GrossesMitaines/GrossesMitaines/Startup.cs +++ b/GrossesMitaines/GrossesMitaines/Startup.cs @@ -1,3 +1,4 @@ +using FluentValidation.AspNetCore; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpsPolicy; @@ -19,6 +20,8 @@ namespace GrossesMitaines { public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); + + services.AddFluentValidation(option => option.RegisterValidatorsFromAssemblyContaining()); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { diff --git a/GrossesMitaines/GrossesMitaines/Views/Home/Index.cshtml b/GrossesMitaines/GrossesMitaines/Views/Home/Index.cshtml index 561d25b..3a2fb71 100644 --- a/GrossesMitaines/GrossesMitaines/Views/Home/Index.cshtml +++ b/GrossesMitaines/GrossesMitaines/Views/Home/Index.cshtml @@ -1,4 +1,6 @@ -@{ +@model GrossesMitaines.Models.RequestViewModel + +@{ ViewData["Title"] = "Maison"; } @@ -18,4 +20,51 @@


- \ No newline at end of file + + +
+
+
+
+

Demander une renconte!

+
+
+ + + +
+
+
+
+ + + +
+
+
+
+ + + +
+
+
+
+ + + +
+
+
+
+ +
+
+
+
+
+
+ +@section Scripts { + @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } +} \ No newline at end of file