Margarita form reference documentation
    Preparing search index...

    Class MargaritaForm<FIELD>

    Type Parameters

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    Accessors

    Methods

    Constructors

    Properties

    _buildParams: ControlBuildParams = ...
    _field: FIELD
    activate: () => void = ...
    addControl: <CHILD_FIELD extends any>(
        field: CHILD_FIELD,
        replaceExisting?: boolean,
    ) => MFC<CHILD_FIELD> = ...

    Add new control to the form group.

    Type declaration

      • <CHILD_FIELD extends any>(
            field: CHILD_FIELD,
            replaceExisting?: boolean,
        ): MFC<CHILD_FIELD>
      • Type Parameters

        • CHILD_FIELD extends any

        Parameters

        • field: CHILD_FIELD

          The field to use as a template for the new control

        • OptionalreplaceExisting: boolean

          Replace existing control with the same name when parent is not an array type

        Returns MFC<CHILD_FIELD>

        Control that was added

    addValue: (
        value: ControlValueItem<FIELD>,
        mustBeUnique?: boolean,
        setAsDirty?: boolean,
        emitEvent?: boolean,
    ) => void = ...

    Add a new value to array

    Type declaration

      • (
            value: ControlValueItem<FIELD>,
            mustBeUnique?: boolean,
            setAsDirty?: boolean,
            emitEvent?: boolean,
        ): void
      • Parameters

        • value: ControlValueItem<FIELD>

          Value to add to the array

        • OptionalmustBeUnique: boolean

          Should the value be unique (default = false)

        • OptionalsetAsDirty: boolean

          Should the dirty state be set to true

        • OptionalemitEvent: boolean

        Returns void

    appendControl: <CHILD_FIELD extends MFF<FieldParams> = FIELD>(
        fieldTemplate?: string | CHILD_FIELD,
        overrides?: Partial<CHILD_FIELD>,
    ) => null | MFC<CHILD_FIELD> = ...

    Add new control to the form array.

    appendControls: <CHILD_FIELD extends MFF<FieldParams> = FIELD>(
        fieldTemplates: string[] | CHILD_FIELD[],
    ) => MFC<CHILD_FIELD>[] = ...

    Add new controls to the form array.

    changes: BehaviorSubject<ControlChange>
    cleanup: () => void = ...

    Unsubscribe from all subscriptions for current control

    clear: (resetChildren?: boolean) => void = ...
    clearValue: (
        setDirtyAs?: boolean,
        resetChildren?: boolean,
        origin?: boolean,
    ) => void = ...
    deactivate: () => void = ...
    disable: () => void = ...
    dispatch: (
        action: any,
        setAsDirty?: boolean,
        emitEvent?: boolean,
    ) => Promise<void> = ...

    Run value through field's dispatcher and set the result as the new value

    emitChange: (
        name: ControlChangeName,
        change: unknown,
        origin?: MFC<any>,
    ) => void = ...
    enable: () => void = ...
    error: undefined | Error
    field: FIELD
    findControl: <
        FIELD_TYPE,
        IDENTIFIER extends any = DeepControlIdentifier<FIELD>,
        _T = ChildControl<FIELD_TYPE, IDENTIFIER, FIELD, ChildField<FIELD>>,
    >(
        identifier: IDENTIFIER,
    ) => undefined | _T = ...

    Find control with identifier. Searches all child controls recursively and returns the first match.

    Type declaration

    get: <VALUE>(key: OrString | (keyof MFC<MFGF<FieldParams>>)) => VALUE = ...

    In some cases type of an attribute is not known by the compiler. Use this method to get the attribute with correct type.

    Type declaration

      • <VALUE>(key: OrString | (keyof MFC<MFGF<FieldParams>>)): VALUE
      • Type Parameters

        • VALUE

        Parameters

        • key: OrString | (keyof MFC<MFGF<FieldParams>>)

          The key of the attribute

        Returns VALUE

        The attribute value with custom type

    getActiveSiblings: <SIBLING_FIELD extends MFF<FieldParams> = MFGF<FieldParams>>() => MFC<
        SIBLING_FIELD,
    >[] = ...

    Get all active sibling controls as an array

    Type declaration

    Error if control is root

    getControl: <
        FIELD_TYPE,
        IDENTIFIER extends any = DeepControlIdentifier<FIELD>,
        _T = ChildControl<FIELD_TYPE, IDENTIFIER, FIELD, ChildField<FIELD>>,
    >(
        identifier: IDENTIFIER,
    ) => ControlValue<FIELD> extends any[] ? undefined | _T : _T = ...

    Get control with identifier

    Type declaration

    getControls: <FIELD extends MFF<FieldParams> = ChildField<FIELD>>(
        states?: string | string[],
    ) => MFC<FIELD>[] = ...

    Get all controls that match required states

    getFieldValue: <OUTPUT = unknown>(
        key: keyof FIELD,
        defaultValue?: OUTPUT,
    ) => OUTPUT = ...

    Get the control's field's value

    Type declaration

      • <OUTPUT = unknown>(key: keyof FIELD, defaultValue?: OUTPUT): OUTPUT
      • Type Parameters

        • OUTPUT = unknown

        Parameters

        • key: keyof FIELD

          The field to get

        • OptionaldefaultValue: OUTPUT

        Returns OUTPUT

        Field's value or fallback

    getManager: <MANAGER>(key: string) => MANAGER = ...
    getOrAddControl: <CHILD_FIELD extends any>(
        field: CHILD_FIELD,
    ) => MFC<CHILD_FIELD> = ...

    Get control or add it if it doesn't exist. NOTE: If grouping is array this method might not work as expected as arrays allow multiple controls with the same name!

    Type declaration

      • <CHILD_FIELD extends any>(field: CHILD_FIELD): MFC<CHILD_FIELD>
      • Type Parameters

        • CHILD_FIELD extends any

        Parameters

        • field: CHILD_FIELD

          The field to use as a template for the new control

        Returns MFC<CHILD_FIELD>

        The control that was found or added

    getPath: (outcome?: "controls" | "keys" | "default" | "uids") => ControlPath = ...
    getSibling: <SIBLING_FIELD extends MFF<FieldParams> = MFGF<FieldParams>>(
        identifier: ControlIdentifier,
    ) => MFC<SIBLING_FIELD> = ...

    Get sibling control with identifier

    Type declaration

    Error if control is root

    getSiblings: <SIBLING_FIELD extends MFF<FieldParams> = MFGF<FieldParams>>() => MFC<
        SIBLING_FIELD,
    >[] = ...

    Get all sibling controls as an array

    Type declaration

    Error if control is root

    getState: <T extends keyof MargaritaFormState>(key: T) => MargaritaFormState[T] = ...
    getStateChanges: <T extends keyof MargaritaFormState>(
        key: T,
    ) => Observable<MargaritaFormState[T]> = ...
    hasControl: (identifier: ControlIdentifier) => boolean = ...

    Check if control exists

    Type declaration

    initialized: boolean = false
    key: string
    managers: Managers = ...
    moveControl: <IDENTIFIER extends any = DeepControlIdentifier<FIELD>>(
        identifier: IDENTIFIER,
        index: number,
    ) => void = ...

    Moves a child control to a new index in the array.

    Type declaration

      • <IDENTIFIER extends any = DeepControlIdentifier<FIELD>>(
            identifier: IDENTIFIER,
            index: number,
        ): void
      • Type Parameters

        Parameters

        • identifier: IDENTIFIER

          name, index or key of the control to move

        • index: number

        Returns void

    moveToIndex: (index: number) => void = ...

    Move control to another index

    patchValue: (
        value: undefined | null | Partial<ControlValue<FIELD>>,
        setAsDirty?: boolean,
        emitEvent?: boolean,
    ) => void = ...

    Set value of the control group by updating it's childrens values. Unlike setValue, this method will not delete children values if they are not present in the new value.

    Type declaration

      • (
            value: undefined | null | Partial<ControlValue<FIELD>>,
            setAsDirty?: boolean,
            emitEvent?: boolean,
        ): void
      • Parameters

        • value: undefined | null | Partial<ControlValue<FIELD>>
        • OptionalsetAsDirty: boolean

          update dirty state to true

        • OptionalemitEvent: boolean

        Returns void

    prepared: boolean = false
    queryControls: <RESULT_FIELD extends MFF<FieldParams> = MFGF<FieldParams>>(
        query: (control: MFC<MFGF<FieldParams>>) => boolean,
        recursive?: boolean,
    ) => MFC<RESULT_FIELD>[] = ...

    Find control where query function returns true. Can search all child controls recursively to get all matches in the tree.

    Type declaration

      • <RESULT_FIELD extends MFF<FieldParams> = MFGF<FieldParams>>(
            query: (control: MFC<MFGF<FieldParams>>) => boolean,
            recursive?: boolean,
        ): MFC<RESULT_FIELD>[]
      • Type Parameters

        Parameters

        • query: (control: MFC<MFGF<FieldParams>>) => boolean

          Query function that returns true if control matches

        • Optionalrecursive: boolean

          Should the query be run recursively

        Returns MFC<RESULT_FIELD>[]

        The control that was found or undefined if control doesn't exist.

    ready: boolean = false
    redo: () => void
    registerResolver: (key: string, resolver: MargaritaFormResolver) => void = ...

    Register a new resolver

    Type declaration

    registerValidator: (key: string, validator: MargaritaFormValidator) => void = ...
    reInitialize: () => void = ...

    Resubscribe to all subscriptions for current control

    remove: () => void = ...

    Remove the control from the parent

    removeControl: <IDENTIFIER extends any = DeepControlIdentifier<FIELD>>(
        identifier: IDENTIFIER,
    ) => void = ...

    Removes a child control from the form group.

    Type declaration

    removeValue: (
        value: ControlValueItem<FIELD>,
        setAsDirty?: boolean,
        emitEvent?: boolean,
    ) => void = ...

    Remove a value from array

    Type declaration

      • (
            value: ControlValueItem<FIELD>,
            setAsDirty?: boolean,
            emitEvent?: boolean,
        ): void
      • Parameters

        • value: ControlValueItem<FIELD>

          Value to remove from the array

        • OptionalsetAsDirty: boolean

          Should the dirty state be set to true

        • OptionalemitEvent: boolean

        Returns void

    reset: (resetChildren?: boolean, origin?: boolean) => void = ...
    resetState: (respectField?: boolean, resetChildren?: boolean) => void = ...
    resetValue: (
        setDirtyAs?: boolean,
        resetChildren?: boolean,
        origin?: boolean,
    ) => void = ...
    setField: (field: FIELD, resetControl?: boolean) => Promise<void> = ...
    setRef: (ref: any) => void = ...

    Connect control to a HTML element.

    
    
    const el = document.querySelector('#myInput');
    control.setRef(el);
    setValue: (
        value: undefined | null | ControlValue<FIELD>,
        setAsDirty?: boolean,
        emitEvent?: boolean,
    ) => void = ...

    Set value of the control

    Type declaration

      • (
            value: undefined | null | ControlValue<FIELD>,
            setAsDirty?: boolean,
            emitEvent?: boolean,
        ): void
      • Parameters

        • value: undefined | null | ControlValue<FIELD>

          value to set

        • OptionalsetAsDirty: boolean

          update dirty state to true

        • OptionalemitEvent: boolean

        Returns void

    submit: <OUTPUT, PARAMS = object>(params?: PARAMS) => Promise<OUTPUT> = ...

    Submit the form. If the form is invalid, the invalid submit handler will be called. Define submit handlers in the field's handleSubmit property. You can define a different handler for valid and invalid submits but only valid submit handler is required. If handleSubmit is a function it will be used as the valid handler. If handleSubmit or handleSubmit.valid is a string, it will be used as a url for a POST request.

    Type declaration

      • <OUTPUT, PARAMS = object>(params?: PARAMS): Promise<OUTPUT>
      • Type Parameters

        • OUTPUT
        • PARAMS = object

        Parameters

        • Optionalparams: PARAMS

          Params that get passed as the second argument to the submit handler

        Returns Promise<OUTPUT>

        Promise with the submit handler's response

    subscriptions: Subscription[] = []
    syncId: string = ...
    toggleActive: () => void = ...
    toggleEnabled: () => void = ...
    toggleValue: (
        value: ControlValueItem<FIELD>,
        mustBeUnique?: boolean,
        setAsDirty?: boolean,
        emitEvent?: boolean,
    ) => void = ...

    Toggle a value in array

    Type declaration

      • (
            value: ControlValueItem<FIELD>,
            mustBeUnique?: boolean,
            setAsDirty?: boolean,
            emitEvent?: boolean,
        ): void
      • Parameters

        • value: ControlValueItem<FIELD>

          Value to add or remove from the array

        • OptionalmustBeUnique: boolean

          Should the value be unique when adding (default = true)

        • OptionalsetAsDirty: boolean

          Should the dirty state be set to true

        • OptionalemitEvent: boolean

        Returns void

    uid: string
    undo: () => void
    updateField: (field: Partial<FIELD>, resetControl?: boolean) => Promise<void> = ...
    updateKey: () => void = ...
    updateState: <T extends Partial<MargaritaFormState>>(
        changes: T,
    ) => Promise<boolean> = ...
    updateStateValue: <T extends keyof MargaritaFormState>(
        key: T,
        value: MargaritaFormState[T],
    ) => Promise<boolean> = ...
    updateSyncId: () => void = ...
    updateUid: () => void = ...
    validate: (setAsTouched?: boolean) => Promise<boolean> = ...

    Validate the control and update state. Mark the control as touched to show errors.

    Type declaration

      • (setAsTouched?: boolean): Promise<boolean>
      • Parameters

        • OptionalsetAsTouched: boolean

          Set the touched state to true

        Returns Promise<boolean>

    config: Partial<MargaritaFormConfig> = {}
    context: Partial<ControlContext<any, any>> = {}
    extensions: Set<typeof ExtensionBase> = ...
    managers: Record<string, typeof _ManagerLike> = ...
    states: Set<
        (
            state: StateManager<MFC>,
        ) => BooleanPairState | GeneralState<unknown> | DerivedState<unknown>,
    > = ...
    validators: Record<string, MargaritaFormValidator> = ...

    Accessors

    • get activeExtensions(): ExtensionBase[]

      Returns ExtensionBase[]

    • get errorMessage(): undefined | string

      Returns undefined | string

    • get expectArray(): boolean

      Check if control's output should be an array

      Returns boolean

    • get expectChildControls(): boolean

      Check if control's output should be an group / object

      Returns boolean

    • get expectFlat(): boolean

      Check if control's output should be merged to parent

      Returns boolean

    • get expectGroup(): boolean

      Check if control's output should be an group / object

      Returns boolean

    • get hasActiveControls(): boolean

      Check if control has any active child controls

      Returns boolean

    • get hasControls(): boolean

      Check if control has any child controls

      Returns boolean

    • get isDefaultValue(): boolean

      Check if the control's value is the same as the default value

      Returns boolean

    • get isInitialValue(): boolean

      Check if the control's value is the same as the initial value. Warning: this works properly only in the root level. Use defaultValue and isDefaultValue to add checks for child controls

      Returns boolean

    • get name(): FIELD["name"] extends string ? any[any] : string

      Returns FIELD["name"] extends string ? any[any] : string

    Methods