• Stars
    star
    255
  • Rank 159,729 (Top 4 %)
  • Language
    C#
  • License
    MIT License
  • Created about 12 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Shape-shifting your forms experience in ASP.NET Core MVC

Chameleon Forms logo

ChameleonForms

Build status NuGet version docs | up to date

ChameleonForms takes away the pain and repetition of building forms with ASP.NET Core MVC by following a philosophy of:

  • Model-driven defaults - Spend less time with tedious reptition by letting your view models do the hard work for you. (e.g. enum is drop-down or radio list, collection/array of enums is a multi-select drop-down or checkbox list, [DataType(DataType.Password)] is password textbox, int is a number textbox with a step of 1, etc.).
  • Extend the best of ASP.NET Core MVC - makes use of HTML generation, client validation and model binding, but makes them work the way you'd expect in more scenarios so you spend more time pumping out business value and less time fighting and patching gaps in MVC.
  • DRY up your forms - your forms will be quicker to write and easier to maintain and you won't get stuck writing the same form boilerplate markup form after form after form.
  • Consistent - consistency of the (ChameleonForms) API and form structure within your forms and consistency across all forms in your site via templating. This will make your forms easier to maintain and have a better user experience.
  • Declarative syntax - specify how the form is structured rather than worrying about the boilerplate HTML markup of the form.
  • Beautiful, terse, fluent APIs - it's a pleasure to read and write the code.
  • Extensible and flexible core - you can extend or completely change anything you want at any layer of ChameleonForms and you can drop out to plain HTML at any point in your form for those moments where pre-prepared field types and templates just don't cut it.

It's ideally suited for situations where you want to quickly build forms that are highly consistent and maintainable. If you are trying to build highly specialised forms that are individually, painstakingly crafted then that's not what this library is for. That's where it makes sense to break out your JavaScript library of choice.

Find out more about why we created ChameleonForms and the advantages it gives you or check out the documentation.

Contents

Getting started

Prerequisites

This library works against netcoreapp3.1. If you are using a different version of .NET Core or are running ASP.NET Core against Full Framework then feel free to raise an issue to discuss opening up broader support. If you are using ASP.NET MVC 5 then check out v3.0.3 of the NuGet package and documentation.

This library works against ASP.NET Core MVC - if you want to use it for Blazor or Razor Pages then feel free to raise an issue to discuss.

Getting it running

  1. Install the NuGet package Install-Package ChameleonForms -pre (v4 is currently marked beta so you need to include pre-release versions)

  2. Register ChameleonForms in your Startup.cs file:

    public void ConfigureServices(IServiceCollection services)
    {
        ...
        services.AddMvc(...);
        ...
        services.AddChameleonForms();
    }

    Note: you can alter the configuration from the default, see the docs.

  3. Add the following to your _ViewImports.cshtml:

    @using ChameleonForms;
    @using ChameleonForms.Enums;
    @using ChameleonForms.Component;
    
    @addTagHelper ChameleonForms.TagHelpers.*, ChameleonForms
    
    @* optional: *@
    @addTagHelper ChameleonForms.Templates.TwitterBootstrap3.*, ChameleonForms
  4. Create your first form, e.g.:

    ~/Controllers/MyFormController.cs:

    using System;
    using System.ComponentModel.DataAnnotations;
    using Microsoft.AspNetCore.Mvc;
    
    namespace MyWebApp.Controllers
    {
        public class MyFormViewModel
        {
            [Required]
            public string Name { get; set; }
    
            public int FavouriteNumber { get; set; }
    
            [DisplayFormat(DataFormatString = "{0:d/M/yyyy}", ApplyFormatInEditMode = true)]
            public DateTime DateOfBirth { get; set; }
        }
        public class MyFormController : Controller
        {
            public IActionResult Index()
            {
                return View();
            }
    
            [HttpPost]
            public IActionResult Index(MyFormViewModel vm)
            {
                if (ModelState.IsValid)
                {
                    // Do stuff
                    return RedirectToAction("Index");
                }
                return View(vm);
            }
        }
    }

    ~/Views/MyForm/Index.cshtml:

    You have two options for your view - tag helper syntax or the more traditional HTML helper syntax.

    Tag Helpers variant

    @model MyWebApp.Controllers.ViewModel
    @{
        ViewData["Title"] = "My Form";
    }
    
    <chameleon-form>
        <form-section heading="About you!?">
            <field for="Name" />
            <field for="FavouriteNumber" />
            <field for="DateOfBirth" />
        </form-section>
        <form-navigation>
            <submit-button label="Submit" />
        </form-navigation>
    </chameleon-form>
    
    @section Scripts
    {
        <partial name="_ValidationScriptsPartial" />
        @* ... or relevant equivalent *@
    }

    HTML Helpers variant

    @model MyWebApp.Controllers.ViewModel
    @{
        ViewData["Title"] = "My Form";
    }
    
    @using (var f = Html.BeginChameleonForm())
    {
        using (var s = f.BeginSection("About you!?"))
        {
            @s.FieldFor(m => m.Name)
            @s.FieldFor(m => m.FavouriteNumber)
            @s.FieldFor(m => m.DateOfBirth)
        }
        using (var n = f.BeginNavigation())
        {
            @n.Submit("Submit")
        }
    }
    
    @section Scripts
    {
        <partial name="_ValidationScriptsPartial" />
        @* ... or relevant equivalent *@
    }
  5. Run it!

  6. (Optional) If you want to add the additional client-side validation support in ChameleonForms (which supports both jquery validate unobtrusive validation and aspnet-validation) then add the following to your _ValidationScriptsPartial.cshtml or equivalent file:

    <script src="~/lib/chameleonforms/unobtrusive-date-validation.chameleonforms.js" asp-append-version="true"></script>
  7. (Optional) If you are using Twitter Bootstrap 3 then add the following to your _ValidationScriptsPartial.cshtml (which only supports jquery validate unobtrusive validation for now):

    <script src="~/lib/chameleonforms/unobtrusive-twitterbootstrap3-validation.chameleonforms.js" asp-append-version="true"></script>

    And add the following to your _Layout.cshtml or equivalent file:

    <link href="~/lib/chameleonforms/chameleonforms-twitterbootstrap3.css" rel="stylesheet" type="text/css" asp-append-version="true" />

