Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

SmartyValidate issue

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
jimisanchez
Smarty n00b


Joined: 14 Jun 2010
Posts: 3

PostPosted: Thu Jun 17, 2010 7:08 pm    Post subject: SmartyValidate issue Reply with quote

I am using SmartyValidate with Smarty2.

Occasionally, on submit, I receive errors about all of the validators being unregistered, however, a refresh of the page, or hitting submit in the url bar and then re-submitting works.
Its very rare that, it happens, so I am having a hard time determining the exact cause. I have seen this behavior on page-back before, but not on a fresh visit form submission.

This form is submitting to itself if that matters.

Do you have any ideas as to how to go about diagnosing?

Here is my code

Code:

<?php

if (isset($_COOKIE['Page']))
{
   $page = (int) $_COOKIE['Page'];
   if ($page !=1 )
   {
   header('Location: home_'.$page.'.php?id='.base64_encode($_COOKIE['ID']));
   die();
   }
}
  ini_set('session.gc_maxlifetime',3600);
  ini_set('session.gc_probability',1);
  ini_set('session.gc_divisor',1);
 session_start();
define("USE_DEBUG",true);
define("BENCHMARK",true);
define("USE_MYSQLI",true);
define('TEMPLATE_DIR','views/');

if(preg_match('@^/var/www/(?:prod|dev|test)/@', __FILE__, $dir_struct)) {
   $dir_struct = $dir_struct[0];
}
include($dir_struct . 'lib/config.php');
require $dir_struct.'lib/classes/Smarty/libs/Smarty.class.php';
require $dir_struct.'lib/classes/Smarty/libs/SmartyValidate/libs/SmartyValidate.class.php';

  $smarty = new Smarty;
  $smarty->template_dir = TEMPLATE_DIR;



  unset($dir_struct);
  $source=$_GET['source'];
  if ($source == "" ) { $source = "organic"; }


