We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17412
    • 270 Posts
    Hi, I have a FormIt form that has dynamic fields (user can add further fields as required). These fields auto-increment i.e. form-1-supplier , form-2-supplier, form-3-supplier, etc.

    I need to output a different FormIt snippet depending on whether form-1-supplier, form-2-supplier, etc are empty or not.

    The reason for this is to include validation and specify which fields should be saved to database (using FormSave).

    This is what I have at the moment (the different FormIt statements are contained in chunks [[$gasformit_9]], etc

    [[!If?
    &subject=`[[+fi.form-9-supplier]]`
    &operator=`empty`
    &then=`[[$gasformit_9]]`
    &else=`[[!If?
    &subject=`[[+fi.form-8-supplier]]`
    &operator=`empty`
    &then=`[[$gasformit_8]]`
    &else=`[[!If?
    &subject=`[[+fi.form-7-supplier]]`
    &operator=`empty`
    &then=`[[$gasformit_7]]`
    &else=`[[!If?
    &subject=`[[+fi.form-6-supplier]]`
    &operator=`empty`
    &then=`[[$gasformit_6]]`
    &else=`[[!If?
    &subject=`[[+fi.form-5-supplier]]`
    &operator=`empty`
    &then=`[[$gasformit_5]]`
    &else=`[[!If?
    &subject=`[[+fi.form-4-supplier]]`
    &operator=`empty`
    &then=`[[$gasformit_4]]`
    &else=`[[!If?
    &subject=`[[+fi.form-3-supplier]]`
    &operator=`empty`
    &then=`[[$gasformit_3]]`
    &else=`[[!If?
    &subject=`[[+fi.form-2-supplier]]`
    &operator=`empty`
    &then=`[[$gasformit_2]]`
    &else=`[[!If?
    &subject=`[[+fi.form-1-supplier]]`
    &operator=`empty`
    &then=`[[$gasformit_1]]`
    &else=`[[!If?
    &subject=`[[+fi.form-0-supplier]]`
    &operator=`empty`
    &then=`[[$gasformit_0]]`
    &else=``]]`]]`]]`]]`]]`]]`]]`]]`]]`]]



    Currently, regardless of whether form-9-supplier is empty or not, the first chunk is output [[$gasformit_9]]

    My question, is how to get the bloody thing to work smiley?

    Any help greatly appreciated.
      • 40045
      • 534 Posts
      First: How could this work?

      A
      [[+fi.fieldname]]
      placeholder is only set/used by FormIt itself, so if you don't have this inside the submitted form (submit to FormIt) the placeholder would not get parsed, making the
      [[+fi.form-9-supplier]]
      always empty. That's why you always get the
      [[$gasformit_9]]
      chunk.

      Second: It's not so easy to do what you want =)...because basically the FormIt call needs to change dynamically (as you say), and the architecture with x different chunks handling exactly one case seems not to be very elegant...and because the user is able to add additional fields (probably via jQuery or so?), these can't be parsed on document load (where the FormIt call gets parsed). This means that if the user added a field, the site (with the new chunk/FormIt Call) had to be reloaded, but then the field is gone again for the user^^...I also had this scenario and I just had to skip validation for these "possible-to-add-one-more" fields... =/, the only possibility I can think of is some ajax handling of the whole form container. ie. let the user fill out the form, add the needed fields (probably you have to generate some additional info to pass via ajax when a field is added) and then (before submitting it or on blur event or whatever) send the complete form code to the server, let him parse the additional information about added fields, generate a new FormIt call for that and then return it back together with the before sent form code (including the inputed values from the user...)...


      would also be interested in an elegant solution to that problem.
        • 17412
        • 270 Posts
        Thanks for the input Exside!! - it's really useful for me to better understand the mechanics. It seems that an ajax solution would be needed for such cases. For now I am working around the problem by not validating the additional fields, displaying all potential fields in the FormSave component (background noise though acceptable) and hardcoding the email template with the various additional fields (with custom modifiers to hide empty fields) and of course trusting in my fellow humans to input sane data :~
        • You can use jquery for validation, then your fellow humans would have to work at it to deliberately send bad data, and of course your final processing (into the database, etc) should sanitize and validate any incoming data before inserting it in any case.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 17412
            • 270 Posts
            Thanks Susan, Yep I've got client-side validation in place.