Contributors

Core Team

Other contributors

The core team would like to thank everyone that has contributed code to this project! Check out the contributors graph to see them :). Thanks to Jason Roberts for the logo.

Contributing

If you would like to contribute to this project then feel free to communicate with us via Twitter @robdmoore / @mdaviesnet or alternatively send a pull request / issue to this GitHub project.

Continuous Integration

We have a continuous integration build in AppVeyor that automatically builds and runs tests when we push/merge to master as well as all pull requests and generates the NuGet packages that we can publish to NuGet.org at the click of a button. This is implemented using the MRCollective AppVeyor yml.

Documentation

We are using DocFX to generate the documentation. The documentation consists of three parts:

  1. The shiny homepage
  2. The main documentation area, which is generated off a bunch of Markdown files
  3. The API documentation, which is auto-generated from the xmldoc comments in the source code

To see the documentation:

  1. choco install docfx (Windows); brew install docfx (Mac); Other
  2. > docfx docfx.json
  3. The documentation site will be running at http://localhost:8080/

The key files to look for to modify the documentation are:

  1. docfx.json - DocFX config
  2. toc.yml - Table of contents
  3. index.md - Homepage
  4. docs/templates/chameleonforms/*.* - Template
  5. docs/*.* - Documentation files

Code coverage

To see code coverage, ensure you have ReportGenerator installed:

> dotnet tool install -g dotnet-reportgenerator-globaltool

And then run:

rmdir ChameleonForms.Tests/TestResults -Force
dotnet test --collect:"XPlat Code Coverage" --settings ChameleonForms.Tests/coverlet.runsettings ChameleonForms.Tests/ChameleonForms.Tests.csproj
reportgenerator "-reports:ChameleonForms.Tests/TestResults/**/coverage.cobertura.xml" "-targetdir:ChameleonForms.Tests/TestResults/report"

If you are using VSCode, then the first command can be achieved using the test with coverage task.

Support

If you need to raise an issue or check for an existing issue, see https://github.com/MRCollective/ChameleonForms/issues.

Roadmap

To get an idea of where ChameleonForms is heading in the future check out the roadmap in the documentation.

More Repositories

1

NHibernate.SqlAzure

Reliable NHibernate connection to SQL Azure using the Microsoft Transient Fault Handling library.
C#
37
star
2

AspNet.Mvc.Grid

The Grid control from mvccontrib.codeplex.com updated for ASP.NET MVC5 and without dependencies.
C#
29
star
3

AutofacContrib.NSubstitute

Automocking container for Autofac using NSubstitute
C#
26
star
4

nextjs-server-azuresiteextension

An Azure Site Extension for hosting Next.js apps built using `next build` where you just need to deploy the .next folder.
PowerShell
25
star
5

AzureWebFarm.OctopusDeploy

Scalable, OctopusDeploy-powered webfarm using Windows Azure Web Roles.
C#
25
star
6

TortoiseGitToolbar

Lightweight toolbar for launching commonly used TortoiseGit functionality from within Visual Studio.
C#
22
star
7

repave.psm1

A PowerShell module that allows you to easily create a terse re-pave script for a Windows Machine making heavy use of Chocolatey.
PowerShell
21
star
8

ReliableDbProvider

Provides a Db Provider Factory that uses the Microsoft Transient Fault Handling library to allow for reliable SQL Azure connections when using Entity Framework 4, Entity Framework 5 or Linq 2 SQL.
C#
20
star
9

MSBuildRazorCompiler

.NET Core 3.1 compatible MSBuild task that will automatically compile all `.cshtml` files into `.cs` files in-place.
C#
17
star
10

MiniProfiler.NHibernate

NHibernate drivers supporting logging to MiniProfiler
C#
14
star
11

ModernAuthPresentation

Presentation about modern authentication
12
star
12

CDPipeline_MicrosoftAzureGlobalBootcampPerth2016

Workshop to create a continuous delivery pipeline using Azure and VSTS.
PowerShell
11
star
13

MiniProfiler.DeepProfiling.Autofac

Automatically supports profiling all methods called on interfaces resolved from an Autofac IoC container.
C#
8
star
14

MicrotestingPresentation

Presentation about microtesting
C#
5
star
15

WindowsAzureBootcampPerth2014

Sessions for the 2014 Global Windows Azure Bootcamp in Perth by Rob Moore and Matt Davies.
C#
4
star
16

DotNETContinuousDeploymentScripts

A set of MSBuild and PowerShell scripts for building and deploying ASP.NET applications and TopShelf Windows services.
PowerShell
4
star
17

WindowsAzure.Storage.Services

This library extends the Windows Azure Storage library by Microsoft with a set of services and a factory that makes it easy to construct your application in a testable and DRY manner
C#
3
star
18

EntityEncryption

Libraries to transparently handle encryption for properties on data entities. Currently supports AES encryption with NHibernate.
C#
2
star
19

AzureTraceListeners

C#
2
star
20

AzureResourceManager_MicrosoftAzureSaturdayPerth2015

C#
1
star
21

NugetDirSync

A simple windows service to monitor a folder for filesystem changes, and upload a nuget package automatically with the contents when they change.
C#
1
star