Flat Forms: FormBuilder - ProCodersPtyLtd/MasterDetailsDataEntry GitHub Wiki

Class FormBuilder

Namespace:

using Platz.SqlForms;

Declaration:

public class FormBuilder

FormBuilder class is used to organize the fluent flow definition of a Dynamic Form.

'Entity' - method

public virtual FormBuilder Entity<TEntity>([NotNullAttribute] Action<FormEntityTypeBuilder<TEntity>> buildAction) where TEntity : class

This method should be used to supply the Entity Framework entity and its property definitions. The method accepts the TEntity template parameter. The method accepts buildAction parameter (not null). It is supposed to be a lambda expression (see example).

The return type is FormBuilder, and you can add execution of other FormBuilder methods to the chain, for example:

        protected override void Define(FormBuilder builder)
        {
            builder.Entity<Department>(
            // lambda expression
            e =>
            {
                e.Property(p => p.DepartmentId).IsFilter().IsUnique();
            })
            // another method in the chain
            .IsMaster();
        }

IsMaster - method (Inline Editing only)

public virtual FormBuilder IsMaster()

This method tells Platz.SqlForms Inline Editing framework that the last defined entity is a Master entity, the framework will recognize it as Master when rendering FormMasterDetailsComponent razor control.

This method is supposed to be used only after the Entity<TEntity> method in one chain, see the example above.

Class FormEntityTypeBuilder<TEntity>

Namespace:

using Platz.SqlForms;

Declaration:

public class FormEntityTypeBuilder<TEntity> : FormEntityTypeBuilder where TEntity : class

FormEntityTypeBuilder is used for defining particular properties in the Entity.

AfterSave - method

public virtual FormEntityTypeBuilder<TEntity> AfterSave(Action<TEntity, DataOperationArgs> method)

Allows to provide delegate that will be executed right after successful save operation to the database

        protected override void Define(DynamicFormBuilder builder)
        {
            builder.Entity<Address>(e =>
            {
                ...
                e.AfterSave(SaveToCustomerAddress);
            });
        }

        public void SaveToCustomerAddress(Address model, DataOperationArgs args)
        {
            if (args.Operation == DataOperations.Insert)
            {
                int customerId = (int)args.Parameters[0];
                var item = new CustomerAddress { AddressId = model.AddressId, CustomerId = customerId, ModifiedDate = DateTime.Now, AddressType = "Test" };
                args.DbContext.Add(item);
                args.DbContext.SaveChanges();
            }
        }

ContextButton - method (List Form only)

public virtual FormEntityTypeBuilder<TEntity> ContextButton(string buttonText, string actionLinkText)

Allows to define a context menu for List Forms inherited from DataServiceBase

This method accepts parameters:

  • string buttonText - string that is shown in the menu item link
  • string actionLinkText - navigation link with format parameters, for example: "CustomerAddrEdit/{0}/{1}", where Parameter {0} is always PrimaryKey, parameters {1} and above - Filter Keys

It returns FormEntityTypeBuilder<TEntity>, so many definitions can be chained in one line:

e.ContextButton("Edit", "EnrollmentEdit/{0}/{1}").ContextButton("Delete", "EnrollmentDelete/{0}/{1}");

DialogButton - method

public virtual FormEntityTypeBuilder<TEntity> DialogButton(ButtonActionTypes actionType, string buttonText = null, string hint = null, string actionLinkText = null)
public virtual FormEntityTypeBuilder<TEntity> DialogButton(string actionLinkText, ButtonActionTypes actionType, string buttonText = null, string hint = null)

Allows to define a dialog button located at the bottom of UI component for List Forms (DataServiceBase) and Dynamic Edit forms (DynamicEditFormBase)

This method accepts parameters:

  • ButtonActionTypes actionType - button action from enum (Submit, Add, Delete, Cancel, Close, Validate, Custom)
  • string buttonText - string that is shown in the button component
  • string hint - string that is shown when mouse moved to the button component
  • string actionLinkText - navigation link with format parameters, for example: "CustomerAddrEdit/{0}/{1}", where Parameter {0} is always PrimaryKey, parameters {1} and above - Filter Keys

It returns FormEntityTypeBuilder<TEntity>, so many definitions can be chained in one line:

e.DialogButton(ButtonActionTypes.Cancel).DialogButton(ButtonActionTypes.Validate).DialogButton(ButtonActionTypes.Submit);

DialogButtonNavigation - method

public virtual FormEntityTypeBuilder<TEntity> DialogButtonNavigation(string actionLinkText, params ButtonActionTypes[] actions)

Allows to specify one Action Link for a group of Dialog Buttons

This method accepts parameters:

  • string actionLinkText - navigation link with format parameters, for example: "CustomerAddrEdit/{0}/{1}", where Parameter {0} is always PrimaryKey, parameters {1} and above - Filter Keys
  • params ButtonActionTypes[] actions - a list of action types from ButtonActionTypes enum (Submit, Add, Delete, Cancel, Close, Validate, Custom)

