+ Start a Discussion
renuamirenuami 

commandlink target=_blank

 

Hi

 

If i do not select any color the value of colr is null. when Submit button is clicked for the code below just the outputText value should be displayed.

 

But a new window is openening and the same test1 page is appearing with the outPuttext in the new Window.

 

 If the color value is null or empty then the new window should not be opened. How can we restrict this?

 

Please advise.Thanks

 

 

<apex:page controller="test1">
<apex:form >
<apex:pageBlock>
<apex:outputText value="please select color" rendered="{!err}"/>
<apex:outputLabel value="Select color" />
<apex:selectRadio value="{!colr}">
<apex:selectOptions value="{!items}"/>
</apex:selectRadio>
<apex:CommandLink action="{!myaction}" target="_blank">Submit</apex:CommandLink>
</apex:PageBlock> </form> </page> public class test1 { public String colr{get;set;} Public Boolean err=false; public Boolean geterr(){ return err;} public List<SelectOption> getItems() { List<SelectOption> options = new List<SelectOption>(); options.add(new SelectOption('Red','Red')); options.add(new SelectOption('Blue','Blue')); return options; } public PageReference myaction() { if(colr == null || colr == '') { err=true; return null; } else { PageReference pr = new PageReference('/apex/test2'); return pr; } } }

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bmabma

Do you need to open a new page, or a redirect would be sufficient for what you are trying to do?

 

To Redirect:

Remove the target attribute, and in your controller change the return null in "myaction()" to return Page.(second page name).

 

To Open a new Page:

You probably would need to do some javascript validation and open the new window if the validation is successful.

All Answers

renuamirenuami

 

 

Please help!!!

Bulent703Bulent703

Just an idea...

 

First place onchange methods on your radio buttons:

 

onchange="setColor(this)";

 

setColor is a javascript such as:

 

<script>

var color='';

function setColor(o)

{

color = o.value;

}

</script>

 

Then in your commandlink place an onclick method:

 

onclick="checkColor()"

 

checkColor function will check if color is selected or not

 

<script>

function checkColor

{

if (color == '')

{

alert('Please select color');

return false;

}

}

</script>

 

This will not submit the page. I'm not sure if this will work but give it a try.

bmabma

Do you need to open a new page, or a redirect would be sufficient for what you are trying to do?

 

To Redirect:

Remove the target attribute, and in your controller change the return null in "myaction()" to return Page.(second page name).

 

To Open a new Page:

You probably would need to do some javascript validation and open the new window if the validation is successful.

This was selected as the best answer