include('tmp_config.php');

  $states = $mysqli->get("states")->return_array_col('fullState',TRUE,'state_abb');
 
  $smarty->assign('states',$states);
  /**
   * Temporarily assign the property address to the $_GET zip value until post'd
   */
     if ($_GET['zip'])
     {
      $mysqli->limit(1);
   $postal_info =   $mysqli->get_where('GreatData',array('zip'=>$_GET['zip']))->return_array();

        $state = $postal_info[0]['State'];
        $zip = $postal_info[0]['zip'];
        $county = $postal_info[0]['county'];
        $city = $postal_info[0]['city'];
     }
        foreach( array('prop','prior','mailing') as $type)
        {
     
          $smarty->assign($type.'_state',(empty($state) ? $_POST[$type.'state'] : $state));
        $smarty->assign($type.'_city',(empty($city) ? $_POST[$type.'_city'] : $city));
        $smarty->assign($type.'_zip',(empty($zip) ? $_POST[$type.'_zip'] : $zip));
        $smarty->assign($type.'_county',(empty($county) ? $_POST[$type.'_county'] : $county));
        }
        
  $smarty->assign('cutoff',date('Y',strtotime('-20 year')));
  $smarty->assign('construction_types',array('' => 'Please choose','F' =>'Wood/Frame','com.travelers_BRKMY' => 'Brick','BRKVEN' => 'Brick Veneer', 'L' => 'Log Home','MOBILE' => 'Mobile Home',));
  $smarty->assign('foundation_types',array('' => 'Please choose','basement' =>'Basement','slab' => 'Slab','Crawl space' => 'Crawl Space', 'piers' => 'Piers/Pilings'));
  $smarty->assign('garage_types',array('' => 'Please choose',
  'NoGarage' => 'No Garage',
                                'CarportAttached' => 'Carport Attached',
                                'CarportDetached' => 'Carport Detached',
                                'OneCarAttached' => 'One Car Attached',
                                'TwoCarAttached' => 'Two Car Attached',
                                'ThreeCarAttached' => 'Three Car Attached',
                                'OneCarDetached' => 'One Car Detached',
                                'TwoCarDetached' => 'Two Car Detached',
                                'ThreeCarDetached' => 'Three Car Detached',
                                'Other' => 'Other',
                                ));
  $smarty->assign('deck_options',array('Yes' => 'Yes','No' => 'No'));
 
     $smarty->assign('construction_selected',!empty($_POST['construction']) ? $_POST['construction'] : 'F');
     $smarty->assign('foundation_selected',!empty($_POST['foundation']) ? $_POST['foundation'] : 'basement');
     $smarty->assign('garage_selected',!empty($_POST['garage']) ? $_POST['garage'] : '');
     $smarty->assign('deck_selected',!empty($_POST['deck']) ? $_POST['deck'] : '');
     
     
     $smarty->assign('prior_address_options',array('Yes' => 'Yes','No' => 'No'));
     $smarty->assign('prior_address_selected',(empty($_POST['prioraddressquestion']) ? '' : $_POST['prioraddressquestion']));
     
     
     
         if ($_GET['lead_id'] || isset($_COOKIE['ID']))
       {
          $mysqli->select('prefill_values');
          $mysqli->where('page',1);
          if ($_GET['lead_id']) {
             $mysqli->where('lead_id',$_GET['lead_id']);
          }
          if (isset($_COOKIE['ID'])) {
             $mysqli->where('lead_id',$_COOKIE['ID']);
          }
          $json = $mysqli->get('home_save_retrieve')->return_array_row_col();
          $smarty->assign('json',$json);
       }
     
     

        if (!$_POST) {
        $smarty->assign('show_mailing_address',false);
        }
        if ($_POST) {
           if ($_POST['propertyAddress'] == 'No')
           {
              $smarty->assign('show_mailing_address',true);
           }
        }
        if (!$_POST) {
        $smarty->assign('show_prior_address',false);
        }
        if ($_POST) {
           if ($_POST['prioraddressquestion'] == 'No')
           {
              $smarty->assign('show_prior_address',true);
           }
        }
        
        $yearbuilt = (empty($_GET['year']) ? $_POST['yearBuiltYear'] : $_GET['year']);
        if (empty($yearbuilt)) { $yearbuilt = date('Y'); }
        
        $roof_age = (empty($_POST['roof_ageYear']) ? '0000-00-00' : $_POST['roof_ageYear'].'-01-01');
        $electrical_age = (empty($_POST['electrical_ageYear']) ? '0000-00-00' : $_POST['electrical_ageYear'].'-01-01');
        $plumbing_age = (empty($_POST['plumbing_ageYear']) ? '0000-00-00' : $_POST['plumbing_ageYear'].'-01-01');
        $heat_age = (empty($_POST['heat_ageYear']) ? '0000-00-00' : $_POST['heat_ageYear'].'-01-01');
        
        $smarty->assign('yearbuilt',$yearbuilt.'-01-01');
        $smarty->assign('roof_age',$roof_age);
        $smarty->assign('electrical_age',$electrical_age);
        $smarty->assign('plumbing_age',$plumbing_age);
        $smarty->assign('heat_age',$heat_age);
        $smarty->assign('show_improvements',( ((date('Y') - $yearbuilt) >= 20))  ? true : false);
   
        $smarty->assign('show_roof_improvements',( ((date('Y') - $yearbuilt) >= 20))  ? true : false);
        
        if (date('Y') - $yearbuilt >= 20)
        {
           $smarty->assign('roof_years',20);
        }
        else {
           $smarty->assign('roof_years','20');
        }
       

 
