ihslimn / jet-form-builder-update-field

16 stars 1 forks source link

jet-form-builder-update-field

Allows updating field options / values on another field(s) update.

To refer to repeater field, write inner repeater field in square braces, like [text_field1].

Updating field options

This example requires JetEngine to be installed; otherwise, custom options Generator should be created - see Advanced section.

Works for Select, Radio, Checkboxes fields

  1. Enable options updater, set the field to get options from JetEngine Query Generator

image

See https://jetformbuilder.com/features/multi-optional-field-source-settings/ to set up a field using Get values list from JetEngine Query generator

  1. In the query, you may use the fact that form fields values are stored to $_REQUEST['jfb_update_related_' . $field_name], e.g. $_REQUEST['jfb_update_related_select1'] to update options accordingly. In this example, in the field 'select2' we get posts, related to post selected in the field 'select1'

Also, there is a macro \ image \ JFB Update Field - Form Field Value that you can use,

and a source for Relation macros \ image \ added by this addon.

Examples:

Get related items based on a form field

We set Related Items macro to get initial object ID from jfb_update_related_select1, so that when we choose the post in the field 'select1', 'select2' will be populated with posts, related to post from the field 'select1'

image

Don't forget to set a fallback, so that up until selection in 'select1' there will be no posts in 'select2'

Alternatively, use JFB Update Field - Form Field Value macro to do the same: \ image

Get terms, which are children of term from the form

image


Updating field value

This example requires JetEngine to be installed; otherwise, use custom callback - see Advanced section

Works for Text, Textarea, Hidden fields

  1. Enable value updater

image

242|post_title - 242 is an ID of the query you will use, post_title is a parameter that will be retrieved from the first query item.

  1. Query is set up like this:

image

Like in Updating field options example, we use jfb_update_related_select1 variable. Don't forget to set a fallback too.


Advanced

https://github.com/ihslimn/jet-forms-generate-from-posts-query example of registering a generator. Form field values are stored into $_REQUEST['jfb_update_related_' . $field_name]

To update field value with a custom callback, register a function, for example:

function jfbu_get_post_date( $field_name, $form_id, $form_fields ) {

    if ( $form_id !== 28447 || $field_name !== 'text_field' || empty( $form_fields['select1'] ) ) {
        return;
    }

    $post = get_post( $form_fields['select1'] );

    if ( ! $post ) {
        return;
    }

    return $post->post_date;

}

and use it in options

image