Jump to content

zipcode masking - optional digits?


ebonit

Recommended Posts

Hi,

 

Standard zipcode in the US is 5 digits. However, sometimes people want to add extra digits to the zipcode. It seems that in remote areas it is custom to add digits so the mailman knows where to go.

 

I customer of ours has a zipcode in the form of NNNNN-NNNN

 

is it possible to put optional digits in the zip_code_format field? if not, I can always just empty the field so one can put in what he/she wants... but I like the masking.

 

thanx, Eric

Link to comment
Share on other sites

  • 5 weeks later...

I've made an override of the AddressController to solve this problem.

In the ps_country table you can add an asterix (*) at the end of the mask if you want to extend the mask. so in the example in post above, the mask will be NNNNN* that way you always need to have 5 numbers but you can extend it with whatever you want

 

public function postProcess()
{
	if (isset($_POST['submitAdd'.$this->table]))
	{
		// Transform e-mail in id_customer for parent processing
		if ($this->addressType == 'customer')
		{
			if (Validate::isEmail(Tools::getValue('email')))
			{
				$customer = new Customer;
				$customer = $customer->getByemail(Tools::getValue('email'));
				if (Validate::isLoadedObject($customer))
					$_POST['id_customer'] = $customer->id;
				else
					$this->_errors[] = Tools::displayError('This e-mail address is not registered.');
			}
			elseif ($id_customer = Tools::getValue('id_customer'))
			{
				$customer = new Customer((int)($id_customer));
				if (Validate::isLoadedObject($customer))
					$_POST['id_customer'] = $customer->id;
				else
					$this->_errors[] = Tools::displayError('Unknown customer');
			}
			else
				$this->_errors[] = Tools::displayError('Unknown customer');
			if (Country::isNeedDniByCountryId(Tools::getValue('id_country')) AND !Tools::getValue('dni'))
				$this->_errors[] = Tools::displayError('Identification number is incorrect or has already been used.');
		}

		// Check manufacturer selected
		if ($this->addressType == 'manufacturer')
		{
			$manufacturer = new Manufacturer((int)(Tools::getValue('id_manufacturer')));
			if (!Validate::isLoadedObject($manufacturer))
				$this->_errors[] = Tools::displayError('Manufacturer selected is not valid.');
		}

		/* If the selected country does not contain states */
		$id_state = (int)(Tools::getValue('id_state'));
		if ($id_country = Tools::getValue('id_country') AND $country = new Country((int)($id_country)) AND !(int)($country->contains_states) AND $id_state)
			$this->_errors[] = Tools::displayError('You have selected a state for a country that does not contain states.');

		/* If the selected country contains states, then a state have to be selected */
		if ((int)($country->contains_states) AND !$id_state)
			$this->_errors[] = Tools::displayError('An address located in a country containing states must have a state selected.');

		/* Check zip code */
		if ($country->need_zip_code)
		{
/*here starts the change */
			$zip_code_format = $country->zip_code_format;
			if(strpos($zip_code_format,'*')){
				$zcfLength = strpos($zip_code_format,'*');
				$zip_code_format = substr($zip_code_format, 0, $zcfLength);
				$postcode  = substr(Tools::getValue('postcode'), 0, $zcfLength);
			}else{
				$postcode = Tools::getValue('postcode');
			}
			if ($postcode AND $zip_code_format)
/*end changed code*/
//if (($postcode = Tools::getValue('postcode')) AND $zip_code_format)
			{
				$zip_regexp = '/^'.$zip_code_format.'$/ui';
				$zip_regexp = str_replace(' ', '( |)', $zip_regexp);
				$zip_regexp = str_replace('-', '(-|)', $zip_regexp);
				$zip_regexp = str_replace('N', '[0-9]', $zip_regexp);
				$zip_regexp = str_replace('L', '[a-zA-Z]', $zip_regexp);
				$zip_regexp = str_replace('C', $country->iso_code, $zip_regexp);
				if (!preg_match($zip_regexp, $postcode))
					$this->_errors[] = Tools::displayError('Your zip/postal code is incorrect.').'<br />'.Tools::displayError('Must be typed as follows:').' '.str_replace('C', $country->iso_code, str_replace('N', '0', str_replace('L', 'A', $zip_code_format)));
			}
			elseif ($zip_code_format)
				$this->_errors[] = Tools::displayError('Postcode required.');
			elseif ($postcode AND !preg_match('/^[0-9a-zA-Z -]{4,9}$/ui', $postcode))
				$this->_errors[] = Tools::displayError('Your zip/postal code is incorrect.');
		}


		/* If this address come from order's edition and is the same as the other one (invoice or delivery one)
		** we delete its id_address to force the creation of a new one */
		if ((int)(Tools::getValue('id_order')))
		{
			$this->_redirect = false;
			if (isset($_POST['address_type']))
				$_POST['id_address'] = '';
		}
	}
	if (!sizeof($this->_errors))
		parent::postProcess();

	/* Reassignation of the order's new (invoice or delivery) address */
	$address_type = ((int)(Tools::getValue('address_type')) == 2 ? 'invoice' : ((int)(Tools::getValue('address_type')) == 1 ? 'delivery' : ''));
	if (isset($_POST['submitAdd'.$this->table]) AND ($id_order = (int)(Tools::getValue('id_order'))) AND !sizeof($this->_errors) AND !empty($address_type))
	{
		if (!Db::getInstance()->Execute('UPDATE '._DB_PREFIX_.'orders SET `id_address_'.$address_type.'` = '.Db::getInstance()->Insert_ID().' WHERE `id_order` = '.$id_order))
			$this->_errors[] = Tools::displayError('An error occurred while linking this address to its order.');
		else
			Tools::redirectAdmin(Tools::getValue('back').'&conf=4');
	}
}

 

you can do the same in te /admin/tab/AdminAdresses.php. However note that you actually should make a module that overrides that class because you cannot override them in the override directory.

Link to comment
Share on other sites

This is good info. I posted a similar question a while back at this link and have marked that as [work around] in the post title. Until this can be handled normally by the back office setup, this is a good solution. I also posted a request on the forge.

 

Minor info update on the above post - the modified file shown is the one from the Admin/tabs folder. This has the public function postProcess() section.

 

The update for the controller override is in the public function preProcess() section

Link to comment
Share on other sites

Minor info update on the above post - the modified file shown is the one from the Admin/tabs folder. This has the public function postProcess() section.

 

The update for the controller override is in the public function preProcess() section

 

ah, ofcourse. thanks for the correction!

Link to comment
Share on other sites

  • 1 year later...

Since updating to PS 1.5 this override no longer works. It appears that the answer might be in an override for classes/Validate.php. I've pasted where I think that code needs to be changed. Can anyone help here so multiple Zip code formats (NNNNN or NNNNN-NNNN) are acceptable for validation?

/**
 * Check for postal code validity
 *
 * @param string $postcode Postal code to validate
 * @return boolean Validity is ok or not
 */
public static function isPostCode($postcode)
{
 return empty($postcode) || preg_match('/^[a-zA-Z 0-9-]+$/', $postcode);
}
/**
 * Check for zip code format validity
 *
 * @param string $zip_code zip code format to validate
 * @return boolean Validity is ok or not
 */
public static function isZipCodeFormat($zip_code)
{
 if (!empty($zip_code))
  return preg_match('/^[NLCnlc 0-9-]+$/', $zip_code);
 return true;
}

Link to comment
Share on other sites

  • 8 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...