if( empty($_POST) ) {
        // new form, we (re)set the session data
        SmartyValidate::connect($smarty, true);
        // register our validators
          SmartyValidate::register_validator('prioraddressquestion', 'prioraddressquestion', 'notEmpty',
            false, false, 'trim');
                 
        SmartyValidate::register_validator('prop_address', 'prop_address', 'notEmpty',
            false, false, 'trim');
        SmartyValidate::register_validator('prop_city', 'prop_city', 'notEmpty',
            false, false, 'trim');           
        SmartyValidate::register_validator('prop_state', 'prop_state', 'notEmpty',
            false, false, 'trim');           
        SmartyValidate::register_validator('prop_county', 'prop_county', 'notEmpty',
            false, false, 'trim');           
        SmartyValidate::register_validator('prop_zip', 'prop_zip', 'isZipcode',
            false, false, 'trim');           
           
        SmartyValidate::register_validator('sqft', 'sqft', 'isInt',
            false, false, 'trim');           
        SmartyValidate::register_validator('construction', 'construction', 'notEmpty',
            false, false, 'trim');           
        SmartyValidate::register_validator('foundation', 'foundation', 'notEmpty',
            false, false, 'trim');
        SmartyValidate::register_validator('garage', 'garage', 'notEmpty',
            false, false, 'trim');           
        SmartyValidate::register_validator('deck', 'deck', 'notEmpty',
            false, false, 'trim');             
        SmartyValidate::register_validator('yearBuiltYear', 'yearBuiltYear', 'notEmpty',
            false, false, 'trim');
        SmartyValidate::register_validator('propertyaddress_question', 'propertyAddress', 'notEmpty',
            false, false, 'trim');
           
                      SmartyValidate::register_validator('address', 'mailing_address', 'isAddress',
            false, false, 'trim');
        SmartyValidate::register_validator('city', 'mailing_city', 'isCity',
            false, false, 'trim');           
        SmartyValidate::register_validator('state', 'mailing_state', 'isState',
            false, false, 'trim');           
        SmartyValidate::register_validator('county', 'mailing_county', 'isCounty',
            false, false, 'trim');           
        SmartyValidate::register_validator('zip', 'mailing_zip', 'isZipcode',
            false, false, 'trim');           

            SmartyValidate::register_validator('prior_address', 'prior_address', 'isAddress',
            false, false, 'trim');
        SmartyValidate::register_validator('prior_city', 'prior_city', 'isCity',
            false, false, 'trim');           
        SmartyValidate::register_validator('prior_state', 'prior_state', 'isState',
            false, false, 'trim');           
        SmartyValidate::register_validator('prior_county', 'prior_county', 'isCounty',
            false, false, 'trim');           
        SmartyValidate::register_validator('prior_zip', 'prior_zip', 'isZipcode',
            false, false, 'trim');

                       

    }

 //begin form validation
