mcintyre321 / FormFactory

MVC5, Core or standalone - Generate rich HTML5 forms from your ViewModels, or build them programatically
http://formfactoryaspmvc.azurewebsites.net/
MIT License
304 stars 102 forks source link

Looking a way to extend FormFactory functionalities #106

Closed michael-ye closed 3 years ago

michael-ye commented 3 years ago

We are looking for a "postback" kind of functionality on form field change event.

I can make this work by adding a property to the ProjectVM

//Adding a DoPostback property to the PropertyVM public class PropertyVM : IHasDisplayName { ... ... public string DoPostback {get; set;} }

And modifying the FormFactory rendering razor view classes, for example in Property.System.String.cshtml, modifying the following line of code and adding onchange attribute:

   <select name="@Model.Name" onchange="@Model.DoPostBack" class="form-control" @Html.Raw(Model.Readonly()) @Html.Raw(Model.Disabled()) @Html.Raw(isRequired ? "required" : "")>

Then, in the the asp.net core razor view file, handle the DoPostBack with javascript:

@using FormFactory

@model List<PropertyVm>

<div class="row">
    <div class="span1">&nbsp;</div>
    <div class="col-md-5">
        <form name="FFDemoForm" asp-area="Assessments" asp-controller="Assessment" asp-action="save" method="post">

            @Model.Render(Html)

            <input type="submit" value="submit" />
        </form>

    </div>
</div>

<script>
    function DoPostBack() {

        alert("Popup On Change demo - you are about to trigger an onchange event and doing  postback.");

        document.FFDemoForm.action = "/Assessments/Assessment/Postback";
        document.FFDemoForm.submit();

    };
</script>

If a PropertyVM with DoPostBack property set, at changes, the view will popup and then do a Postback. This works but I am looking for advice on how we can do this without directly changing FormFactory. Is there a way that we can extend FF and accomplish what we need?

mcintyre321 commented 3 years ago

You can avoid modifying the PropertyVM type by using a creating a DoPostbackAttribute and putting that on your property. The PropertyVm exposes the custom attributes, which you can then use in the razor view.

Does that solve the problem?

On Thu, 27 May 2021 at 16:56, michael-ye @.***> wrote:

We are looking for a "postback" kind of functionality on form field change event.

I can make this work by adding a property to the ProjectVM

//Adding a DoPostback property to the PropertyVM public class PropertyVM : IHasDisplayName { ... ... public string DoPostback {get; set;} }

And modifying the FormFactory rendering razor view classes, for example in Property.System.String.cshtml, modifying the following line of code and adding onchange attribute:

<select name="@Model.Name" onchange="@Model.DoPostBack" class="form-control" @Html.Raw(Model.Readonly()) @Html.Raw(Model.Disabled()) @Html.Raw(isRequired ? "required" : "")>

Then, in the the asp.net core razor view file, handle the DoPostBack with javascript:

@using https://github.com/using FormFactory

@model https://github.com/model List

    @Model.Render(Html)

    <input type="submit" value="submit" />

</form>

If a PropertyVM with DoPostBack property set, at changes, the view will popup and then do a Postback. This works but I am looking for advice on how we can do this without directly changing FormFactory. Is there a way that we can extend FF and accomplish what we need?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/mcintyre321/FormFactory/issues/106, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACDJ6X5BJFCWJGSVOTPOSDTPZTTLANCNFSM45UTSO6A .

michael-ye commented 3 years ago

Thanks, I think this will solve the problem of modifying PropertyVm.

Can you suggest how to avoid modifying Property.System.String.cshtml (adding the onchange attribute) and all other types?

michael-ye commented 3 years ago

Answer my own question here. In FormFactory project, there is a _ReadMe.txt file under the folder Views/Shared/FormFactory which indicates that we can modify these view files and override the ones that would be loaded from the FormFactory assembly.

I think this would work for us.

mcintyre321 commented 3 years ago

Yes, you should be able to add your own views and override the defaults. I haven't actually tried it for a few .net core versions though...

On Thu, 27 May 2021, 17:24 michael-ye, @.***> wrote:

Answer my own question here. In FormFactory project, there is a _ReadMe.txt file under the folder Views/Shared?FormFactory which indicates that we can modify these view files and override the ones that would be loaded from the FormFactory assembly.

I think this would work for us.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/mcintyre321/FormFactory/issues/106#issuecomment-849769234, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACDJ6WPBOANIFRQURNVV73TPZW3BANCNFSM45UTSO6A .

michael-ye commented 3 years ago

This is working on asp.net core 3.1

Thank you for the great work. It has made our work a lot simpler!