Problems, need help? Have a tip or advice? Post it here.
16 posts Page 2 of 2
Hi guys,

thanks for your suggestions. I had a workaround for the previous site but now for a new site I'm facing the same problem. Use case is a ecommerce site where the customer can optionally add a different shipping address than the billing address.

My idea now would be to extend the couch required-attribute.

I'm thinking of adding a "required-when=<condition>" attribute where condition would be to check a certain form field (hidden) and its value.
So it would then be something like:

Code: Select all
<cms:input type="text" value="" class="form-control" 
   name="billing_city" required_when='alternative_billing_adrr="1" '
   validator_msg='required_when=Pls enter city.'/>


Could someone give me a jumpstart as where to look and how this could be achieved? Or possibly any other ideas? I would add a ZIP but the site is currently a mess...

TA

Patrick

EDIT: It might help to see the current template ( not fully finished yet):
Code: Select all
<?php require_once 'couch/cms.php'; ?>
<cms:no_cache />
<cms:template title="Bestellung" order="23" >
  <cms:embed "templates/header_fields.inc" />
  <cms:editable type="text" name="discount_description"  label="Rabattbeschreibung" order="30"/>
  <cms:editable name="page_intro" label="Page Intro"  type="richtext" order="10"/>
  <cms:editable name="page_text" label="Page Text"  type="richtext" order="10"/>

</cms:template>

<cms:embed "html_header.inc" />
  <body>
<cms:embed "head.inc" />

      <div id="middle" class="container">
         <div class="white">

            <header class="page-header">
               <h1><cms:show page_headline /></h1>
            </header>

            <div id="shop">

               <div class="row">
                  <div class="col-md-7">
                     <cms:form method="post" anchor='0' class="white-row">
                     
                        <cms:if page_subheadline !=''>
                        <p>
                           <strong><cms:show page_subheadline /></strong>
                        </p>
                        </cms:if>
                        <cms:if page_intro !=''>
                        <p>
                           <cms:show page_intro />
                        </p>
                        </cms:if>

