つきのさんアソビバ。

IT的なことからアケゲー、オンゲなど趣味もつらつら

Visualforceハマリ。~apex:formとapex:repeat編~

メンバーから問い合わせあってちょっとハマった話。

<apex:form id="Sample">
    <apex:repeat value="{!list}" var="item">
        <apex:variable var="loopIndex" value="{! loopIndex + 1 }"/>
        <apex:pageBlock title="list">
            <apex:pageBlockSection title="list">
                <apex:inputField value="{!item.Name}"/>
                <apex:inputField value="{!item.xxx__c}"/>
                <apex:commandButton rerender="Sample" value="Save" action="{!save}">
                    <apex:param name="loopIndex" value="{!loopIndex}" assignTo="{!loopIndex}"/>
                </apex:commandButton>
            </apex:pageBlockSection>
            <apex:commandButton rerender="Sample" value="Save" action="{!save}">
                <apex:param name="loopIndex" value="{!loopIndex}" assignTo="{!loopIndex}"/>
            </apex:commandButton>
        </apex:pageBlock>
    </apex:repeat>
</apex:form>

これだとApex側のSaveメソッドでInputfieldの値がセットされていなかった。

<apex:form id="Sample">
    <apex:repeat value="{!list}" var="item">
        <apex:variable var="loopIndex" value="{! loopIndex + 1 }"/>
        <apex:pageBlock title="list">
            <apex:pageBlockSection title="list">
                <apex:inputField value="{!item.Name}"/>
                <apex:inputField value="{!item.xxx__c}"/>
            </apex:pageBlockSection>
            <apex:commandButton rerender="Sample" value="Save" action="{!save}">
                <apex:param name="loopIndex" value="{!loopIndex}" assignTo="{!loopIndex}"/>
            </apex:commandButton>
        </apex:pageBlock>
    </apex:repeat>
</apex:form>

こうしたらとりあえず解決した模様。


と思ったらちょっと違ったらしい。
そもそも前提ソースが少し違っていたようでapex:repeatの子要素にapex:formが指定されていたらしい。

developer.salesforce.com

リファレンスにも書いてあるけど、ダメな記述。

うーん、しかし何故ダメなんだろう。
しかもエラーにはならず、挙動が変わるってのはちと恐ろしい。
開発者ガイドに書いてあるとはいえ・・・。