Fork me on GitHub

[pgForm] Relations and checkboxes  Bottom

  • I have created custom pgForm new and edit templates which contain relation fields. In the new template, everything works great. However, in the edit template, I can't seem to get pgForm to recognize if a relations checkbox is checked. Here's what I have in the template:

    Code

    <!-- realtor -->
        <!--[section name=field loop=$pubtype.fields]-->
          <!--[if $pubtype.fields[field].name == 'realtor']-->
            <!--[assign var="realtorlistitems" value=`$pubtype.fields[field].relateToPubs.publications`]-->
          <!--[/if]-->
        <!--[/section]-->
        <p>
          <label for="field-realtor">Realtor</label><br />
          <!--[foreach from=$realtorlistitems item=listitem]-->
            <input type="checkbox" id="field-realtor-<!--[$listitem.pid]-->" name="pubfields[realtor][]" value="<!--[$listitem.pid]-->"<!--[if @in_array($listitem.pid, $pubtype.fields[field].selectedPubs)]--> checked="checked"<!--[/if]--> />
            <!--[php]-->for ($i = 0; $i < $this->_tpl_vars[listitem][indent]; $i++) echo('-');<!--[/php]-->
            &nbsp;<!--[$listitem.title|pnvarprepfordisplay]--><br />
          <!--[/foreach]-->
        </p>

    Obviously, it is the conditional in the input where it tries to decide whether to add checked="checked" to the input. The conditional I used there is right out of the standard pgForm modify template and I'm not sure what to do with it to make it work properly.

    Thank you.
  • If it helps, here's what I get when I use pndebug in the template:

    Code

    0 => array (15)
        id => "93"
        name => "realtor"
        title => "Realtor"
        description => "Listing Realtor(s)"
        type => "relation"
        typeData => "2:93:3:92:::2:0:"
        isTitle => 0
        isPageable => 0
        isSearchable => 0
        isMandatory => 0
        lineno => 0
        selecttype => "2"
        popup => "0"
        relateToPubs => array (2)
          publications => array (1)
            0 => array (9)
              id => "4"
              pid => "1"
              author => "admin"
              created => "1252203970"
              approvalState => "UNKNOWN"
              topic => null
              revision => "4"
              online => "1"
              title => "Tyler Becker"
          more => false
        selectedPubs => array (1)
          0 => "1"


    And FWIW, I think the following code is where the problem is, I just don't know what to do to make it work:

    Code

    <!--[if @in_array($listitem.pid, $pubtype.fields[field].selectedPubs)]--> checked="checked"<!--[/if]-->
  • Tried this too:

    Code

    <!--[section name=field loop=$pubtype.fields]-->
          <!--[if $pubtype.fields[field].name == 'realtor']-->
            <!--[assign var="realtorlistitems" value=`$pubtype.fields[field].relateToPubs.publications`]-->
            <!--[assign var="realtorlistchecked" value=`$pubtype.fields[field].selectedPubs.publications`]-->
          <!--[/if]-->
        <!--[/section]-->
        <p>
          <label for="field-realtor">Realtor</label><br />
          <!--[foreach from=$realtorlistitems item=listitem]-->
            <input type="checkbox" id="field-realtor-<!--[$listitem.pid]-->" name="pubfields[realtor][]" value="<!--[$listitem.pid]-->"<!--[if @in_array($listitem.pid, $realtorlistchecked)]--> checked="checked"<!--[/if]--> />
            <!--[php]-->for ($i = 0; $i < $this->_tpl_vars[listitem][indent]; $i++) echo('-');<!--[/php]-->
            &nbsp;<!--[$listitem.title|pnvarprepfordisplay]--><br />
          <!--[/foreach]-->
        </p>
  • GOT IT!!

    The line in my second solution above that reads:

    Code

    <!--[assign var="realtorlistchecked" value=`$pubtype.fields[field].selectedPubs.publications`]-->
    Should instead read:

    Code

    <!--[assign var="realtorlistchecked" value=`$pubtype.fields[field].selectedPubs`]-->


    So, the final sample code for relations with checkboxes in a pgForm modify template is:

    Code

    <!--[section name=field loop=$pubtype.fields]-->
          <!--[if $pubtype.fields[field].name == 'realtor']-->
            <!--[assign var="realtorlistitems" value=`$pubtype.fields[field].relateToPubs.publications`]-->
            <!--[assign var="realtorlistchecked" value=`$pubtype.fields[field].selectedPubs`]-->
          <!--[/if]-->
        <!--[/section]-->
        <p>
          <label for="field-realtor">Realtor</label>
          <!--[foreach from=$realtorlistitems item=listitem]-->
            <input type="checkbox" id="field-realtor-<!--[$listitem.pid]-->" name="pubfields[realtor][]" value="<!--[$listitem.pid]-->"<!--[if @in_array($listitem.pid, $realtorlistchecked)]--> checked="checked"<!--[/if]--> />
            <!--[php]-->for ($i = 0; $i < $this->_tpl_vars[listitem][indent]; $i++) echo('-');<!--[/php]-->
             <!--[$listitem.title|pnvarprepfordisplay]-->
          <!--[/foreach]-->
        </p>


    And, for those of you watching at home, here's what you'd use in a pgForm template for new pubs:

    Code

    <!--[section name=field loop=$pubtype.fields]-->
          <!--[if $pubtype.fields[field].name == 'realtor']-->
            <!--[assign var="realtorlistitems" value=`$pubtype.fields[field].relateToPubs.publications`]-->
          <!--[/if]-->
        <!--[/section]-->
        <p>
          <label for="field-realtor">Realtor</label><br />
          <!--[foreach from=$realtorlistitems item=listitem]-->
            <input type="checkbox" id="field-realtor-<!--[$listitem.pid]-->" name="pubfields[realtor][]" value="<!--[$listitem.pid]-->" />
            <!--[php]-->for ($i = 0; $i < $this->_tpl_vars[listitem][indent]; $i++) echo('-');<!--[/php]-->
            &nbsp;<!--[$listitem.title|pnvarprepfordisplay]--><br />
          <!--[/foreach]-->
        </p>

This list is based on users active over the last 60 minutes.