<cms:if k_error >
            <div class="k_errormessage">
                <ul>
                    <cms:each k_error >
                        <li><cms:show item /></li>
                    </cms:each>
                </ul>
            </div>
  </cms:if>   

                        <h5>Lieferadresse</h5>

                        <!-- SHIPPIING ADDRESS -->
                        <div class="row">
                           <div class="form-group">
                              <div class="col-md-12">
                                 <label>Land</label>
                                 <cms:input type="dropdown" name="country" class="form-control pointer" required='1'
                                     opt_values="Bitte auswählen | &nbsp; | Deutschland |  &nbsp; | Afghanistan | Ägypten | Aland | Albanien | Algerien |
                                     Amerikanisch-Samoa | Amerikanische Jungferninseln | Andorra | Angola | Anguilla |
                                     Antarktis | Antigua und Barbuda | Äquatorialguinea | Argentinien | Armenien | Aruba |
                                     Ascension | Aserbaidschan | Äthiopien | Australien | Bahamas | Bahrain | Bangladesch |
                                     Barbados | Belgien | Belize | Benin | Bermuda | Bhutan | Bolivien | Bosnien und Herzegowina |
                                     Botswana | Bouvetinsel | Brasilien | Brunei | Bulgarien | Burkina Faso | Burundi | Chile | China |
                                     Cookinseln | Costa Rica | Cote d'Ivoire | Dänemark | Deutschland | Diego Garcia | Dominica |
                                     Dominikanische Republik | Dschibuti | Ecuador | El Salvador | Eritrea | Estland | Europäische Union |
                                     Falklandinseln | Färöer | Fidschi | Finnland | Frankreich | Französisch-Guayana |
                                     Französisch-Polynesien | Gabun | Gambia | Georgien | Ghana | Gibraltar | Grenada | Griechenland |
                                     Grönland | Großbritannien | Guadeloupe | Guam | Guatemala | Guernsey | Guinea | Guinea-Bissau |
                                     Guyana | Haiti | Heard und McDonaldinseln | Honduras | Hongkong | Indien | Indonesien | Irak | Iran |
                                     Irland | Island | Israel | Italien | Jamaika | Japan | Jemen | Jersey | Jordanien | Kaimaninseln | Kambodscha |
                                     Kamerun | Kanada | Kanarische Inseln | Kap Verde | Kasachstan | Katar | Kenia | Kirgisistan | Kiribati |
                                     Kokosinseln | Kolumbien | Komoren | Kongo | Kroatien | Kuba | Kuwait | Laos | Lesotho | Lettland | Libanon |
                                     Liberia | Libyen | Liechtenstein | Litauen | Luxemburg | Macao | Madagaskar | Malawi | Malaysia | Malediven |
                                     Mali | Malta | Marokko | Marshallinseln | Martinique | Mauretanien | Mauritius | Mayotte | Mazedonien | Mexiko |
                                     Mikronesien | Moldawien | Monaco | Mongolei | Montserrat | Mosambik | Myanmar | Namibia | Nauru | Nepal |
                                     Neukaledonien | Neuseeland | Neutrale Zone | Nicaragua | Niederlande | Niederländische Antillen | Niger |
                                     Nigeria | Niue | Nordkorea | Nördliche Marianen | Norfolkinsel | Norwegen | Oman | Österreich | Pakistan |
                                     Palästina | Palau | Panama | Papua-Neuguinea | Paraguay | Peru | Philippinen | Pitcairninseln | Polen |
                                     Portugal | Puerto Rico | Réunion | Ruanda | Rumänien | Russische Föderation | Salomonen | Sambia |
                                     Samoa | San Marino | São Tomé und Príncipe | Saudi-Arabien | Schweden | Schweiz | Senegal |
                                     Serbien und Montenegro | Seychellen | Sierra Leone | Simbabwe | Singapur | Slowakei | Slowenien |
                                     Somalia | Spanien | Sri Lanka | St. Helena | St. Kitts und Nevis | St. Lucia | St. Pierre und Miquelon |
                                     St. Vincent/Grenadinen (GB) | Südafrika, Republik | Sudan | Südkorea | Suriname | Svalbard und Jan Mayen |
                                     Swasiland | Syrien | Tadschikistan | Taiwan | Tansania | Thailand | Timor-Leste | Togo | Tokelau | Tonga |
                                     Trinidad und Tobago | Tristan da Cunha | Tschad | Tschechische Republik | Tunesien | Türkei | Turkmenistan |
                                     Turks- und Caicosinseln | Tuvalu | Uganda | Ukraine | Ungarn | Uruguay | Usbekistan | Vanuatu | Vatikanstadt |
                                     Venezuela | Vereinigte Arabische Emirate | Vereinigte Staaten von Amerika | Vietnam | Wallis und Futuna |
                                     Weihnachtsinsel | Weißrussland | Westsahara | Zentralafrikanische Republik | Zypern "
                                     validator='regex=/^(?!Bitte).*$/'
                                     validator_msg='regex=Bitte wählen Sie ein Land aus der Liste aus.'
                                 />
                              </div>
                           </div>
                        </div>
                        <div class="row">
                           <div class="form-group">
                              <div class="col-md-6">
                                 <label>Vorname</label>
                                 <cms:input type="text" value="" class="form-control" name="firstname" required='1'
                                 validator_msg='required=Bitte geben Sie Ihren Vornamen ein.'/>
                              </div>
                              <div class="col-md-6">
                                 <label>Nachname</label>
                                 <cms:input type="text" value="" class="form-control" name="lastname" required='1'
                                 validator_msg='required=Bitte geben Sie Ihren Nachnamen ein.'/>
                              </div>
                           </div>
                        </div>
                        <div class="row">
                           <div class="form-group">
                              <div class="col-md-12">
                                 <label>Firma</label>
                                 <cms:input type="text" value="" class="form-control" name="company"/>
                              </div>
                           </div>
                        </div>
                        <div class="row">
                           <div class="form-group">
                              <div class="col-md-9">
                                 <label>Strasse</label>
                                 <cms:input type="text" value="" class="form-control" name="street" required='1'
                                 validator_msg='required=Bitte geben Sie eine Strasse ein.'/>
                              </div>
                              <div class="col-md-3">
                                 <label>Hausnummer</label>
                                 <cms:input type="text" value="" class="form-control" name="housenumber" required='1'
                                 validator_msg='required=Bitte geben Sie eine Hausnummer ein.'/>
                              </div>
                           </div>
                        </div>
                        <div class="row">
                           <div class="form-group">
                              <div class="col-md-3">
                                 <label>Postleitzahl</label>
                                 <cms:input type="text" value="" class="form-control" name="postalcode" required='1'
                                 validator='regex=/[0-9]{5}/'
                                 validator_msg='required=Bitte geben Sie Ihre Postleitzahl ein. | regex=Bitte geben Sie eine gültige Postleitzahl  ein.'
                                 />
                              </div>
                              <div class="col-md-9">
                                 <label>Stadt </label>
                                 <cms:input type="text" value="" class="form-control" name="city" required='1'
                                 validator_msg='required=Bitte geben Sie Ihre Stadt an.'/>
                              </div>
                           </div>
                        </div>
                        <!-- /SHIPPIING ADDRESS -->


                        <div class="alert alert-default shop-cart-checkout-alert">

                           <label class="pointer">
                              <input type="checkbox" id="show-hide" data-for="shop-cart-billing" onclick="addShippingAddress();"/>
                              ANDERE RECHNUNGSADRESSE
                           </label>

                           <input type="hidden" id="shipping_addr" value="0" />
                           <!-- BILLING ADDRESS -->
                           <div id="shop-cart-shipping">
                              <div class="row">
                           <div class="form-group">
                              <div class="col-md-12">
                                 <label>Land</label>
                                 <cms:input type="dropdown" name="billing_country" class="form-control pointer" required='1'
                                     opt_values="Bitte auswählen | &nbsp; | Deutschland |  &nbsp; | Afghanistan | Ägypten | Aland | Albanien | Algerien |
                                     Amerikanisch-Samoa | Amerikanische Jungferninseln | Andorra | Angola | Anguilla |
                                     Antarktis | Antigua und Barbuda | Äquatorialguinea | Argentinien | Armenien | Aruba |
                                     Ascension | Aserbaidschan | Äthiopien | Australien | Bahamas | Bahrain | Bangladesch |
                                     Barbados | Belgien | Belize | Benin | Bermuda | Bhutan | Bolivien | Bosnien und Herzegowina |
                                     Botswana | Bouvetinsel | Brasilien | Brunei | Bulgarien | Burkina Faso | Burundi | Chile | China |
                                     Cookinseln | Costa Rica | Cote d'Ivoire | Dänemark | Deutschland | Diego Garcia | Dominica |
                                     Dominikanische Republik | Dschibuti | Ecuador | El Salvador | Eritrea | Estland | Europäische Union |
                                     Falklandinseln | Färöer | Fidschi | Finnland | Frankreich | Französisch-Guayana |
                                     Französisch-Polynesien | Gabun | Gambia | Georgien | Ghana | Gibraltar | Grenada | Griechenland |
                                     Grönland | Großbritannien | Guadeloupe | Guam | Guatemala | Guernsey | Guinea | Guinea-Bissau |
                                     Guyana | Haiti | Heard und McDonaldinseln | Honduras | Hongkong | Indien | Indonesien | Irak | Iran |
                                     Irland | Island | Israel | Italien | Jamaika | Japan | Jemen | Jersey | Jordanien | Kaimaninseln | Kambodscha |
                                     Kamerun | Kanada | Kanarische Inseln | Kap Verde | Kasachstan | Katar | Kenia | Kirgisistan | Kiribati |
                                     Kokosinseln | Kolumbien | Komoren | Kongo | Kroatien | Kuba | Kuwait | Laos | Lesotho | Lettland | Libanon |
                                     Liberia | Libyen | Liechtenstein | Litauen | Luxemburg | Macao | Madagaskar | Malawi | Malaysia | Malediven |
                                     Mali | Malta | Marokko | Marshallinseln | Martinique | Mauretanien | Mauritius | Mayotte | Mazedonien | Mexiko |
                                     Mikronesien | Moldawien | Monaco | Mongolei | Montserrat | Mosambik | Myanmar | Namibia | Nauru | Nepal |
                                     Neukaledonien | Neuseeland | Neutrale Zone | Nicaragua | Niederlande | Niederländische Antillen | Niger |
                                     Nigeria | Niue | Nordkorea | Nördliche Marianen | Norfolkinsel | Norwegen | Oman | Österreich | Pakistan |
                                     Palästina | Palau | Panama | Papua-Neuguinea | Paraguay | Peru | Philippinen | Pitcairninseln | Polen |
                                     Portugal | Puerto Rico | Réunion | Ruanda | Rumänien | Russische Föderation | Salomonen | Sambia |
                                     Samoa | San Marino | São Tomé und Príncipe | Saudi-Arabien | Schweden | Schweiz | Senegal |
                                     Serbien und Montenegro | Seychellen | Sierra Leone | Simbabwe | Singapur | Slowakei | Slowenien |
                                     Somalia | Spanien | Sri Lanka | St. Helena | St. Kitts und Nevis | St. Lucia | St. Pierre und Miquelon |
                                     St. Vincent/Grenadinen (GB) | Südafrika, Republik | Sudan | Südkorea | Suriname | Svalbard und Jan Mayen |
                                     Swasiland | Syrien | Tadschikistan | Taiwan | Tansania | Thailand | Timor-Leste | Togo | Tokelau | Tonga |
                                     Trinidad und Tobago | Tristan da Cunha | Tschad | Tschechische Republik | Tunesien | Türkei | Turkmenistan |
                                     Turks- und Caicosinseln | Tuvalu | Uganda | Ukraine | Ungarn | Uruguay | Usbekistan | Vanuatu | Vatikanstadt |
                                     Venezuela | Vereinigte Arabische Emirate | Vereinigte Staaten von Amerika | Vietnam | Wallis und Futuna |
                                     Weihnachtsinsel | Weißrussland | Westsahara | Zentralafrikanische Republik | Zypern "
                                     validator='regex=/^(?!Bitte).*$/'
                                     validator_msg='regex=Bitte wählen Sie ein Land aus der Liste aus.'
                                 />
                              </div>
                           </div>
                        </div>
                        <div class="row">
                           <div class="form-group">
                              <div class="col-md-6">
                                 <label>Vorname</label>
                                 <cms:input type="text" value="" class="form-control" name="billing_firstname" required='1'
                                 validator_msg='required=Bitte geben Sie Ihren Vornamen ein.'/>
                              </div>
                              <div class="col-md-6">
                                 <label>Nachname</label>
                                 <cms:input type="text" value="" class="form-control" name="billing_lastname" required='1'
                                 validator_msg='required=Bitte geben Sie Ihren Nachnamen ein.'/>
                              </div>
                           </div>
                        </div>
                        <div class="row">
                           <div class="form-group">
                              <div class="col-md-12">
                                 <label>Firma</label>
                                 <cms:input type="text" value="" class="form-control" name="billing_company"/>
                              </div>
                           </div>
                        </div>
                        <div class="row">
                           <div class="form-group">
                              <div class="col-md-9">
                                 <label>Strasse</label>
                                 <cms:input type="text" value="" class="form-control" name="billing_street" required='1'
                                 validator_msg='required=Bitte geben Sie eine Strasse ein.'/>
                              </div>
                              <div class="col-md-3">
                                 <label>Hausnummer</label>
                                 <cms:input type="text" value="" class="form-control" name="billing_housenumber" required='1'
                                 validator_msg='required=Bitte geben Sie eine Hausnummer ein.'/>
                              </div>
                           </div>
                        </div>
                        <div class="row">
                           <div class="form-group">
                              <div class="col-md-3">
                                 <label>Postleitzahl</label>
                                 <cms:input type="text" value="" class="form-control" name="billing_postalcode" required='1'
                                 validator='regex=/[0-9]{5}/'
                                 validator_msg='required=Bitte geben Sie Ihre Postleitzahl ein. | regex=Bitte geben Sie eine gültige Postleitzahl  ein.'
                                 />
                              </div>
                              <div class="col-md-9">
                                 <label>Stadt </label>
                                 <cms:input type="text" value="" class="form-control" name="billing_city" required='1'
                                 validator_msg='required=Bitte geben Sie Ihre Stadt an.'/>
                              </div>
                           </div>
                        </div>
                        </div></div>
                        <!-- /BILLING ADDRESS -->

                        <!-- PAYMNET SELECTOR -->
                        <div class="alert alert-default shop-cart-checkout-alert">

                           <label class="pointer">
                              <input type="radio" name="payment_method" value="1" checked="checked" /> <strong>Direct Bank Transfer</strong>
                           </label>

                           <label class="pointer">
                              <input type="radio" name="payment_method" value="2" /> <strong>Cheque Payment</strong>
                           </label>

                           <label class="pointer">
                              <input type="radio" name="payment_method" value="3" /> <strong>Paypal</strong>
                           </label>

                        </div>
                        <!-- /PAYMNET SELECTOR -->

                        <!-- AGREE -->
                        <div class="row">
                           <div class="col-md-12">
                              <label class="pointer shop-cart-agree">
                                 <input type="checkbox" name="agree" value="1" /> I understand and agree the <a href="page-terms-and-conditions.html" target="_blank">terms and conditions</a>.
                              </label>
                           </div>
                        </div>
                        <!-- /AGREE -->

                        <button class="btn btn-primary btn-lg"><i class="fa fa-check"></i> &nbsp; PLACE ORDER</button>
                     </cms:form>

                  </div>

                  <div class="col-md-5">

                     <h3><strong>Discount</strong> Code</h3>
                     <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur pellentesque neque eget diam porta.</p>

                     <div class="alert alert-success">
                        <i class="fa fa-check-circle"></i>
                        <strong>CODE-12345</strong> - 10% Discount!
                     </div>

                     <div class="alert alert-danger">
                        <i class="fa fa-frown-o"></i>
                        Sorry, Invalid Code!
                     </div>

                     <form class="input-group" method="post" action="#">
                        <input type="text" class="form-control" name="k" id="k" value="" placeholder="Enter Coupon Code" />
                        <span class="input-group-btn">
                           <button class="btn btn-primary">Apply Code</button>
                        </span>
                     </form>

                     <div class="shop-cart-final-payment text-right">
                        <hr />
                        <span class="block fsize13">Shipping: $0</span>
                        <span class="block fsize13">Discount: $0</span>
                        <span class="fsize20 styleSecondColor bold">TOTAL: $64</span>
                     </div>

                  </div>
               </div>


            </div>

         </div>
      </div>




