We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 52227
    • 2 Posts
    I've been trying to figure out this issue and maybe I'm at the point where I can't see the forest from the trees; any help is appreciated.

    So I have a Promo Code form on the a page where you enter in a code and it sets a session and sends a success message, this is the structure I have:

    Resource (test-form) - calls the snippet [[!formPromoCode]]
    [[!formPromoCode]]

    Snippet (formPromoCode) - sets SESSION and success message
    if (!isset($_SESSION['promo_code'])) {
    
      $output = $modx->getChunk('formPromoCode');
    
      return $output;
    
    } else {
      $output = 'You\'re using Promo Code: ' . $_SESSION['promo_code'];
    
      return $output;
    }

    Chunk (formPromoCode) - shows form using FormIt.
    [[!FormIt?
      &customValidators=`validateCoupon`
      &validate=`promo_code:required:minLength=^6^:validateCoupon`
      &promo_code.vTextRequired=`Please enter a Promo Code.`
      &promo_code.vTextMinLength=`Promo Code must have at least 6 characters in length.`
      &submitVar=`promo-code-submit`
    ]]
    
    <form id="coupon-form" action="[[~[[*id]]]]" method="POST">
      <!-- [[!+fi.validation_error_message:notempty=`<span class="payment-errors">[[!+fi.validation_error_message]] </span>`]] -->
      <input type="hidden" name="nospam:blank" value="[[+fi.nospam]]" />
      <div class="row">
        <div class="large-12 columns">
          <div class="row collapse">
            [[!+fi.error.promo_code:notempty=`[[!+fi.error.promo_code]]`]]
            <div class="small-8 columns">
              <input type="text" name="promo_code" value="[[!+fi.coupon_code]]" />
            </div>
            <div class="small-4 columns"><input class="button postfix" type="submit" name="promo-code-submit" value="Apply" /></div>
          </div>
        </div>
      </div>
    </form>


    And the customValidator for FormIt
    if($value != '123456'){
       $validator->addError($key,'Promo Code is invalid.');
       //$validator->addError($key, $validator->_getErrorMessage($key, 'vTextAllowedFileEmpty', 'No file data found. Please try again!'));
       return false;
    }
    
    $_SESSION['promo_code'] = $value;
    $_SESSION['success'] = 'Success! Your promo code ('.$value.') has been applied.';
    return true;


    So errors show up and in the customValidator is triggering. When I enter the promo code "123456" it's successful but displays nothing but the form again, if I hit refresh and only after will it show the success message and removes the form with the text... I'm confused as to why a refresh is needed?

    This question has been answered by lagpro. See the first response.

    • discuss.answer
      • 52227
      • 2 Posts
      It seems after some trial and error I finally figured it out... I added the hooks redirect (&hooks=`redirect`) and the resource ID to redirect to (&redirectTo=`46`)

      [[!FormIt?
        &hooks=`redirect`
        &customValidators=`validateCoupon`
        &validate=`promo_code:required:minLength=^6^:validateCoupon`
        &promo_code.vTextRequired=`Please enter a Promo Code.`
        &promo_code.vTextMinLength=`Promo Code must have at least 6 characters in length.`
        &submitVar=`promo-code-submit`
        &redirectTo=`46`
      ]]