• Resolved chrisjdahl

    (@chrisjdahl)


    I’m trying to use the ‘integration-cds/forms/fields’ filter, but I don’t know how to dynamically pass it the field names that have been “touched”/”changed”. I’ve tried putting the add_filter inside of an ajax handler which passes the “columns to update” to the ‘my_icds_filter_function’ all within the in the functions.php file, but I don’t think that I can add_filter after the page has been rendered… Any suggestions?

    I’d like to avoid using the twig “{{lead.firstname}}” etc to populate the form fields with the existing data if possible.

        add_filter('integration-cds/forms/fields','my_icds_filter_function',1);
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author alexacrm

    (@alexacrm)

    @chrisjdahl can you please describe the scenario? Are you editing an existing record or creating a new one? I’m trying to understand the context when we shouldn’t send empty fields as nulls

    Thread Starter chrisjdahl

    (@chrisjdahl)

    Sure, the record does already exist. The user is redirected from the initial form to this “more information” conversational form. I am trying to load a single form that has all my fields, and then via javascript, control which fields are displayed to the user on each prompt.

    Below is the code for my form builder, then I have all my questions stored in a twig collection called questions and all the logic and the content that is displayed for each page in a javascript object called “Pathways”. Basically there are 4 “starting point pathways” and each page after displays a different set of questions/messages based on the previous response. Many of these pathways reuse the same questions so I thought loading each question in a single form would be a better way to go. I also wanted to utilize “stay on same page after form submit” so we don’t possibly lose users each time the page reloads.

    One solution that seems like it might work is renaming the input’s name attribute on submit so that it doesn’t map correctly to the record…. (although it throws a lot of warnings in the icds logs and a lot of my javascript rely’s on the name attribute)

    There’s probably a much better way to do what I’m trying to do, and I’m all ears for it! Thanks

    {# generate form #}
    {% form entity="lead" keep=true keep-data=true mode="update" record=lead|to_entity_reference %}
    <form id="get-started" method="post" enctype="multipart/form-data">
    <h2 id="form-heading"></h2>
    <p id="form-message"></p>
    <div class="active-questions"></div>
    <div class="inactive-questions">
    {% for key, field in questions %}
    <div class="form-group">
    <label for="{{ field.name }}">{{ field.label }}</label>

    {% if field.type == 'text' %}
    <input type="text" name="{{ field.name }}" id="{{ field.name }}" class="form-control" />

    {% elseif field.type == 'date' %}
    <input type="date" name="{{ field.name }}" id="{{ field.name }}" class="form-control" />

    {% elseif field.type == 'file' %}
    <input type="file" name="{{ field.name }}" id="{{ field.name }}" class="form-control" />

    {% elseif field.type == 'number' %}
    <input type="number" name="{{ field.name }}" id="{{ field.name }}" class="form-control"
    min="{{ field.min }}" max="{{ field.max }}" />

    {% elseif field.type == 'textarea' %}
    <textarea name="{{ field.name }}" id="{{ field.name }}" class="form-control"
    rows="{{ field.rows }}"></textarea>

    {% elseif field.type == 'radio' %}
    <div class="{{field.class}}">
    {% for option in field.options %}
    <div class="form-check">
    <input type="radio" name="{{ field.name }}" id="{{ field.name }}_{{ option.value }}"
    value="{{ option.value }}" class="form-check-input" {% if option.selected %} checked {% endif %}
    {% if option.hidden %} hidden {% endif %} {% if option.disabled %} disabled {% endif %} />
    <label class="form-check-label" for="{{ field.name }}_{{ option.value }}">{{ option.label }}</label>
    </div>
    {% endfor %}
    </div>

    {% elseif field.type == 'checkbox' %}
    {% for option in field.options %}
    <div class="form-check">
    <input type="checkbox" name="{{ field.name }}" id="{{ field.name }}_{{ option.value }}"
    value="{{ option.value }}" class="form-check-input {{option.class}}" {% if option.selected %}
    checked {% endif %} {% if option.hidden %} hidden {% endif %} {% if option.disabled %} disabled {%
    endif %} />
    <label class="form-check-label" for="{{ field.name }}_{{ option.value }}">{{ option.label }}</label>
    </div>
    {% endfor %}

    {% elseif field.type == 'select' %}
    <select name="{{ field.name }}" id="{{ field.name }}" class="form-control">
    {% for option in field.options %}
    <option value="{{ option.value }}" {% if option.selected %} selected {% endif %} {% if option.hidden %}
    hidden {% endif %} {% if option.disabled %} disabled {% endif %}>{{ option.label }}
    </option>
    {% endfor %}
    </select>
    </div>

    </div>
    <div class="form-nav">
    <button type="button" id="prev-btn" class="btn btn-primary">Prev</button>
    <button type="submit" id="next-btn" class="btn btn-primary">Next</button>
    <button type="submit" id="submit-btn" class="btn btn-primary">Submit</button>
    </div>
    </form>

    {% endform %}
    Plugin Author alexacrm

    (@alexacrm)

    Let me see if I understand it correctly. If you want to keep the HTML field empty while the actual Dataverse column has a value and then treat no input as “leave the field alone” then off top of my head, you can use two approaches:

    1. Keep collection of values “on a side” and on form submit inject those values instead of blanks. Or even try removing the values using FormData object.
    2. Create additional columns in Dataverse that would take the values entered by the user and then have a Power Automate flow on update that would copy the values over to the actual columns but only if they are not blanks

    HTH

    Thread Starter chrisjdahl

    (@chrisjdahl)

    Thank you very much! Today I have been trying to modify the FormData object, but I couldn’t figure out how to access it and modify the data.

    However I found one of your custom events “icds.forms.submitting”, and it looks like I can modify the Object in here.

    Unless you see any issues here, I’m happy as can be and will mark this resolved.

    		document.addEventListener("icds.forms.submitting", (e) => {
    const fields = e.detail.fields;
    Object.entries(fields).forEach(([key, value]) => {
    if (value === null) delete fields[key];
    });
    });
    Plugin Author alexacrm

    (@alexacrm)

    Tbh, this is a very left of field scenario and if that approach works, all the powers to you!

    Glad so see it resolved. If you happy with the plugin and have a spare minute or two, we’d appreciate a review.

    Thank you,
    George
    AlexaCRM

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.