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
Thibault WeberThibault Weber 

Problem on rerender dynamicComponent

Hi,

 

I have a problem with my visualforce page.

 

This the kind of page/result I want to (in a simplified version):

 

<apex:page controller="Test1" >
	<apex:form >
		<apex:pageBlock>
			<apex:pageBlockButtons location="top">
				<apex:commandButton value="Save" action="{!saveNewRecord}" rerender="newRecordForm" />
			</apex:pageBlockButtons>
			
			<apex:pageBlockSection id="newRecordForm" >
				<apex:inputField value="{!newRecord['Name']}"/>
				<apex:inputField value="{!newRecord['Date__c']}" required="true"/>
			</apex:pageBlockSection>			
		</apex:pageBlock>
	</apex:form>
</apex:page>

 

public with sharing class Test1 {
	
	public sobject newRecord{get;set;}
	
	public Test1(){
		 this.newRecord = Schema.getGlobalDescribe().get('MyObject__c').newSObject();
	}
	
	public void saveNewRecord(){
		//Save Here
	}
}

 When I just fill the "Name" field and save, it works fine:

 

Name:
{My Value}
Date:
{empty}
  [ 18/06/2012 ]
Error: You must enter a value

 

 

Now I try do the same but with a dynamicComponent (I need to for my non-simplified page):

 

<apex:page controller="Test2" >
	<apex:form >
		<apex:pageBlock>
			<apex:pageBlockButtons location="top">
				<apex:commandButton value="Save" action="{!saveNewRecord}" rerender="newRecordForm" />
			</apex:pageBlockButtons>
			
			<apex:dynamicComponent componentValue="{!newRecordForm}"/>		
		</apex:pageBlock>
	</apex:form>
</apex:page>

 

public with sharing class Test2{
	
	public sobject newRecord{get;set;}
	
	public Test2(){
		 this.newRecord = Schema.getGlobalDescribe().get('MyObject__c').newSObject();
	}
	
	public Component.Apex.PageBlockSection getNewRecordForm(){
		
		Component.Apex.PageBlockSection form = new Component.Apex.PageBlockSection(id = 'newRecordForm');
		
		Component.Apex.inputField nameField = new Component.Apex.inputField();
		nameField.expressions.value = '{!newRecord[\'Name\']}';
			
		Component.Apex.inputField dateField = new Component.Apex.inputField(required = true);
		dateField.expressions.value = '{!newRecord[\'Date__c\']}';
		
		form.childComponents.add(nameField);
		form.childComponents.add(dateField);
		return form; 
	}
	
	public void saveNewRecord(){
		//Save Here
	}
}

 

 When I just fill the "Name" field and save, the "Name" value disappears on rerender:


Name:
{EMPTY}
Date:
{empty}
  [ 18/06/2012 ]
Error: You must enter a value

 

The newRecord controller variable doesn't seems to be updated here. But if I fill both fields, there is no error and the saveNewRecord() method retrieves both values...


I hope I was clear and that someone could help me.

 

Thanks

 

aballardaballard

There have been some issues with inputField losing input values when a validation error occurs.  Sounds like you ahve found another one.   You should open a support case.

Thibault WeberThibault Weber

I did, but if it's an Apex/VF bug it won't be resolved before a long time and I need it now...

 

 

Worst solution ever:

 

<apex:commandButton value="Save" action="{!saveNewRecord}" rerender="newRecordForm" onclick="fnSaveInputValues();" oncomplete="fnLoadInputValues();"/>

 

With something like:

	<script type="text/javascript">
		$$ = jQuery.noConflict();
		
		var oInputValues = {};
		
		function fnSaveInputValues(){
			$$("theFormSelector").find(':input[type!=button]').each(function(){
				if(this.id)
					oInputValues[this.id] = this.value;
			});
		}
		
		function fnLoadInputValues(){
			$$.each(oInputValues, function(id, value){
				document.getElementById(id).value = value;
			});
		}
	</script>

 

I'm waiting for my case response because I really don't like this "solution".

Chris TquilaChris Tquila

dirty solution but still one solution ...

 

I ameliorated a bit your saveInputFunction to get also select fields. This looks now like that:

function fnSaveInputValues(){
            $("div.pbBody").find('input,select').each(function(){
                if(this.id)
                    oInputValues[this.id] = $(this).val();
            });
        }

 

I'm not sure how that works with multiselect but I don't need that for my code so that's fine.