On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • Hi

    I'm now starting to use the Login extra for the first time and have managed to create all the basic functionality which works successfully, by using the tutorials provided. However, I'm now trying to use extended profiles and custom fields with MODx and have come across an issue with getting checkboxes to check by default.

    My Register snippet works fine and will add the custom field - propertyAlerts - with the relevant '1' or '0' value depending upon whether the checkbox is checked prior to submitting this form. My profile page can display the relevant value for the custom field. When I try to use the UpdateProfile snippet, will be checked if the value is 1, however, if I subsequently 'uncheck' the field, this change is not saved. I've included my code below, can anyone assist with this?

    [[!UpdateProfile? &excludeExtended=`email:required:email,login-updprof-btn` &useExtended=`1`]]
      
    <div class="update-profile">
        <div class="updprof-error">[[+error.message]]</div>
        [[+login.update_success:if=`[[+login.update_success]]`:is=`1`:then=`[[%login.profile_updated? &namespace=`login` &topic=`updateprofile`]]`]]
      
        <form class="form" action="[[~[[*id]]]]" method="post">
            <input type="hidden" name="nospam:blank" value="" />
      
            <label for="fullname">[[!%login.fullname? &namespace=`login` &topic=`updateprofile`]]
                <span class="error">[[+error.fullname]]</span>
            </label>
            <input type="text" name="fullname" id="fullname" value="[[+fullname]]" />
    
            <label for="email">[[!%login.email]]
                <span class="error">[[+error.email]]</span>
            </label>
            <input type="text" name="email:required:email" id="email" value="[[+email]]" />
    
            <label for="propertyAlerts">[[!%login.propertyAlerts]]
                <span class="error">[[+error.propertyAlerts]]</span>
            </label>
    		<input type="hidden" name="propertyAlerts" id="propertyAlerts" value="[[+propertyAlerts:default=`0`]]" />
            <input type="checkbox" name="propertyAlerts" id="propertyAlerts" value="[[+propertyAlerts]]" [[+propertyAlerts:gt=`0`:then=`checked="checked"`]] />
    
    
            <div class="form-buttons">
                <input type="submit" name="login-updprof-btn" value="[[!%login.update_profile]]" />
            </div>
        </form>
    </div>	
    
    

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

    • [[+propertyAlerts:gt=`0`:then=`checked="checked"`]]

      This seems to indicate that you have got unchecked (ie 0) equal to checked.
      • Try changing this:

        [[+propertyAlerts:gt=`0`:then=`checked="checked"`]]


        to this:

        [[+propertyAlerts:gt=`0`:then=`checked="checked"`:else=``]]


        Some conditional modifiers don't work well without an else clause.

        Also, instead of using &excludeExtended, try using &allowedExtendedFields
          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
        • Hi bobray

          Thanks for replying.

          I've included my updated code below. I've tried setting the &allowExtendedFields as a blank property as well as including the name of the extended field, as shown below. Unfortunately, the code still isn't working and the extended field isn't being updated on submission. If it helps, I'm using MODX redo version 2.3.3-pl and Login version 1.9.0-pl.

          I've tried editing the extended fields directly within the user manager of MODx and cannot seem to set the value to '0' instead of '1'. Is there something else wrong here? Should I be able to modify extended fields of a user who as registered themselves and requested permission - as per the tutorials. My MODX manager user is a 'sudo' user.

          [[!UpdateProfile? &excludeExtended=`email:required:email,login-updprof-btn` &useExtended=`1` &allowExtendedFields=`propertyAlerts`]]
          
          <div class="update-profile">
              <div class="updprof-error">[[+error.message]]</div>
              [[+login.update_success:if=`[[+login.update_success]]`:is=`1`:then=`[[%login.profile_updated? &namespace=`login` &topic=`updateprofile`]]`]]
            
              <form class="form" action="[[~[[*id]]]]" method="post">
                  <input type="hidden" name="nospam:blank" value="" />
            
                  <label for="fullname">[[!%login.fullname? &namespace=`login` &topic=`updateprofile`]]
                      <span class="error">[[+error.fullname]]</span>
                  </label>
                  <input type="text" name="fullname" id="fullname" value="[[+fullname]]" />
          
                  <label for="email">[[!%login.email]]
                      <span class="error">[[+error.email]]</span>
                  </label>
                  <input type="text" name="email:required:email" id="email" value="[[+email]]" />
          
                  <label for="propertyAlerts">[[!%login.propertyAlerts]]
                      <span class="error">[[+error.propertyAlerts]]</span>
                  </label>
          		<input type="hidden" name="propertyAlerts" id="propertyAlerts" value="[[+propertyAlerts:default=`0`]]" />
                  <input type="checkbox" name="propertyAlerts" id="propertyAlerts" value="[[+propertyAlerts]]" [[+propertyAlerts:gt=`0`:then=`checked="checked"`]] />
          
          
                  <div class="form-buttons">
                      <input type="submit" name="login-updprof-btn" value="[[!%login.update_profile]]" />
                  </div>
              </form>
          </div>			
          
          
          • I don't know why you couldn't save a 0 value. I don't ever use the extended field for anything.

            I was suggesting this, so you might give it a try if you haven't already:

            [[!UpdateProfile? &useExtended=`1` &allowExtendedFields=`propertyAlerts`]]


            You might also look at the UpdateProfile code to see if this fix has been implemented: https://gist.github.com/pepebe/3928666

              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
            • discuss.answer
              Quite obvious what is going wrong here:
              <input type="hidden" name="propertyAlerts" id="propertyAlerts" value="[[+propertyAlerts:default=`0`]]" />
              <input type="checkbox" name="propertyAlerts" id="propertyAlerts" value="[[+propertyAlerts]]" [[+propertyAlerts:gt=`0`:then=`checked="checked"`]] />
              


              You set the value of both (hidden and checkbox) to the value of 'propertyAlerts' placeholder. That way you can't change the value after it is set.

              <input type="hidden" name="propertyAlerts" id="propertyAlerts" value="0" />
              <input type="checkbox" name="propertyAlerts" id="propertyAlerts" value="1" [[+propertyAlerts:eq=`1`:then=`checked="checked"`]] />
              


              With this code a post will always set $_POST['propertyAlerts'] to a value. If you remove the hidden input $_POST['propertyAlerts'] is only set if the checkbox is checked. If then the checkbox is unchecked $_POST['propertyAlerts'] is unset (thats a limitation of the checkbox input). [ed. note: Jako last edited this post 4 years, 2 months ago.]
              • Thanks Jako, that has fixed it. Could see that for looking!
                • Good catch Jako! Thanks for stepping in. smiley
                    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
                  • Thanks. Sometimes it is better to have a third person looking on.