function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
StormConsultingStormConsulting 

Create basic form

How can I create a basic form that is not bound to an object. I just want some inputFields with a submit button, that gets processed in the custom controller class, then finally output some text on the page saying if it was successful or not.

Here's what I have
Code:
<apex:form >
 To <apex:inputField id="to" />
From <apex:inputField id="from" />
Message <apex:inputField id="message" />

<apex:commandButton value="Send" action="{!sendSMS}" />
</apex:form>

I am getting an error 'value for inputField is not a dynamic binding!'

How can this be done?

Jon Mountjoy_Jon Mountjoy_
The docs seem to indicate it only works for SOjbect fields, so I don't think you can use inputField.  Which is rather a pity.

I think you can at least get some input by simply using
 inputText instead.  ie. in your form have:

 To <apex:inputText value="{!myTo}" />

 
Then you can create a String variable myTo with getters and setters in your controller.

But this isn't typed at all. ie. if myTo is a date field, you don't get a date picker - uh.   Perhaps you can create a custom object that extends SObject in some wa and reference its fields instead? (I don't know)
StormConsultingStormConsulting
I should be able to do it with just normal HTML inputs like:

<input type="text" />

The only problem is how do I have the form submit to itself and pickup the values that were submitted?

Thanks for the help
Jon Mountjoy_Jon Mountjoy_
Here's a simple Visualforce page:
<apex:page controller="NoObj" >
  <apex:form >
 To <apex:inputText value="{!myTo}" />
 <apex:commandButton value="Send" action="{!sendSMS}" />
</apex:form>
</apex:page>

 And here's a simple controller:
public class NoObj {

   public string myTo {get;set;}
  
   public PageReference sendSMS() {
      System.debug('I have access to the variable!! : ' + myTo);
      return null;
   }

}

 The method sendSMS has access to the form input (myTo) because it's bound to myTo from the Visualforce page by using the {!myTo}.

Hope that helps
Jon


StormConsultingStormConsulting
Ok thanks Jon that part is clear.

After I have accessed the inputs, is it possible to write back to the VF page with an output message? For example 'the message has been sent'.

Thanks a lot
Jon Mountjoy_Jon Mountjoy_
Yes, you can even do it with an Ajax write so that the page doesn't have to refresh.

Check out the Visualforce documentation, it should guide you on this.
(http://wiki.apexdevnet.com/index.php/Documentation)

Look for the sections on AJAX and "rerender". Look also at the example on around page 58 on Controller Methods/Setter Methods - It has a nice example ("For example, the following markup displays a page that implements basic search functionality for Leads .... ") that updates a block with search results.
StormConsultingStormConsulting
I can get away with it if I could use picklists but I don't think I can get the normal HTML elements values (<select> <input>) from the controller class can I?

Cheers
Chris
Jon Mountjoy_Jon Mountjoy_
Hi StormConsulting

Have you checked out the docs yet? They seem to have a selectList and selectOption (around page 182) that looks like it will do the trick for you. You should also be able to find them by hitting the "component reference" link in the inline Visualforce page editor.

LMK if that works.

Regards,
Jon
StormConsultingStormConsulting
Thanks Jon, I will try the select and inputText's as a last resort.

What I have now is a VF page with the standard controller on my custom object. I also have a custom controller as an extension. All of the fields are displayed fine (with the types e.g datepicker).

The submit button calls {!Save} which is a page ref in my extension class. When I try to output one of the form vars, its just ouputing the var name e.g. 'The value is: To__c'. If i can get access to these then I can keep the field types.

My VF page:

Code:
<apex:page extensions="controllerSendSMS" standardController="SentSMS__c" tabStyle="account">        
<apex:form > To <apex:inputField id="to" value="{!SentSMS__c.To__c}" /> <apex:commandButton value="Send" action="{!Save}" /> </apex:form>
</apex:page>

 Entension controller:
Code:
    public PageReference Save() {
        System.debug('The value is: ' + SentSMS__c.To__c);
        return null;
    }

 


Jon Mountjoy_Jon Mountjoy_
Well, SentSMS__c is a type, not an instance. So when you do "SentSMS__c.To__c" in your controller (extension) you're not getting the value for a field, but the title of a field. You want to grab the data from the current instance.

I suspect (i can't tell, not all the source is here), that your controller extension is not doing the work of grabbing the current object from the controller. ie. it's constructor should have something like "this.mysend = (SentSMS__c) controller.getRecord()"

Then your debug statement can use "mysend.to__c" to grab the variable.

See "Building a Controller Extension" in the Visualforce documentation.

LMK how you get on.

Regards,
Jon
StormConsultingStormConsulting
Jon,

Your suggestion worked and I hae access to the variables.

The one thing that I can't find any information about is how to write messages back to the VF page and cancel the save if needed.

For example:

1. Form is submitted
2. First run my apex code to do some verification checks (user has enough credits)
    If checks fail, show message on page 'Insufficient credits'
    If checks pass, call Send() and continue to save the record.

I appreciate your help on this!

Cheers
Chris
Jon Mountjoy_Jon Mountjoy_
Cool.

See my previous post on Ajax and rerender.
StormConsultingStormConsulting
Thanks Jon, I am showing error messages using the addError method.

In an Apex class how can I get the current Salesforce User ID?

Thanks
Chris
Jon Mountjoy_Jon Mountjoy_
Again, I suggest checking the docs. It's there somewhere. I think $User is the object. Docs should tell you.

Regards,
Jon
StormConsultingStormConsulting
Got it, thanks for the help Jon.