Na most egy validator is beköszönt
This commit is contained in:
@@ -44,6 +44,10 @@
|
||||
</div>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
||||
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.19.5/dist/jquery.validate.min.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/jquery-validation-unobtrusive@4.0.0/dist/jquery.validate.unobtrusive.min.js"></script>
|
||||
|
||||
<partial name="_ValidationScriptsPartial" />
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<html lang="hu">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
|
||||
@@ -44,8 +44,8 @@
|
||||
const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
|
||||
var formData = getFormAsNestedObject('#UserForm');
|
||||
const $form = $('#UserForm');
|
||||
console.log($form);
|
||||
if ($form[0].checkValidity())
|
||||
|
||||
if ($form.valid())
|
||||
{
|
||||
$.ajax({
|
||||
url: $('#UserEditPostUrl').val(),
|
||||
@@ -78,6 +78,22 @@
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
$(document).ready(function () {
|
||||
const $form = $("#userForm");
|
||||
|
||||
$form.validate({
|
||||
errorClass: "text-danger small",
|
||||
errorPlacement: function (error, element) {
|
||||
error.appendTo(element.parent());
|
||||
}
|
||||
});
|
||||
|
||||
// $form.on("submit", function (e) {
|
||||
// if (!$form.valid()) {
|
||||
// e.preventDefault(); // Ne küldje be, ha van hiba
|
||||
// }
|
||||
// });
|
||||
});
|
||||
</script>
|
||||
}
|
||||
@@ -8,6 +8,7 @@ using WorkFlowCheck.Common.DTO;
|
||||
using WorkFlowCheck.Web.Middleware;
|
||||
using WorkFlowCheck.Web.Services;
|
||||
using WorkFlowCheck.Web.Services.Interfaces;
|
||||
using WorkFlowCheck.Web.Validators;
|
||||
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
@@ -50,6 +51,8 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
};
|
||||
});
|
||||
|
||||
builder.Services.AddFluentValidationSetup();
|
||||
|
||||
var app = builder.Build();
|
||||
app.UseMiddleware<JwtMiddleware>();
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
using FluentValidation.AspNetCore;
|
||||
using FluentValidation;
|
||||
|
||||
namespace WorkFlowCheck.Web.Validators
|
||||
{
|
||||
public static class FluentValidationConfig
|
||||
{
|
||||
public static IServiceCollection AddFluentValidationSetup(this IServiceCollection services)
|
||||
{
|
||||
services.AddFluentValidationAutoValidation();
|
||||
services.AddFluentValidationClientsideAdapters(); // ha valaha szeretnél client-side-t is
|
||||
|
||||
// Itt regisztrálod a validátorokat – automatikusan az adott assembly-ből
|
||||
services.AddValidatorsFromAssembly(typeof(FluentValidationConfig).Assembly);
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using FluentValidation;
|
||||
using WorkFlowCheck.Common.DTO;
|
||||
|
||||
namespace WorkFlowCheck.Web.Validators
|
||||
{
|
||||
public class UserDTOValidator : AbstractValidator<UserDTO>
|
||||
{
|
||||
public UserDTOValidator()
|
||||
{
|
||||
RuleFor(x => x.UserName)
|
||||
.NotEmpty().WithMessage("A felhasználónév kötelező.");
|
||||
|
||||
RuleFor(x => x.FirstName)
|
||||
.NotEmpty().WithMessage("A keresztnév kötelező.");
|
||||
|
||||
RuleFor(x => x.LastName)
|
||||
.NotEmpty().WithMessage("A vezetéknév kötelező.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,8 @@
|
||||
<Content Remove="C:\Users\ivanszabo\.nuget\packages\datatables.net\2.2.2\contentFiles\any\any\wwwroot\js\dataTables.min.js" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FluentValidation" Version="11.11.0" />
|
||||
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
|
||||
<PackageReference Include="jquery" Version="3.7.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.14" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.11" />
|
||||
|
||||
Reference in New Issue
Block a user