We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23163
    • 5 Posts
    Hello,

    I have a contact form which allows website visitor to register for an event, the user can pick the event from a dropdown list which is populated by Ditto. The code looks like this:

    <select name="event" id="cfEvent" eform="Event::1">
    			[[Ditto? &tpl=`events-dropdown` &startID=`19` &display=`all` &sortDir=`ASC` ]]
                </select>  


    When submitting the form I get the error "Event » Incorrect value" - has anyone used this method of populating form options from Ditto and are there any known issues?

    Best regards,
    Pete
      • 4310
      • 2,310 Posts
      Have you tied without the eform validation :
      eform="Event::1"
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        With a drop-down, you need to have the value="whatever" in the <option> tag. This tag is probably generated in the Ditto template?
          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
          • 30223
          • 1,010 Posts
          Dropdown lists (as well as radio and check boxes) in eForm are validated according to the values eForm parses from the html form.  For example with a form like this:
          <form action="[~[*id*]~]" id="someForm">
              <select name="test">
                  <option value="1">One</option>
                  <option value="2">Two</option>
              </select>
          </form>
          

          eForm would parse the values 1 & 2 and only validate against those two values.  This automatic validation is sufficient in most cases but, as in your case, when values are generated dynamically things get a bit confusing.  Your Ditto call likely is not populated until after eForm has parsed the form and thus sees values it doesn’t know about, hence the error message.

          There are two options. Either you embed the ditto call in a new snippet which you call from eForm’s eFormOnBeforeFormParse event (see eForm documentation or the wiki) or you override the validation rule for the select statement using the eform pseudo attribute (Read all about validation rules in the documentation in assets/snippets/eform/docs/eform.htm)

          Edit: removed confusing example
            • 13697
            • 19 Posts
            Hi,

            I think I am having a similar problem.

            I am trying to develop an application with modx that allows adding editing and removing data from a database table using eform.

            I have added a dropdown select which gets its values from a table and i have got this to display but i can’t submit the form without the dropdown field reporting an error like

            work provider » Incorrect value


            i have the following in my form chunk

            <select name="work_supplier" id="cFworkprovider" eform="A Color::1">
            [[getworksuppliersdropdown]]
            </select>



            getworksuppliersdropdown is the following snippet


            <?php
            $output = '';//create variable for holding the output
            $sql = $modx->db->query( 'SELECT * FROM `work_suppliers`');//define the DB query, in this case the table is named 'tblprojects', add WHERE AND etc. as needed
            $resultArray = $modx->db->makeArray( $sql );//put it into an array
            foreach($resultArray as $item)//go through each record in the array
            {//start looping through the data array
            $params['name']=$item['name'];//set the placeholders, the $params sets the name of the placeholder, the $item is the field name from the DB table
            $params['ID']=$item['ID'];
            $params['company']=$item['company'];
            $output.=$modx->parseChunk('worksupplierdropTpl', $params, '[+', '+]');//define the chunk name and process the content, here it's ProjectTpl which has the placeholders for the data
            }//finish looping through the data array
            return $output;//now return the output from the processed chunk
            ?>


            and

            worksupplierdropTpl looks like

            <OPTION VALUE="[+ID+]">[+name+] - [+company+]</OPTION>



            but i am not sure how i should have it setup to make it work, I sort of understand that because the values of the dropdown are added after the form has been parsedeform cant see them to compare them and validate to the possible values but I am not sure how I should change my code to get it to work, especially when editing as i am trying to use the eFormOnBeforeFormParse to populate the data into the form.

            I have searched around the forum and have found very little to do with adding editng and removing data from a database using modx, with or without eform, am i going around this the right way?

            thanks

            J
              • 30223
              • 1,010 Posts
              Sorry about the confusing example in my post  above. I think I was going to explain something more after the code, which I didn’t. I’ve removed it from the post.


              <select name="work_supplier" id="cFworkprovider" eform="A Color::1">
              [[getworksuppliersdropdown]]
                  </select>

              This will indeed not work as eForm will simply see "[[getworksuppliersdropdown]]" instead of the option tags you want to generate. "[[getworksuppliersdropdown]]" isn’t evaluated until after eform is finished.

              In your case you need to place a placeholder (eg. [+worksuppliersoptions+] ) in your template instead of a snippet call. You then will have to extend the function you are calling with the eFormOnBeforeFormParse event to create the option tags and replace the placeholder with those values.

              <?php
              function yourFunction( &$fields, &$templates ){
              
              //do all your other stuff
              $options = getAllMyOptions();
              $templates['tpl'] = str_replace('[+worksuppliersoptions+]', $options, $templates['tpl'] );
              
              // any other code
              return '';
              }
              


              This way eForm will see and parse the option tags properly!

              As for using eForm to work with databases see the eForm2db snippet discussion on this forum
                • 13697
                • 19 Posts
                Hi,

                thanks for the reply,

                have tried changing the code but am not sure what the should be passed to the $options variable with the function getAllMyOptions();
                if I just use my
                getworksuppliersdropdown
                snippet and create a function within the snippet that contains the function that is called with eFormOnBeforeFormParse and call
                $options = getAllMyOptions();
                $templates[’tpl’] = str_replace(’[+worksuppliersoptions+]’, $options, $templates[’tpl’] );
                from within the function that is called with
                eFormOnBeforeFormParse

                should that work?

                J
                  • 30223
                  • 1,010 Posts
                  getAllMyOptions() was just an example, obviously. I don’t know what other code you have and just used an arbitrary function name. The output for $options would simply be something like:

                  <option value="1">Option 1</option>
                  <option value="2">Option2</option>
                  etcetera


                  You’d have to write the function yourself. Using your existing getworksuppliersdropdown code would be a good start except that you have to embed it in a function that you call in the eFormOnBeforeFormParse event
                    • 13697
                    • 19 Posts
                    Hi,

                    I have got the dropdown appearing by calling the function that constructs it from within the function that is called from the eFormOnBeforeFormParse event.

                    the problem I am now having is that although the options are appearing they look like this in the html

                    <option VALUE="1" value= >Dom</OPTION>

                    I am not sure where the extra value= is coming from but I think it having no value is still stopping it from work as the error is still there when I try and submit the form

                    work_supplier » Incorrect value

                    I can only think I am not far away and I have a small syntax error or something?

                    thanks

                    J
                      • 30223
                      • 1,010 Posts
                      Start by making your options template all lower-case...