Correct way to work with relationships?
Posted: 04 January 2011 04:52 AM   [ Ignore ]
Wallflower
Avatar
Rank
Total Posts:  8
Joined  2010-12-02

What’s the proper way to pull a custom field in from one channel into a SafeCracker form which is setup for another channel? For example, here’s what I’m trying to do:

{exp:safecracker channel="employment_application" return="main/index" url_title="{segment_3}" author_only="yes" preserve_checkboxes="yes"}
    
<ul>
        <
li>
            <
label for="appFirstLastName">First &ampLast Name</label><br />
            <
input type="text" name="title" id="appFirstLastName" value="{title}" />
        </
li>
        <
li>
            <
label for="appPhoneNumber">Your Phone Number</label><br />
            <
input type="text" name="emp_app_phone_number" id="appPhoneNumber" value="{emp_app_phone_number}" />
        </
li>
        ...
        <
li>
            
Your Desired Position<br />

            <!-- 
The below custom fieldapplication_positionis actually from a different channel called 'emp_app_positions' -->

            <
select name="application_position" id="application_position">
                
{options:application_position}
                    
<option value="{option_value}"{selected}>{option_name}</option>
                
{/options:application_position}
            
</select>

            <!-- ================================================================== -->

        </
li>
        ...
        <
li>
            
Your Specialized Skills<br />
            
{options:emp_app_specialized_skills}
                
<label><input type="checkbox" name="emp_app_specialized_skills[]" value="{option_value}"{checked} /> {option_name}</label><br />
            
{/options:emp_app_specialized_skills}
        
</li>
        <
li>
            
Are You 18 Years Old or Older?<br />
            
{options:emp_app_18_or_older}
                
<label><input type="radio" name="emp_app_18_or_older" value="{option_value}"{checked} /> {option_name}</label><br />
            
{/options:emp_app_18_or_older}
        
</li>
        <
li>
            <
input type="submit" name="submit" value="Submit" />
        </
li>
    </
ul>
{/exp:safecracker} 

You can see in the commented-out code above that there’s one particular field that I want to populate as a select drop-down from another channel altogether. My client asked for the ability to add/edit/remove new positions to their application, so I made a new channel, put a custom field in (called application_position), but I have no idea what I’m supposed to write in the template to grab each entry in that channel and drop it into a select drop-down.

Any tips?

Profile
 
 
Posted: 04 January 2011 04:54 AM   [ Ignore ]   [ # 1 ]
Teen Scream
Avatar
RankRankRank
Total Posts:  3541
Joined  2009-05-29

Is application_position a native EE Relationship fieldtype? If so, you should do {field:application_position}.

Profile
 
 
Posted: 04 January 2011 05:01 AM   [ Ignore ]   [ # 2 ]
Wallflower
Avatar
Rank
Total Posts:  8
Joined  2010-12-02

Thanks for the fast reply! So, should my code become:

{field:application_position}
        {options
:application_position}
                
<option value="{option_value}"{selected}>{option_name}</option>
        
{/options:application_position}
{
/field:application_position} 

(I swapped only the first and last tags) â?¦ nothing else should change?

Profile
 
 
Posted: 04 January 2011 05:04 AM   [ Ignore ]   [ # 3 ]
Teen Scream
Avatar
RankRankRank
Total Posts:  3541
Joined  2009-05-29

Actually you just have to do this, in place of the whole <select>

{field:application_position} 

Which will print out the select for you. It’s probably a good idea for me to add the ability to use the options tag pair with relationship fields in the future, but for now you’re pretty much just stuck with the field single tag.

Profile
 
 
Posted: 04 January 2011 05:05 AM   [ Ignore ]   [ # 4 ]
Teen Scream
Avatar
RankRankRank
Total Posts:  3541
Joined  2009-05-29
<li>
            
Your Desired Position<br />

            <!-- 
The below custom fieldapplication_positionis actually from a different channel called 'emp_app_positions' -->

            
{field:application_position}

            
<!-- ================================================================== -->

        </
li
Profile
 
 
Posted: 04 January 2011 05:17 AM   [ Ignore ]   [ # 5 ]
Wallflower
Avatar
Rank
Total Posts:  8
Joined  2010-12-02

Well holy smokes � that was easy. Thanks Rob!

Profile