Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

ssn-field.inc.php

Go to the documentation of this file.
00001 <?php
00008 class SSNField extends CollectionField
00009 {
00016     public function __construct($name, Form &$parentForm, array $attribs = array())
00017     {
00018         $attribs['fields'] = array(
00019             array('TextField', "${name}_1", array('size' => 3, 'regExp' => '/^[0-9]{3}$/', 'maxlength' => 3)),
00020             array('LabelField', "${name}_dash1", array('text' => '-')),
00021             array('TextField', "${name}_2", array('size' => 2, 'regExp' => '/^[0-9]{2}$/', 'maxlength' => 2)),
00022             array('LabelField', "${name}_dash2", array('text' => '-')),
00023             array('TextField', "${name}_3", array('size' => 4, 'regExp' => '/^[0-9]{4}$/', 'maxlength' => 4))
00024         );
00025 
00026         parent::__construct($name, $parentForm, $attribs);
00027 
00028         $this->autoAdvance = true;
00029     }
00030 
00035     public function validate()
00036     {
00037         # call parent to validate individual text fields
00038         parent::validate();
00039 
00040         # If any of the sub fields have input automatically all sub fields _must_ have input
00041         if(!$this->allFieldsHaveData() && !$this->noFieldsHaveData())
00042         {
00043             $this->hasError = true;
00044             $this->errorMessage = "The $this->title field must be a valid SSN number.";
00045         }
00046     }
00047 
00048 
00053     public function getData()
00054     {
00055         $ret = $this->fields[0]->getData() . '-' . $this->fields[2]->getData() . '-' . $this->fields[4]->getData();
00056         
00057         # Check for empty values
00058         if($ret == '--')
00059             return null;
00060 
00061         return $ret;
00062     }
00063 
00064 
00069     public function setData($data)
00070     {
00071         # This nasty huge regex matches 123-45-6789, 123 45 6789, and 123456789
00072         if(preg_match('/(^|\s)(00[1-9]|0[1-9]0|0[1-9][1-9]|[1-6]\d{2}|7[0-6]\d|77[0-2])(-?|[\. ])([1-9]0|0[1-9]|[1-9][1-9])\3(\d{3}[1-9]|[1-9]\d{3}|\d[1-9]\d{2}|\d{2}[1-9]\d)($|\s|[;:,!\.\?])/', $data, $matches))
00073         {
00074             $this->fields[0]->setData($matches[2]);
00075             $this->fields[2]->setData($matches[4]);
00076             $this->fields[4]->setData($matches[5]);
00077         }
00078         elseif($data != '')
00079             throw new InvalidValueException();
00080     }
00081 
00085     public function getSqlImpl()
00086     {
00087         return "$this->name = '" . $this->getData() . "'";
00088     }
00089 }
00090 ?>

Generated on Fri Oct 27 12:26:40 2006 for BaseJumper by doxygen 1.3.9.1