00001 <?php
00008 class IpAddressField 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, 'maxlength' => 3)),
00020 array('LabelField', "${name}_dot1", array('text' => ' . ')),
00021 array('TextField', "${name}_2", array('size' => 3, 'maxlength' => 3)),
00022 array('LabelField', "${name}_dot2", array('text' => ' . ')),
00023 array('TextField', "${name}_3", array('size' => 3, 'maxlength' => 3)),
00024 array('LabelField', "${name}_dot3", array('text' => ' . ')),
00025 array('TextField', "${name}_4", array('size' => 3, 'maxlength' => 3)),
00026 );
00027
00028 parent::__construct($name, $parentForm, $attribs);
00029 $this->autoAdvance = true;
00030 }
00031
00036 public function validate()
00037 {
00038 # call parent to validate individual text fields
00039 parent::validate();
00040
00041 # If any of the sub fields have input automatically all sub fields _must_ have input
00042 if(!$this->allFieldsHaveData() && !$this->noFieldsHaveData())
00043 {
00044 $this->hasError = true;
00045 $this->errorMessage = "The $this->title field must be a valid IP address.";
00046 }
00047 }
00048
00053 public function getData()
00054 {
00055 $ret = $this->fields[0]->getData() . '.' . $this->fields[2]->getData() . '.' .
00056 $this->fields[4]->getData() . '.' . $this->fields[6]->getData();
00057
00058 # check for an empty value
00059 if($ret == '...')
00060 return null;
00061
00062 return $ret;
00063 }
00064
00065
00070 public function setData($data)
00071 {
00072 if(preg_match('/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/', $data, $matches))
00073 {
00074 if(($matches[1] <= 255 && $matches[1] >= 0) &&
00075 ($matches[2] <= 255 && $matches[2] >= 0) &&
00076 ($matches[3] <= 255 && $matches[3] >= 0) &&
00077 ($matches[4] <= 255 && $matches[4] >= 0)
00078 )
00079 {
00080 $this->fields[0]->setData($matches[1]);
00081 $this->fields[2]->setData($matches[2]);
00082 $this->fields[4]->setData($matches[3]);
00083 $this->fields[6]->setData($matches[4]);
00084
00085 return;
00086 }
00087 }
00088
00089 if($data != '')
00090 throw new InvalidValueException();
00091 }
00092
00096 public function getSqlImpl()
00097 {
00098 return "$this->name = '" . $this->getData() . "'";
00099 }
00100 }
00101 ?>