The Form widget is rendered using a combination of the following view structure and the view model FormWidgetViewModel
:
using (Ajax.Kentico().BeginForm( ... ))
{
@Html.AntiForgeryToken()
@Html.Kentico().FormFields(Model.FormComponents, Model.FormConfiguration, FormFieldRenderingConfiguration.Widget)
// Render Model.SubmitButtonImage using @Html.Kentico().ImageInput( ... )
// Or render a plain <input> using Model.SubmitButtonText
}
If you have the BizFormInfo
object for the form, it is needed for the following properties:
new FormWidgetViewModel
{
FormName = formInfo.FormName,
FormConfiguration = IFormBuilderConfigurationRetriever.Retrieve(formInfo),
FormComponents = IFormProvider.GetFormComponents(formInfo).GetDisplayedComponents( ... ),
FormPrefix = // This may be optional outside of the Page Builder context,
SubmitButtonText = formInfo.FormSubmitButtonText,
SubmitButtonImage = formInfo.FormSubmitButtonImage
}
Inside Ajax.Kentico().BeginForm
you can pass in the controller and action handling the form.
Use methods in IFormProvider
to update or add the form submission and to send emails.
Update (see comments):
IFormBuilderConfigurationRetriever
is marked internal
, so it is not directly accessible. Its implementation in turn uses IFormBuilderConfigurationSerializer
to deserialize formInfo.FormBuilderLayout
. That interface is also marked internal
. Further, the implementation of that interface uses the internal
FormBuilderTypesBinder
.
This means that there is no API available to retrieve Model.FormConfiguration
. As of Kentico 12.0.16, you would need to recreate the internal functionality. The basic implementation is like this:
JsonConvert.DeserializeObject<FormBuilderConfiguration>(formInfo.FormBuilderLayout, new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
TypeNameHandling = TypeNameHandling.Auto,
SerializationBinder = // Set to the internal FormBuilderTypesBinder, which validates only known form builder types
StringEscapeHandling = StringEscapeHandling.EscapeHtml
});