Visualforce apex:param bound to non-existent controller property – no error?

Discovered while debugging another developer’s code … sigh

Given a controller defined with a single getter/setter

public with sharing class SfseCtrlExample {
  public String foo {get; set;}
  public void doAction() {}
}

Given a VF page using a commandButton and commandLink that passes a parameter to existing (foo) and non-existing (bar) controller properties

<apex:page id="SfseCtrlExample" controller="SfseCtrlExample">
 <apex:form id="theForm">
    <apex:outputText value="foo paramVal={!foo}"/>
    <br/>
    <apex:commandButton value="Do Action" action="{!doAction}" reRender="theForm">
        <apex:param name="p1f" assignTo="{!foo}" value="fooValCB"/>
        <apex:param name="p1b" assignTo="{!bar}" value="barValCB"/>
    </apex:commandButton>
    <br/>
    <apex:commandLink value="Do Action (CL)" action="{!doAction}" reRender="theForm">
        <apex:param name="p2f" assignTo="{!foo}" value="fooValCL"/>
        <apex:param name="p2b" assignTo="{!bar}" value="barValCL"/>
    </apex:commandLink>
 </apex:form>
</apex:page>

When either commandButton or commandLink clicked

  • The assignTo="{!foo}" successfully passes the value to the controller (for commandButton, requires the well-known reRender workaround)

But no compile-time or run-time error results for the
assignTo="{!bar}"

I can sort of see why there’s no error for apex:param nested under apex:commandButton as that is not supported, but given that the VF page compiler can check for missing properties and methods in other components, why is it not for apex:param?

Answer

This is not strictly a bug, at least not in Visualforce terms. Allow me to explain. Unlikely many other aspects of Salesforce, Visualforce is defined by its documentation. The documentation for Visualforce is literally the specification for Visualforce. Anything that is not documented is simply undefined, even if you don’t get a compilation error.

The documentation team has spent a lot of time documenting the bugs in the platform; many of the less dangerous ones are pointed out in the documentation (this is a relatively innocent, if not hair-pulling bug), and they omit the dangerous bugs completely hoping nobody finds them (I love finding these ones).

My advice to you is to brush up on the documentation. Everything you are allowed to do is in the documentation. Everything you are not allowed to do is not in the documentation. You can choose to ignore this advice at your own peril.

For the most part, Visualforce development has finished. It’s as good as it’s ever going to get. There are no new features coming out for it, just better Lightning compatibility to encourage us to move over. To fix this bug and a myriad of others, they would most likely need a major refactor or possibly a total rewrite, and it’s simply not worth it with Lightning.

Attribution
Source : Link , Question Author : cropredy , Answer Author : sfdcfox

Leave a Comment