<cms:embed "footer.inc" />
  </body>
</html>
<?php COUCH::invoke(); ?>
Perhaps the following thread would help -
viewtopic.php?f=4&t=9118
Hi KK,

thanks! Exactly what I needed!

Thanks alot!
Patrick
Hi KK,

sorry -it's me again ;). I must have gotten something wrong:
I have added the new custom validator to my fields (I have several fields which need to be checked, not just one as in above example).
In functions.php I have added the following:
Code: Select all
function required_on_billing( $field, $args ) {    
    $field_name = $field->name;
    error_log("entered required_on_billing with field_name=".$field_name );
    if( isAdressCheckRequired($field) ) {     
        // find the 'address' field and check that it is not empty
        foreach( $field->siblings as $f ) {
          error_log("Found a field " . $f->name);
            if ($f->name==$field_name && $field_name=='billing_country') {
              $val = trim( $f->get_data() );             
              if (preg_match('/^Bitte/', $val)) {         
                  error_log(" ERR_MATCH -  billing_country: " .$val);     
                  return  KFuncs::raise_error( "" );                   
                }
                break;           
            } elseif ( $f->name==$field_name && $field_name='billing_firstname') { //if ende
                $val = trim( $f->get_data() );               
                if( !strlen($val) ){
                  error_log(" ERR_MATCH - billing_firstname: " .$val);
                  return KFuncs::raise_error( "" );           
                }
                break;             
            }  //else if ende
        }  // for each ende
    } // if check addr ende

}

