README.md 3.21 KB
Newer Older
Sinan Bozkuş's avatar
Sinan Bozkuş committed
1
# Form Helper
Sinan Bozkuş's avatar
Sinan Bozkuş committed
2

Sinan Bozkuş's avatar
Sinan Bozkuş committed
3 4
Form & Validation Helper for **ASP.NET Core**

Sinan Bozkuş's avatar
Sinan Bozkuş committed
5
Form Helper helps you to create ajax forms and validations without writing any javascript code. **It transforms server-side validations to client-side.** You can also use the form validator without ajax.
Sinan Bozkuş's avatar
Sinan Bozkuş committed
6

Sinan Bozkuş's avatar
Sinan Bozkuş committed
7 8 9
**Compatible with Fluent Validation**
(You can add client-side validation support to Fluent Validation.)

Sinan Bozkuş's avatar
Sinan Bozkuş committed
10
[![NuGet](https://img.shields.io/nuget/v/FormHelper.svg)](https://nuget.org/packages/FormHelper) [![Nuget](https://img.shields.io/nuget/dt/FormHelper.svg)](https://nuget.org/packages/FormHelper)
Sinan Bozkuş's avatar
Sinan Bozkuş committed
11

Sinan Bozkuş's avatar
Sinan Bozkuş committed
12 13 14 15
<p align="center">
<img src="http://www.sinanbozkus.com/nuget/formhelper/formhelper-screenshot.png" alt="FormHelper" />
</p>

Sinan Bozkuş's avatar
Sinan Bozkuş committed
16
## Installation
Sinan Bozkuş's avatar
Sinan Bozkuş committed
17 18 19 20 21 22 23 24 25

FormHelper can be installed using the *Nuget Package Manager* or the *dotnet CLI*.

Package Manager:
```
Install-Package FormHelper
```

dotnet CLI:
Sinan Bozkuş's avatar
Sinan Bozkuş committed
26
```csharp
Sinan Bozkuş's avatar
Sinan Bozkuş committed
27 28 29
dotnet add package FormHelper
```

Sinan Bozkuş's avatar
Sinan Bozkuş committed
30
This library depends on some packages:
Sinan Bozkuş's avatar
Sinan Bozkuş committed
31
- [jQuery Validation Unobtrusive](https://github.com/aspnet/jquery-validation-unobtrusive)
Sinan Bozkuş's avatar
Sinan Bozkuş committed
32
- [Toastr](https://github.com/CodeSeven/toastr)
Sinan Bozkuş's avatar
Sinan Bozkuş committed
33

Sinan Bozkuş's avatar
Sinan Bozkuş committed
34
CDN:
Sinan Bozkuş's avatar
Sinan Bozkuş committed
35
```html
Sinan Bozkuş's avatar
Sinan Bozkuş committed
36 37 38 39
// css
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.min.css" />

// js
Sinan Bozkuş's avatar
Sinan Bozkuş committed
40 41 42 43
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-validation-unobtrusive/3.2.11/jquery.validate.unobtrusive.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script>

Sinan Bozkuş's avatar
Sinan Bozkuş committed
44 45
<!-- form helper - You don't need to add this file to your project, just add it. it's embeded! -->
<script src="/formhelper/formhelper.min.js"></script>
Sinan Bozkuş's avatar
Sinan Bozkuş committed
46 47 48 49
```

## Usage

Sinan Bozkuş's avatar
Sinan Bozkuş committed
50
**Startup.cs**
Sinan Bozkuş's avatar
Sinan Bozkuş committed
51 52

ConfigureServices:
Sinan Bozkuş's avatar
Sinan Bozkuş committed
53 54 55
```
services.AddFormHelper();
```
Sinan Bozkuş's avatar
Sinan Bozkuş committed
56
*With configuration: (optional)*
Sinan Bozkuş's avatar
Sinan Bozkuş committed
57 58 59 60
```
services.AddFormHelper(new FormHelperConfiguration
{
    CheckTheFormFieldsMessage = "Your custom message...",
Sinan Bozkuş's avatar
Sinan Bozkuş committed
61
    RedirectDelay = 6000
Sinan Bozkuş's avatar
Sinan Bozkuş committed
62 63
});
```
Sinan Bozkuş's avatar
Sinan Bozkuş committed
64 65
Configure:
```
Sinan Bozkuş's avatar
Sinan Bozkuş committed
66
<!-- If you want to use embeded form helper script file, add this line -->
Sinan Bozkuş's avatar
Sinan Bozkuş committed
67 68 69
app.UseFormHelper();
```

Sinan Bozkuş's avatar
Sinan Bozkuş committed
70 71 72 73 74
**ViewImports.cshtml**
```csharp
@using FormHelper
@addTagHelper *, FormHelper
```
Sinan Bozkuş's avatar
Sinan Bozkuş committed
75 76 77 78


**View: (TagHelper)**
```csharp
Sinan Bozkuş's avatar
Sinan Bozkuş committed
79
<form asp-formhelper="true" asp-controller="Home" asp-action="Save">
Sinan Bozkuş's avatar
Sinan Bozkuş committed
80 81
   // <input...
   // ...
Sinan Bozkuş's avatar
Sinan Bozkuş committed
82
</form>
Sinan Bozkuş's avatar
Sinan Bozkuş committed
83

Sinan Bozkuş's avatar
Sinan Bozkuş committed
84
// You can use <form asp-formhelper="true"> or <formhelper> to activate formhelper.
Sinan Bozkuş's avatar
Sinan Bozkuş committed
85 86 87
// Optional parameters:
// asp-callback="...", asp-beforeSubmit="...", asp-dataType="FormData/Json", asp-enableButtonAfterSuccess="False"
```
Sinan Bozkuş's avatar
Sinan Bozkuş committed
88

Sinan Bozkuş's avatar
Sinan Bozkuş committed
89
**Controller:**
Sinan Bozkuş's avatar
Sinan Bozkuş committed
90
```csharp
Sinan Bozkuş's avatar
Sinan Bozkuş committed
91 92
[HttpPost, FormValidator]
public IActionResult Save(FormViewModel viewModel)
Sinan Bozkuş's avatar
Sinan Bozkuş committed
93 94 95

// If you use Json data type, you need to add [FromBody] attribute.
// public IActionResult Save([FromBody]FormViewModel viewModel)
Sinan Bozkuş's avatar
Sinan Bozkuş committed
96
```
Sinan Bozkuş's avatar
Sinan Bozkuş committed
97

Sinan Bozkuş's avatar
Sinan Bozkuş committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111
**Return a result from Controller:**

Error Message:
```
return FormResult.CreateErrorResult("An error occured.");
```
Success Message:
```
return FormResult.CreateSuccessResult("Product saved.");
```
Success Message with Redirect:
```
return FormResult.CreateSuccessResult("Product saved. Please wait...", Url.Action("Home", "Index"));
```
Sinan Bozkuş's avatar
Sinan Bozkuş committed
112 113 114 115
Success Message with Redirect and Delay Time:
```
return FormResult.CreateSuccessResult("Product saved. Please wait...", Url.Action("Home", "Index"), 10000); // 10 seconds
```