You need to sign in to do that
Don't have an account?
How to give "required field" before creating the new record through controller in visualforce
Hi All,
I am trying to create mandatory field through controller. But, I am not getting any output.
Bellow is my code. I want User_Name__C and Password__C as required fields.
VF Page :
<apex:page standardController="Registration__c" extensions="RegistrationExtention" >
<script>
function mySubmit(){
var sub=confirm("Are you Sure, You want to submit the data");
if(sub==true){
return true;
}
else{
return false;
}
}
</script>
<style>
</style>
<apex:form id="frm" >
<apex:pageBlock id="pb">
<div style="height:400px;">
<table bgcolor="#CEF6E3" align="center" style=" width:100%; height:400px; font-size:15px; " >
<center>
<tr >
<td>
<tr>
<td>
<tr>
<td >
<apex:outputLabel style="margin-left:410px;" >Last Name</apex:outputLabel>
<td>
<apex:inputField value="{!reg.Name}" style="margin-left:-400px;" />
</td>
</td>
</tr>
</td>
</tr>
</td>
</tr>
<tr>
<td>
<apex:outputLabel style="margin-left:315px; padding:10%">First Name</apex:outputLabel>
</td><td>
<apex:inputField value="{!reg.First_Name__c}" style="margin-left:-400px;"/>
</td>
</tr>
<tr>
<td>
<apex:outputLabel style="margin-left:315px; padding:10%" >Gender</apex:outputLabel>
</td><td>
<apex:inputField value="{!reg.Gender__c}" style="margin-left:-400px;" / >
</td>
</tr>
<tr>
<td>
<apex:outputLabel style="margin-left:315px; padding:10%">EMail</apex:outputLabel>
</td><td>
<apex:inputField value="{!reg.Mail_Id__c}" style="margin-left:-400px;"/>
</td>
</tr>
<tr>
<td>
<apex:outputLabel style="margin-left:315px; padding:10%">Phone </apex:outputLabel>
</td><td>
<apex:inputField value="{!reg.Phone__c}" style="margin-left:-400px;"/>
</td>
</tr>
<tr>
<td>
<apex:outputLabel style="margin-left:315px; padding:10%">DateOfBirth </apex:outputLabel>
</td><td>
<apex:inputField value="{!reg.Date_of_Birth__c}" style="margin-left:-400px;"/>
</td>
</tr>
<tr>
<td>
<apex:outputLabel style="margin-left:315px; padding:10%">Username </apex:outputLabel>
</td><td>
<apex:inputField value="{!reg.User_Name__c}" style="margin-left:-400px;"/>
</td>
</tr>
<tr>
<td>
<apex:outputLabel style="margin-left:315px; padding:10%">Password</apex:outputLabel>
</td><td>
<apex:inputField value="{!reg.Password__c}" style="margin-left:-400px;"/>
</td>
</tr>
<tr>
<td>
<apex:outputLabel style="margin-left:315px; padding:8%" > Confirm Password <sup Style="color:Red;font-size:15px">*</sup> </apex:outputLabel>
</td><td>
<apex:inputField value="{!reg.Confirm_Password__c}" style="margin-left:-400px;"/>
</td>
</tr>
<tr>
<td>
<apex:commandButton style="margin-left:500px" value="Submit" action="{!dosave}" onclick="{return mySubmit();}" reRender="Error" />
</td><td>
<apex:outputLink value="https://c.ap2.visual.force.com/apex/loginpage" >Sign In</apex:outputLink>
<apex:pageMessages id="Error"></apex:pageMessages>
</td>
</tr>
</center>
</table>
</div>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller :
public class RegistrationExtention {
public Registration__c reg { get; set;}
public RegistrationExtention(ApexPages.StandardController controller) {
reg=new Registration__c();
}
public PageReference dosave(){
Registration__c reg1=new Registration__c();
reg1.Name=reg.Name;
reg1.First_Name__c=reg.First_Name__c;
reg1.Gender__c=reg.Gender__c;
reg1.Mail_Id__c=reg.Mail_Id__c;
reg1.Phone__c=reg.Phone__c;
reg1.Date_of_Birth__c=reg.Date_of_Birth__c;
reg1.User_Name__c=reg.User_Name__c;
reg1.Password__c=reg.Password__c;
reg1.Confirm_Password__c=reg.Confirm_Password__c;
if(reg1.User_Name__c==null || reg1.User_Name__c==''){
reg.User_Name__c.addError('It is required Field');
//ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.FATAL, 'Name is required.'));
return null;
}else{
insert reg1;
PageReference pr=new PageReference ('/apex/loginpage');
pr.setredirect(true);
return pr;
}
}
}
Please , help me where i did mistake
thanks in Advance
Thulasi
I am trying to create mandatory field through controller. But, I am not getting any output.
Bellow is my code. I want User_Name__C and Password__C as required fields.
VF Page :
<apex:page standardController="Registration__c" extensions="RegistrationExtention" >
<script>
function mySubmit(){
var sub=confirm("Are you Sure, You want to submit the data");
if(sub==true){
return true;
}
else{
return false;
}
}
</script>
<style>
</style>
<apex:form id="frm" >
<apex:pageBlock id="pb">
<div style="height:400px;">
<table bgcolor="#CEF6E3" align="center" style=" width:100%; height:400px; font-size:15px; " >
<center>
<tr >
<td>
<tr>
<td>
<tr>
<td >
<apex:outputLabel style="margin-left:410px;" >Last Name</apex:outputLabel>
<td>
<apex:inputField value="{!reg.Name}" style="margin-left:-400px;" />
</td>
</td>
</tr>
</td>
</tr>
</td>
</tr>
<tr>
<td>
<apex:outputLabel style="margin-left:315px; padding:10%">First Name</apex:outputLabel>
</td><td>
<apex:inputField value="{!reg.First_Name__c}" style="margin-left:-400px;"/>
</td>
</tr>
<tr>
<td>
<apex:outputLabel style="margin-left:315px; padding:10%" >Gender</apex:outputLabel>
</td><td>
<apex:inputField value="{!reg.Gender__c}" style="margin-left:-400px;" / >
</td>
</tr>
<tr>
<td>
<apex:outputLabel style="margin-left:315px; padding:10%">EMail</apex:outputLabel>
</td><td>
<apex:inputField value="{!reg.Mail_Id__c}" style="margin-left:-400px;"/>
</td>
</tr>
<tr>
<td>
<apex:outputLabel style="margin-left:315px; padding:10%">Phone </apex:outputLabel>
</td><td>
<apex:inputField value="{!reg.Phone__c}" style="margin-left:-400px;"/>
</td>
</tr>
<tr>
<td>
<apex:outputLabel style="margin-left:315px; padding:10%">DateOfBirth </apex:outputLabel>
</td><td>
<apex:inputField value="{!reg.Date_of_Birth__c}" style="margin-left:-400px;"/>
</td>
</tr>
<tr>
<td>
<apex:outputLabel style="margin-left:315px; padding:10%">Username </apex:outputLabel>
</td><td>
<apex:inputField value="{!reg.User_Name__c}" style="margin-left:-400px;"/>
</td>
</tr>
<tr>
<td>
<apex:outputLabel style="margin-left:315px; padding:10%">Password</apex:outputLabel>
</td><td>
<apex:inputField value="{!reg.Password__c}" style="margin-left:-400px;"/>
</td>
</tr>
<tr>
<td>
<apex:outputLabel style="margin-left:315px; padding:8%" > Confirm Password <sup Style="color:Red;font-size:15px">*</sup> </apex:outputLabel>
</td><td>
<apex:inputField value="{!reg.Confirm_Password__c}" style="margin-left:-400px;"/>
</td>
</tr>
<tr>
<td>
<apex:commandButton style="margin-left:500px" value="Submit" action="{!dosave}" onclick="{return mySubmit();}" reRender="Error" />
</td><td>
<apex:outputLink value="https://c.ap2.visual.force.com/apex/loginpage" >Sign In</apex:outputLink>
<apex:pageMessages id="Error"></apex:pageMessages>
</td>
</tr>
</center>
</table>
</div>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller :
public class RegistrationExtention {
public Registration__c reg { get; set;}
public RegistrationExtention(ApexPages.StandardController controller) {
reg=new Registration__c();
}
public PageReference dosave(){
Registration__c reg1=new Registration__c();
reg1.Name=reg.Name;
reg1.First_Name__c=reg.First_Name__c;
reg1.Gender__c=reg.Gender__c;
reg1.Mail_Id__c=reg.Mail_Id__c;
reg1.Phone__c=reg.Phone__c;
reg1.Date_of_Birth__c=reg.Date_of_Birth__c;
reg1.User_Name__c=reg.User_Name__c;
reg1.Password__c=reg.Password__c;
reg1.Confirm_Password__c=reg.Confirm_Password__c;
if(reg1.User_Name__c==null || reg1.User_Name__c==''){
reg.User_Name__c.addError('It is required Field');
//ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.FATAL, 'Name is required.'));
return null;
}else{
insert reg1;
PageReference pr=new PageReference ('/apex/loginpage');
pr.setredirect(true);
return pr;
}
}
}
Please , help me where i did mistake
thanks in Advance
Thulasi
<apex:inputField value="{!reg.User_Name__c}" style="margin-left:-400px;" required="true"/>.
In your case i feel pr.setredirect(false); should work.
Thanks for your reply,
I am trying though controller. If Field is empty then i needs to through error. I know, how to implement through this code <apex:inputField value="{!reg.User_Name__c}" style="margin-left:-400px;" required="true"/>.. please, tell me is there any way to implement via contoller.
1) http://www.sfdcpoint.com/salesforce/show-error-message-visualforce-page/
2) http://www.oyecode.com/2014/04/apex-messages-in-salesforce-show.html
3) http://www.infallibletechie.com/2012/10/how-to-display-error-messages-in.html
Ideally this is how you show error messages using Apex <apex:pageMessages id="showmsg"></apex:pageMessages>
In your it look liye you are not refreshing full page. Try to remove reRender and onclick method. I hope that will work
<apex:commandButton style="margin-left:500px" value="Submit" action="{!dosave}" />