function isAdressCheckRequired ( $field ){
    foreach( $field->siblings as $f ) {
        if( $f->name=='alt_billing_addr' ) {
            $val = trim( $f->get_data() );
            if( $val=='alt_billing' ){
                error_log("Alternative Adress check required");
                return true;
            }           
        }
    }
    return false;
}


It runs through the validator code for he first field using the declared validator but non of the other fields are running into the validator method.

What am I missing??

Thanks in advance
Hi Patrick,

It is understandable if you had difficulty in coding up the right PHP.

I can get it done for you but you'll have to make things a little easier for me - please clean up the form code you posted above. Remove all unnecessary markup, shorten up the long list of countries to just a couple and leave only the editable regions in there that would be directly involved in the PHP solution.

Please do that and I'll try to create the a fix for it.

Thanks.
Hi KK,

thanks for your offer and sorry for all the bulky code.
I have tried to clean everything and make it as understandable as possible.


Form Page:
Code: Select all
<!-- form page .-->
<?php require_once 'couch/cms.php'; ?>
<cms:no_cache />
<html>
  <head></head>
  <body>
    <cms:form method="post" >
      <!-- This field is always equired -->
      <cms:input type="text" value="" name="firstname" required='1'
                      validator_msg='required=Pls enter firstname.'/> 
      <!-- Switch for deciding  if alternatefield is required -->
      <cms:input type="checkbox" id="show-hide" name="field_switch"  opt_values="switch"/>                     
      <!-- This field is to be required when switch is set -->
      <cms:input type="text" value="" name="switch_firstname" validator='required_on_switch'  validator_msg='required_on_switch=Pls enter switch firstname.' />
      <!-- This field is to be required when switch is set -->
      <cms:input type="text" value="" name="switch_lastname" validator='required_on_switch'  validator_msg='required_on_switch=Pls enter switch lastname.' />
    </cms:form>
  </body>
</html> 
<?php COUCH::invoke(); ?>


kfunctions.php
Code: Select all
<!-- kfunctions.php -->
[...]
function required_on_swtch ( $field, $args ) {   
    $isSwitchSet = false;
    foreach( $field->siblings as $f ) {
        if( $f->name=='field_switch' ) {
            $val = trim( $f->get_data() );
            if( $val=='switch' ){             
                $isSwitchSet =  true;
            }           
        }
    }
    if( $isSwitchSet ) {     
        // find the 'address' field and check that it is not empty
        foreach( $field->siblings as $f ) {         
           if ( $field_name=='switch_firstname' ) {
              $val = trim( $f->get_data() );               
              if( !strlen($val) ){                 
                return KFuncs::raise_error( "ERR: switch firstname is empty" );           
              }
              break;             
            }  elseif ($field_name=='switch_lastname') {
               $val = trim( $f->get_data() );               
                if( !strlen($val) ){                 
                  return KFuncs::raise_error( "ERR: switch lastname is empty" );           
                }
                break;     
            }
        }
    }
}


When form is submitted and both fields where nt set the error result should show both error messages.


Again, thanks for your support upfront!
P
16 posts Page 2 of 2