if ($_POST ) {
   

   SmartyValidate::connect($smarty);
       if(SmartyValidate::is_valid($_POST)) {
           // no errors, done with SmartyValidate
            SmartyValidate::disconnect();

           
              if (strtolower($_POST['construction']) == 'mobile')
    {
       //header("Location: http://" . $_SERVER['HTTP_HOST'] . "/insurance_redirects.php?ZipCode=$prop_zip");
       //die();
    }
    $yearbuilt = $_POST['yearBuiltYear'];
    $prop_zip = $_POST['propzip'];
    $_POST['yearBuilt'] = $_POST['yearBuiltYear'];
    //include('../../private/lead_filters.php');
    $result = $mysqli->query("SELECT zipcode from Travelers_ExZips where zipcode = '$prop_zip' ") or die("Error: " .$mysqli->error );
    $rff = $result->num_rows;
    $lead = new Lead($mysqli);
    if (!isset($_COOKIE['ID']))
    {
       $lead->init();
       $lead->commit();
       setcookie("ID", $lead->info->id, time()+10800);
    } else {
       $lead->load($_COOKIE['ID']);
    }

    setcookie('Page',2, time()+10800);
    
    $lead_init = array('lob' => 'Home','status' => 'Abandoned','source' => $source,'yearbuilt' => $_POST['yearBuilt'],'sqft' => $_POST['sqft'],'construction' => $_POST['construction'],'foundation' => $_POST['foundation'],'garage' => $_POST['garage'],'deck' => $_POST['deck'],'addedtosystem' => date('Y-m-d H:i:s'),'page' => '1');
    
    $lead_init['propaddress'] = $_POST['prop_address'];
    $lead_init['propcity'] = $_POST['prop_city'];
    $lead_init['propstate'] = $_POST['prop_state'];
    $lead_init['propcounty'] = $_POST['prop_county'];
    $lead_init['propzipcode'] = $_POST['prop_zip'];

    if ($_POST['propertyAddress'] == 'No') {
    $lead_init['address'] = $_POST['mailing_address'];
    $lead_init['city'] = $_POST['mailing_city'];
    $lead_init['state'] = $_POST['mailing_state'];
    $lead_init['county'] = $_POST['mailing_county'];
    $lead_init['zip'] = $_POST['mailing_zip'];
    }
    else {
           $lead_init['address'] = $_POST['prop_address'];
    $lead_init['city'] = $_POST['prop_city'];
    $lead_init['state'] = $_POST['prop_state'];
    $lead_init['county'] = $_POST['prop_county'];
    $lead_init['zip'] = $_POST['prop_zip'];
    }
    
    foreach ($lead_init as $k => $v)
    {
       $lead->info->$k = $v;
    }
    
    $mysqli->delete('lead_addresses', array('lead_id' => $lead->info->id));

    $lead->add_address('property', array('address' => $_POST['prop_address'], 'city' => $_POST['prop_city'], 'state' => $_POST['prop_state'], 'zip' => $_POST['prop_zip'], 'county' => $_POST['prop_county']));
    
    if ($_POST['propertyAddress'] == 'No') {
    $lead->add_address('mailing', array('address' => $_POST['mailing_address'], 'city' => $_POST['mailing_city'], 'state' => $_POST['mailing_state'], 'zip' => $_POST['mailing_zip'], 'county' => $_POST['mailing_county']));
    }
    
    if ($_POST['prioraddressquestion'] == 'No') {

    $lead->add_address('prior', array('address' => $_POST['prior_address'], 'city' => $_POST['prior_city'], 'state' => $_POST['prior_state'], 'zip' => $_POST['prior_zip'], 'county' => $_POST['prior_county']));
    }

    $lead->commit();

    $updates = array('roof','plumbing','electrical','heat');

    foreach ($updates as $update)
    {

       if (!empty($_POST[$update.'_ageYear']))
       {
          $mysqli->insert('lead_home_updates', array('lead_id' => $lead->info->id, 'element_name' => $update, 'year_updated' => $_POST[$update.'_ageYear']),TRUE,TRUE);
       }
    }
    $id = $lead->info->id;

    $lead->load($id);
    $json = json_encode($_POST);
    $mysqli->insert('home_save_retrieve',array('lead_id' => $id, 'page' => 1, 'prefill_values' => $json),TRUE,TRUE);
           
           
           
          header('Location: home_2.php?id='.base64_encode($lead->info->id),301);
                die();
                exit();
     
       }
       else {
       $smarty->assign($_POST);
       }
 }
       $smarty->display('home_1.tpl');
 

?>
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Jun 17, 2010 9:33 pm    Post subject: Reply with quote

Be sure to use connect($smarty,TRUE) on new forms to reset session info, and connect($smarty) everywhere else to access the current session. Be careful about going BACK to a form after a disconnect(), you must reset the session. It may help to not use disconnect() at all.
Back to top
View user's profile Send private message Visit poster's website
jimisanchez
Smarty n00b


Joined: 14 Jun 2010
Posts: 3

PostPosted: Fri Jun 18, 2010 2:17 pm    Post subject: Reply with quote

Mohrt,
Other than removing the disconnects (which didnt help), my code above seems to adhere to your suggestions.

Can you think of any other reason that a fresh form submission would result in this error?
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Jun 18, 2010 2:25 pm    Post subject: Reply with quote

The error could happen if there is $_POST data, and the session vars are not present that hold the registere validators. Are you always starting the session? Are you redrawing the same form, even on a successful submit? That may cause some funkiness. Also, I find it useful to redirect the browser to a success page after a successful form submit, so the back button cannot create a resubmit of the form easily.

You might also test is_registered_form() along with $_POST not empty.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP