We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19872
    • 1,078 Posts
    MODx 2.3.5 & Foundation 5 Form Styling

    Is it possible to make formitcountry a required field? I've made numerous attempts to make a required field with no success, so thought I'd better check to see if it’s even possible. Found some forum posts related to making a select item required, but the markup is much different that what's shown for formitcountry. Plus, not sure from the forum posts, whether or not any of the described solutions actually worked.

     <div class="medium-3 columns">
     <label>COUNTRY
         <select name="addresseeCountry" id="addresseeCountry"><option value="">Choose Country</option>[[!FormItCountryOptions? &selected=`[[+fi.addresseeCountry]]` &prioritized=`US,GB,DE,RU,JP,FR,NL,CA,AU,UA`]] </select>
    </label>
    </div>
    </div>
    

    This question has been answered by BobRay. See the first response.

      • 3749
      • 24,544 Posts
      One way to do it would be with JavaScript. You could make "Choose Country" the first item on the list. On submission, pop up a "Choose a Country" alert and block the submission if that field was still set to "Choose Country".
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 19872
        • 1,078 Posts
        Ug! Tearing my hair out on this.
        Trying to first get my redirectTo in the formit call to direct to a page based on the country selected. I want US to get a specific response page, and all other countries, if selected, to get a different response page.
        Thought I had a solution working with this in the formit call
        &redirectTo=`[[+[[+addresseeCountry:isequalto=`US`:then=`7`:else=`8`]]]]`
        

        And this as my markup for the select menu
        <select name="addresseeCountry" id="addresseeCountry"><option value="">Choose Country</option>[[!FormItCountryOptions? &selected=`[[+fi.addresseeCountry]]` &prioritized=`US,GB,DE,RU,JP,FR,NL,CA,AU,UA`]]</select>
        And now it doesn't work.

        I removed
        <option value="">Choose Country</option>

        from the market, and still my conditional in the formit call is not working.

        I do have addresseeCountry:required as part of my formit validation.

        Are there any tutorials surrounding formitcountry available that are more detailed that what is displayed in the MODx docs?

        Goal:
        Make Country select required.
        Have redirect go to one resource if US is selected
        Have redirect go to an alternate resource if any other countries are selected.
        If nothing is selected, I need to have a message inserted.

        Other required text and text area fields are working and displaying the error message when not completed. But I'm having a hard time understanding how I translate the markup for those to formitcountry.

        Would I have better luck omitting the use of formitCountry and hard code all of the country options so that the select can load with an empty value?
          • 19872
          • 1,078 Posts
          Quote from: BobRay at Jul 18, 2015, 12:16 PM
          One way to do it would be with JavaScript. You could make "Choose Country" the first item on the list. On submission, pop up a "Choose a Country" alert and block the submission if that field was still set to "Choose Country".

          I didn't see your post before I added another. Thanks for responding. I don't know Bob. If I were a javascript expert, I'd jump right in:) At the moment I'm just trying to understand the form. My problem is that I build a form, get it working, reuse it over and over, and then forget how I got to that point. I might need to go back to basics again on forms. sad

          For now I could skip country being required, and just HOPE the user selects a country. Having the asterisk kind of helps. Personally — I respond more to the asterisk next to a label more that waiting for error messages. I know. Probably not reliable to expect users to do what they are supposed to do.:)

          Big question though: Shouldn't I be able to use a conditional for the redirect?
          &redirectTo=`[[+[[+addresseeCountry:isequalto=`US`:then=`7`:else=`8`]]]]`

          I thought I had this working OK, but now this doesn't work, so maybe it was just a coincidence. Is it even advisable to put an output modifier in the formit call.

            • 3749
            • 24,544 Posts
            It could work if FormIt is setting the +addresseeCountry placeholder based on the $_POST value, but only if you're calling FormIt uncached.

            I think you might be better off creating your own postHook that gets the value and redirects on the basis of it. I'm guessing here, but I think it would be something like this:

            $country = $hook->getValue('addresseeCountry');
            
            switch ($country) {
            
               case 'US':
                 $docId = 8;
                 break;
            
               case 'UK':
                 $docId = 12;
                 break;
            
            // etc.
            
               default:
                 $docId = 8;
                 break;    
            }
            
            $docId = empty($docId)? 8 : $docId;
            $url = $modx->makeUrl($docId, "", "", "full");
            $modx->sendRedirect($url);


            For the JS (untested)... make sure your first option is something like '* Select A Country' and its value is 0.

            <script type="text/javascript">
            function validateCountry() {
                var e = document.getElementById("addresseeCountry");
                var optionSelIndex = e.options[e.selectedIndex].value;
                var optionSelectedText = e.options[e.selectedIndex].text;
                if (optionSelIndex == 0) {
                    alert("Please select a Course");
                    return false;
                }
                else {
                    alert("Success !! You have selected Country : " + optionSelectedText);  // remove this line when it's working
                    return true;
                }
            }
            </script> 
            


            Then, in your submit button add this:

            onclick="validateCountry();"
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 19872
              • 1,078 Posts
              Wow Bob! Thanks for the approach here. Excited to receive, yet humbled in that it illustrates how much I STILL have to learn.

              I do have FormIt called uncached — [[!FormIt? — as well as the country options — [[!FormItCountryOptions? —but was hoping it wasn't, because I'm almost sure that my output modifier had indeed worked when I first installed it. So much for an aha moment.
              Upon reviewing my source code from various attempts — it seems that FormItCountryOptions always has a default value, unless I am misunderstanding how it works. Country would be the type of item that you would absolutely need to have validated by default or have an empty option, otherwise, everyone who fills out my form, is coincidentally from Australia smiley

              Anyhow, I'm up early and will work with your approach. I really appreciate your providing this information.
                • 3749
                • 24,544 Posts
                If there is a default value, there's no way to see if the user has answered the question. That's why most sites use a default value like "Select a Country."

                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 19872
                  • 1,078 Posts
                  Hi Bob: Thanks for responding back.
                  I agree. So I setup, and had been attempting various configurations of creating an empty value, or a value that I could identify as "this field has not been filled out". The other issue was in getting an error message to appear that works within the foundation 5 styling for error messages.

                  The following works — I think. Still testing.

                        <label>COUNTRY*
                  <select name="addresseeCountry" id="addresseeCountry"><option value=""></option>[[!FormItCountryOptions? &selected=`[[+fi.addresseeCountry]]` &prioritized=`US,GB,DE,RU,JP,FR,NL,CA,AU,UA`]]</select>
                  [[!+fi.error.addresseeCountry:notempty=`<span class="error">Choose Country</span>`]]
                        </label>


                  Left unselect the error message appears. Selected, the response page loads and email arrives a second later.
                  On my first trial run of leaving the item unselected and then selecting an item, a page displayed with an Internal Error message. No errors in the MODx log though.
                  On my second trial run, all went well.

                  I guess I could also use "Select a Country" as you mentioned for the default value. Then my error code might be !+fi.error.addresseeCountry:`Select a Country`=
                  ???

                  The output modifier does not work however.

                  &redirectTo=`[[+addresseeCountry:isequalto=`US`:then=`7`:else=`8`]]`


                  FormIt seems to just bully it's way through to the else and loads the last page. But maybe it actually is working. the empty value is NOT US, therefore redirect to 8 would make sense.
                  If that's the case, then I need to account for empty.
                    • 19872
                    • 1,078 Posts
                    I just got the Internal Server Error again after another successful test. But it's not related to the form I don't think. I clicked to view the MODx error log to check for any new entries and got the Internal Error page instead of the MODx error log.
                      • 19872
                      • 1,078 Posts
                      Quote from: BobRay at Jul 18, 2015, 11:38 PM
                      It could work if FormIt is setting the +addresseeCountry placeholder based on the $_POST value, but only if you're calling FormIt uncached.

                      I think you might be better off creating your own postHook that gets the value and redirects on the basis of it. I'm guessing here, but I think it would be something like this:

                      $country = $hook->getValue('addresseeCountry');
                      
                      switch ($country) {
                      
                         case 'US':
                           $docId = 8;
                           break;
                      
                         case 'UK':
                           $docId = 12;
                           break;
                      
                      // etc.
                      
                         default:
                           $docId = 8;
                           break;    
                      }
                      
                      $docId = empty($docId)? 8 : $docId;
                      $url = $modx->makeUrl($docId, "", "", "full");
                      $modx->sendRedirect($url);



                      Giving this a try.
                      The postHook above, I put in a snippet. Called it in my chunk with my form code—uncached. And then added $postHook=`nameOfSnippet` to my formit call.

                      Initial attempt resulted in: Fatal error: Call to a member function getValue() on a non-object in ..../public_html/core/cache/includes/elements/modsnippet/41.include.cache.php on line 3

                      Time to break out your book again and review your section on PHP.