diff --git a/controllers/customers_controller.php b/controllers/customers_controller.php index 28b7cc8a..e136dfbd 100755 --- a/controllers/customers_controller.php +++ b/controllers/customers_controller.php @@ -43,7 +43,11 @@ class CustomersController extends AppController { function add() { if (!empty($this->data)) { - + + /* Remove all whitespace and non-digit characters from the user input */ + $this->data['Customer']['abn'] = preg_replace("/\s*\D*/", "", $this->data['Customer']['abn']); + + if($this->Customer->saveAll($this->data, array('validate'=>'first')) ) { $newcustomerid = $this->Customer->id; $this->Session->setFlash(__('The Customer has been saved', true)); @@ -68,16 +72,24 @@ class CustomersController extends AppController { } if (!empty($this->data)) { + /* Remove all whitespace and non-digit characters from the user input */ + $this->data['Customer']['abn'] = preg_replace("/\s*\D*/", "", $this->data['Customer']['abn']); if ($this->Customer->saveAll($this->data)) { $this->Session->setFlash(__('The Customer has been saved', true)); $id = $this->Customer->id; $this->redirect(array('action'=>'view/'.$id)); } else { $this->Session->setFlash(__('The Customer could not be saved. Please, try again.', true)); + + $this->set('customer_categories', $this->Customer->CustomerCategory->find('list')); + + $this->set('industries', $this->Customer->Industry->find('list', array('fields'=>array('Industry.id', 'Industry.name', 'ParentIndustry.name'),'recursive' => 0, + 'order'=>'ParentIndustry.name ASC, Industry.name ASC'))); } } if (empty($this->data)) { $this->data = $this->Customer->read(null, $id); + $this->set('customer_categories', $this->Customer->CustomerCategory->find('list')); $this->set('industries', $this->Customer->Industry->find('list', array('fields'=>array('Industry.id', 'Industry.name', 'ParentIndustry.name'),'recursive' => 0, 'order'=>'ParentIndustry.name ASC, Industry.name ASC'))); @@ -130,5 +142,9 @@ class CustomersController extends AppController { $this->data = $this->Customer->read(null, $id); } } + + + + } ?> diff --git a/models/customer.php b/models/customer.php index 5c0fe723..e65b5cb2 100755 --- a/models/customer.php +++ b/models/customer.php @@ -10,20 +10,26 @@ class Customer extends AppModel { 'message' => 'This Customer name is already taken or has been left blank', 'required' => true, 'allowEmpty' => false), - 'abn' => array( - 'numbers' => array( - 'rule' => 'numeric', - 'message' => 'ABN must only contain numbers - no letters or punctuation', - 'required' => true, - 'allowEmpty' => true), - 'minlength' => array( - 'rule' => array('minLength', '11'), - 'message' => 'ABN must be exactly 11 numbers long') + + + 'abn' => array( + 'rule' => 'checkABN', + 'message' => 'ABN must contain 11 digits' ) ); + /* Custom Validation for ABN */ + + function checkABN($data) { + $numbers = preg_replace("/\s*\D*/", "", $data['abn']); + $length = strlen($numbers); + return $length == 11; + //ABNs must be exactly 11 digits long. + } + + //The Associations below have been created with all possible keys, those that are not needed can be removed var $hasMany = array( 'Address' => array('className' => 'Address', diff --git a/views/addresses/view.ctp b/views/addresses/view.ctp index 31e500e2..ade47ae3 100755 --- a/views/addresses/view.ctp +++ b/views/addresses/view.ctp @@ -1,16 +1,7 @@