It returns FormEntityTypeBuilder<TEntity>, so many definitions can be chained in one line

e.DialogButtonNavigation("CustAddrList/{1}", ButtonActionTypes.Delete, ButtonActionTypes.Cancel);
e.DialogButtonNavigation("CustAddrEditSuccess/{1}", ButtonActionTypes.Submit);

InlineButton - method (Inline Editing only)

public virtual void InlineButton(string buttonText, string hint = null)

This method lets adding a custom button to the Entity is being defined. The custom button will be rendered in the Action buttons section on the right of the dynamic table (Inline Editing only).

This method accepts two string parameters: buttonText and hint. The second parameter is optional

It doesn't return value and should be used like that:

        protected override void Define(FormBuilder builder)
        {
            builder.Entity<Department>(
            // lambda expression
            e =>
            {
                ...
                e.InlineButton("Details", "Details...");
            });
        }

ExcludeAll - method

public virtual void ExcludeAll()

Removes all property definitions from Form definition, only properties mentioned explicitly will be included to the Form definition

In the following example only AddressId and CustomerId properties will be shown on the page

            builder.Entity<CustomerAddress>(e =>
            {
                e.ExcludeAll();
                e.Property(p => p.AddressId).IsPrimaryKey();
                e.Property(p => p.CustomerId).IsFilter().IsReadOnly();
            });

Property<TProperty> - method

public virtual FieldBuilder<TProperty> Property<TProperty>([NotNullAttribute] Expression<Func<TEntity, TProperty>> propertyExpression)

This method is used to specify an entity property that is being defined.

It accepts a lambda expression parameter, specifying a property in the entity (p => p.DepartmentId).

This method is used in the entity lambda expression (e =>):

            builder.Entity<Department>(
            // lambda expression
            e =>
            {
                e.Property(p => p.DepartmentId).IsFilter().IsUnique();
            })

Thus DepartmentId property is specified in Department entity.

The returned value is FieldBuilder<TProperty> that allows adding a field attribute chain.

Class FieldBuilder<TProperty>

Namespace:

using Platz.SqlForms;

Declaration:

Base class:

public class FieldBuilder

and the template class:

public class FieldBuilder<TProperty> : FieldBuilder

FieldBuilder<TProperty> class lets specifying a chain of simple attributes that programmer adds to the entity property to define the property behavior in the Dynamic Form.

The simple attributes added as a chain of methods, that is possible because all of them return FieldBuilder<TProperty>. The available methods are:

Method Declaration Description
IsPrimaryKey IsPrimaryKey(bool pk = true) marks property as a Primary Key if it cannot be retrieved from Entity Framework
IsRequired IsRequired(bool required = true) marks property that required validation rule will be checked
IsHidden IsHidden(bool hidden = true) marks property that it should not be shown in the dynamic form
IsReadOnly IsReadOnly(bool readOnly = true) marks property that it is not editable
IsFilter IsFilter(bool filter = true) marks property that it can be used by filtering values by ID and prepopulated from Master record
IsUnique IsUnique(bool unique = true) marks property that unique validation rule will be checked
Label Label(string label) specifies label or column name for the property
Format Format(string format) specifies string format, very useful for DateTime
Control Control(Type controlType) specifies Dynamic Form UI component that should be used in the dynamic form
ControlReadOnly ControlReadOnly(Type controlType) specifies Dynamic Form UI component that should be used in the dynamic form in ReadOnly mode
Rule Rule([NotNullAttribute] Func<TEntity, FormRuleResult> method, FormRuleTriggers trigger = FormRuleTriggers.ChangeSubmit) allows to provide method to custom validation rule, accepts parameters: method, trigger

The simple attributes are used in the definition chain:

e.Property(p => p.DepartmentId).IsFilter().IsUnique().IsRequired().Label("Id").Format("d");

Rule example:

        protected override void Define(DynamicFormBuilder builder)
        {
            builder.Entity<Customer>(e =>
            {
                ...
                e.Property(p => p.EmailAddress).Rule(CheckEmail);
                ...
            });

        }

        public FormRuleResult CheckEmail(Customer model)
        {
            if (model.EmailAddress != null && !model.EmailAddress.Contains("@"))
            {
                return new FormRuleResult("Email should contain '@' symbol");
            }

            return null;
        }

Dropdown<TEntity> - method

public virtual DropdownFieldBuilder<TEntity> Dropdown<TEntity>()

This method allows defining a selector UI behavior (a dropdown control) when the User should choose a value from a list. It accepts the TEntity template parameter that is Entity Framework entity that linked to a SQL table containing all values that should be shown in the list for selection.

It returns DropdownFieldBuilder<TEntity> embedded class with a single method Set that allows specifying TEntity properties for Id and Name to render in the dropdown control, see example below:

e.Property(p => p.Administrator).Dropdown<Person>().Set(c => c.PersonId, c => c.FullName).IsRequired();

These methods Dropdown and Set should be always used together, and after them, the chain of simple attributes can follow.

Next Page: DataServiceBase

⚠️ **GitHub.com Fallback** ⚠️