Jump to content

Why don't two of my rules work!?? PEAR HTML_QuickForm HELP!


wessidemd

Recommended Posts

First, I'd like to thank in advance anyone willing to even look at this. 

 

Background info.  All my rules, including the two I'm having problems with, worked perfectly before I switched my code to DHTML Tableless.  Now TWO and only TWO rules are not working!!  I'm going to add the code below and highlight the two problem areas and the function I'm using to prove they don't work. 

 

Here we go!

<?php
//$debug = TRUE;
$debug = FALSE;

if($debug){
error_reporting(E_ALL);
} else {
error_reporting(E_ERROR);
}

//Main Configuration File//
require_once 'bin/config.php';

//Classes Requires
require_once 'HTML/QuickForm.php';
require_once 'HTML/QuickForm/DHTMLRulesTableless.php';
require_once 'HTML/QuickForm/Renderer/Tableless.php';

require_once CLASSES . 'Users/Controller/User_Controller.php';
require_once CLASSES . 'Users/Model/User_Model.php';
require_once CLASSES . 'Constants/Constants.php';
require_once CLASSES . 'Forms/Controller/Form_NewUser.php';

require_once CONN;

?>

<div id="new_user">
<?php
	/*
	 * @author Wesley Almeida
	 * @description New User input form with validation
	 *
	 * adding form elements format: $form->addElement('type','name','prompt',$options);
	 * validating form elements format: $form->addRule('form element','error message','rule type (page 462)',NULL,'client');
	 */

	//global vars
	global $conn;
	global $form;
	global $form_functions;
	global $checkBoxes;
	global $mailer;

	$dbc = new  Conn();
	$conn = $dbc->conn();

	//instantiate new form
	$form = new HTML_QuickForm_DHTMLRulesTableless('form_new_user','post');
	$form->removeAttribute('name');

	//instantiate form functions
	$form_functions = new Form_NewUser();

	//instantiate new renderer for XHTML 1.1 output
	$renderer = new HTML_QuickForm_Renderer_Tableless();

	//add form elements
	//header
	$form->addElement('header',NULL,'New User Information');

	//elements
	$form->addElement('text','first_name','First Name: ',array('maxlength'=>30)); //First Name
	$form->addElement('text','last_name','Last Name: ',array('maxlength'=>30)); //Last Name
	$form->addElement('text','company','Company Name: ',array('maxlength'=>50)); //Company Name
	$form->addElement('text','phone','Phone: ',array('maxlength'=>20)); //Phone
	$form->addElement('text','fax','Fax: ',array('maxlength'=>20)); //Fax
	$form->addElement('text','email','Email: ',array('maxlength'=>100)); //email
	$form->addElement('text','address','Address: ',array('maxlength'=>100)); //address
	$form->addElement('text','city','City: ',array('maxlength'=>32)); //city
	$form->addElement('text','zip','Zip Code: ',array('maxlength'=>5));//zip
	$form->addElement('select','state','State: ',$form_functions->makeStates()); //states
	$form->addElement('checkbox','mbe','MBE / DBE: '); //MBE DBE

	//Divisions Checkboxes
	$form->addElement('header',NULL,'Select All Divisions Which Apply to You',array('id'=>'new_user_divisions'));
	$checkBoxes = $form_functions->makeDivisions($form);

	//User Login Information
	$form->addElement('header',NULL,'New User Login Information');
	$form->addElement('text','username','User Name: ',array('maxlength'=>32));
	$form->addElement('password','pass1','Password: ',array('maxlength'=>32));
	$form->addElement('password','pass2','Retype Password: ',array('maxlength'=>32));

	//submit
	$form->addElement('submit','submit','Join Us!',array());

	//Filter bad chars
	$form->applyFilter('__ALL__','escape_data');

                          //custom validation rules
	[b]function username_exists($data){
		$_userController = new User_Controller();
		$result = $_userController->checkIfUserExists($data);
		if(!$result){
			echo "returned FALSE \n ";
			echo "Result = {$result['username']} \n ";
			echo "value = $data";
			return TRUE;
		}
		echo "returned FALSE \n ";
		echo "Result = {$result['error']} \n ";
		echo "value = $data";
		return FALSE;
	}

	//callback function to test if username exists
	function email_exists($data){
		$_userController = new User_Controller();
		$result = $_userController->checkIfEmailExists($data);
		if(!$result){
			echo "returned FALSE \n ";
			echo "Result = {$result['email']} \n ";
			echo "value = $data";
			return TRUE;
		}
		echo "returned FALSE \n ";
		echo "Result = {$result['error']} \n ";
		echo "value = $data";
		return FALSE;
	}
	function proves_it_doesnt_work($data){
		return FALSE;
	}[/b]
	//register special rules
	//registerRule ( string $ruleName , string $type , string $data1 , string $data2 = null )
	//http://pear.php.net/manual/en/package.html.html-quickform.html-quickform.registerrule.php
	$form->registerRule('phone','regex', '/^[\d\-\.\s]+$/');

	//homemade validators
	$pathToRule = CLASSES . 'Validators/Controller/';
	$form->registerRule('zip_code',NULL,'USZipCode',$pathToRule . 'USZipCode.php');
	$form->registerRule('check_username','callback','username_exists');
	$form->registerRule('check_email','callback','email_exists');

	[b]//Validation functions
	$form->addRule('username','The user name you selected already exists.','proves_it_doesnt_work',NULL,'server'); //troublemaker
	$form->addRule('email','The email you selected already exists.','proves_it_doesnt_work',NULL,'server'); //troublemaker[/b]
	$form->addRule('first_name','Please Enter your first name.','required',NULL,'client');
	$form->addRule('last_name','Please Enter your last name.','required',NULL,'client');
	$form->addRule('email','Please include your email address.','required',NULL,'client');
	$form->addRule('email','You must type in a valid email address.','email',NULL,'client');
	$form->addRule('username','You must select a user name.','required',NULL,'client');
	$form->addRule('zip','You must type in a valid zip code','zip_code',NULL,'client');
	$form->addRule('phone','You must type in a valid phone number','phone',NULL,'client');
	$form->addRule('pass1','You must type in a password.','required',NULL,'client');
	$form->addRule('pass2','You must retype in a password.','required',NULL,'client');
	$form->addRule(array('pass1','pass2'),'Your passwords do not match. Please try again','compare',NULL,'client');

	//add Javascript validation
	$form->getValidationScript();

	//set XHTML Renderer
	$form->accept($renderer);

	//Render the form//
	if($form->validate()){
		$data = $form->process('process_data');
		$result = $data['result'];
		$user_data = $data['user_data'];

		//notification script
		include_once 'inc/login_result.inc.php';
	} else {
		//$form->display();
		echo $renderer->toHtml();
	}


	//callback function used to trim and escape data for HTML_Quickform
	function escape_data($data){
		global $conn;
		if(get_magic_quotes_gpc()){
			$data = stripslashes($data);
			return mysqli_real_escape_string($conn,trim($data));
		}
		//return FALSE;
	}

	/**
	 * @desc callback function used to process form data.
	 * @param $data form values
	 * @return boolean, User_Model user_data
	 */
	function process_data($data){
		global $form;
		global $checkBoxes;
		global $form_functions;

		$mbe = 0;
		if($data['mbe']){
			$mbe = 1;
		}
		$user_model = new User_Model();
		$user_model->address = $data['address'];
		$user_model->city = $data['city'];
		$user_model->company = $data['company'];
		$user_model->email = $data['email'];
		$user_model->fax = $data['fax'];
		$user_model->first_name = $data['first_name'];
		$user_model->last_name = $data['last_name'];
		$user_model->mbe = $mbe;
		$user_model->password = $data['pass2'];
		$user_model->phone = $data['phone'];
		$user_model->state_id = $data['state'];
		$user_model->user_type_id = 1;
		$user_model->username = $data['username'];
		$user_model->zip = $data['zip'];

		//count checked checkboxes
		$checked_boxes = $form_functions->countCheckboxes($checkBoxes,$form);

		//sign user up
		$result = $form_functions->signUp($user_model,$checked_boxes);

		return array('result'=>$result,'user_data'=>$user_model);
	}
?>
</div>

<?php
//!$result['result']
if($debug){
	echo "<div id='debug'>";
	echo "<h1>Debug</h1>";
	echo "<h2>The following information has been gathered</h2>";
	$data = $form->exportValues();
	echo "<ul>";
	foreach ($data as $key=>$value){
		echo "<li><strong><em>$key:</em></strong> $value</li>";
	}
	echo "</ul>";

	echo "<h3>Registered Rules</h3>";
	foreach ($form->getRegisteredRules() as $key=>$value){
		echo "<li><strong><em>$key:</em></strong> $value</li>";
	}
	/*****************************************************************/
	/*****************************************************************/

	echo "</div><!-- end debug -->";





}
?>


 

Can you see the weirdness?

 

To see the code in action  go to http://beta.ebidservices.com/xhtml_register.php

 

 

THANKS!

 

PS As I'm typing this, the scrollbar is going crazy, so I apologize for any typing errors.  :facewall:

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.