3

inside a Vf page i have one required field and two action buttons. When a user clicks on a specific button, the action has to start despite the required fields and the validation rules. I put the property immediate=true on the button but it doesn't work. Here my code:

<apex:PageBlock id="contratto">
    <apex:pageBlockButtons location="bottom">
        <apex:commandButton action="{!createContr}" value="Scegli i Prodotti" reRender="panprodotti" />
        <apex:commandButton action="{!goback}" value="Torna a Scheda Cliente" immediate="true" />
    </apex:pageBlockButtons>
    <apex:pageBlockSection title="Specifiche Contratto" collapsible="false">
        <apex:input required="true" label="Data inizio contratto" value="{!cData}" type="date" id="datainiziocontr" />
        <apex:input label="Durata contratto(mesi)" value="{!cDurata}" type="number" id="durcontr" />
        <apex:input label="Condizioni speciali" value="{!cCondizioni}" type="text" id="condizioni" />
    </apex:pageBlockSection>
    </apex:PageBlock>

Now when a user clicks the button "Torna a Scheda Cliente" , a message pops out because "Data inizio contratto" is required, but I want to skip it, how can i do that?

controller:

public with sharing class NuovoContrattoExstension {
    public String SelectedRowId {get; set;}
    public String retURL {get;set;}
    public Date cData {get;set;}
    public String cCondizioni {get;set;}
    public Integer cDurata {get;set;}
    public Prodotto_contratto__c prodotto{get;set;}
    public Integer gionol {get;set;}
    public Integer quant {get;set;}
    public Integer sconto {get;set;}
    public List<Prodotto_contratto__c> shoppingCart {get;set;}
    public Id accid {get;set;}
    public Contract nuovocontratto;

    public NuovoContrattoExstension(ApexPages.StandardController controller) {
    try{
        prodotto=new  Prodotto_contratto__c();
        shoppingCart= new List<Prodotto_contratto__c>();
        System.debug(ApexPages.currentPage().getParameters().get('X'));
        accid=ApexPages.currentPage().getParameters().get('X');
        System.debug(accid);
        }catch(Exception e){
         ApexPages.addMessages(e);
        }


    }

    public Pagereference deleteRow(){
     try{
        System.debug('fatto');
        System.debug(ApexPages.CurrentPage().getParameters().get('rowid'));
        System.debug(SelectedRowId);
        for (Integer i = 0; i < shoppingCart.size(); i++) {
            Prodotto_contratto__c a = shoppingCart[i];
                if (a.Id == SelectedRowId) {
                    delete a;
                    shoppingCart.remove(i);
                    break;
                    }
        }
        }catch(Exception e){
         ApexPages.addMessages(e);
        }

        return null;
    }

    public void createContr(){
     try{
        nuovocontratto= new Contract();
        nuovocontratto.AccountId=accid;
        nuovocontratto.StartDate=cData;
        nuovocontratto.ContractTerm=cDurata;
        nuovocontratto.SpecialTerms=cCondizioni;
        insert nuovocontratto;
        System.debug('contratto'+nuovocontratto.id);
        }catch(Exception e){
         ApexPages.addMessages(e);
        }
    }

    public void removeFromShoppingCart(){
    }

    public void addFromShoppingCart(){
    try{
        System.debug('Creazione Prodotto');
    prodotto.Name='prodotto in contratto';
    prodotto.Contratto__c=nuovocontratto.id;
    prodotto.Contatto__c=accid;
        System.debug(prodotto.Name);
        System.debug(prodotto.Contratto__c);
        System.debug(prodotto.Scelta__c);
        System.debug(prodotto.Quantit_prodotto__c);
        System.debug(prodotto.Contatto__c);
        System.debug(prodotto.Sconto__c);
        System.debug(prodotto.Giorni_durata_noleggio__c);
    insert prodotto;
    System.debug('prodotto '+prodotto.id);
    shoppingCart.add(prodotto);
    prodotto=new  Prodotto_contratto__c();
    }catch(Exception e){
         ApexPages.addMessages(e);
        }
    }

    public Pagereference saveContract(){ 
     Pagereference p= new ApexPages.StandardController(nuovocontratto).view();
     p.setRedirect(true);
      return p;
    }

    public Pagereference goback(){ 
    Account a=[select id from Account where id=:accid];
     Pagereference p= new ApexPages.StandardController(a).view();
     p.setRedirect(true);
      return p;
    }

    public void deleteContract(){
     try{
        System.debug(shoppingCart);
        System.debug(nuovocontratto);
        for(Prodotto_contratto__c c:shoppingCart){
            delete c;}
        delete nuovocontratto;
        nuovocontratto=new Contract();
        shoppingCart= new List<Prodotto_contratto__c>();
        System.debug(nuovocontratto);
        system.debug(shoppingCart);
        }catch(Exception e){
         ApexPages.addMessages(e);
        }

    }
}
  • if required="true" removed, does validation disappear? You can have apex validation. What do you with cData in apex? just save ? – Ilya Lepesh Jul 16 '16 at 16:07
  • yes if I remove required attribute , the validation disappear, I post the controller – L.benedettini Jul 16 '16 at 16:10
  • whatever I use cData to create a Contract record – L.benedettini Jul 16 '16 at 16:25
  • 1
    <apex:inputField required="true" label="Data inizio" value="{!nuovocontratto.cData__c}" /> – Ilya Lepesh Jul 16 '16 at 16:40
  • 1
    I think this is the answer you are looking for link – Ilya Lepesh Jul 16 '16 at 16:44
3

The reason why you have this validation, you are using apex:input tag. This tag requires html5:

<apex:page controller="myController" docType="html-5.0">

Now validation is processed by HMTL5, not VisualForce. Thats why immediate option doesn't work.

According to this you can disable html validation:

<apex:form html-novalidate="true">

Or you can use apex:inputField instead:

<apex:inputField required="true" label="Data inizio" value="{!nuovocontratto.StartDate}" />
  • Nice tidbits! I didn't know that feature could be disabled. Sweet. – Adrian Larson Jul 16 '16 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.