PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
// snip
if ($clean = $this->processInput($input))
{
require_once 'models/Vendors.php';
require_once 'Zend/Db/Expr.php';
$vendor = new Bag_Vendors();
$clean['created'] = new Zend_Db_Expr('NOW()');
try {
$vendor->insert($clean);
$this->_redirect('vendors/view');
}
catch (Zend_Db_Statement_Exception $e) {
if (strstr($e->getMessage(), '1062')) //@todo: find a better way to do this
{
$this->view->formSetError('code', 'This code already exists');
}
}
}
//snip
See the todo in there? Is there a better way to find out if I am getting mysql error code 1062 with this? I know I could do mysql_errno(), but it seems like the Exception would provide this code or at least its own code for that. Do I have to subclass it? Should I just shut up and use mysql_errno()?
I don't think you should ever rely on exceptions for doing data validation, which looks like what you might be doing.
Secondly this is not very portable. What if you have to switch the backend to SQL Server? The error numbers are going to be different.
It might be better to do a query before the insert to see if the vendor information is valid, or have the insert method catch the exception and return false.
$filter = new MC2_Input_Filter_Set();
$filter->addFilter(new MC2_Input_Filter_StringTrim());
$filter->addFilter(new MC2_Input_Filter_Digits(), array('zip', 'phone', 'fax'));
$validate = new MC2_Input_Validate_Set();
$code = new MC2_Input_Validate_Wrapper_Zend(new Zend_Validate_Regex('/^[a-zA-Z0-9_-]{2,6}$/'));
$validate->addValidator($code, 'code', 'Manufacturer code may only contain numbers, letters, dashes, and underscores. It must also be between 2 and 6 characters.');
$validate->addValidator(new MC2_Input_Validate_Email(), array('email_1', 'email_2'), 'You must enter a valid email address');
$validate->addValidator(new MC2_Input_Validate_USState(), 'state');
$validate->addValidator(new MC2_Input_Validate_USZip(), 'zip');
$validate->addValidator(new MC2_Input_Validate_USPhone(), array('phone', 'fax'));
$validate->addValidator(new Bag_NotDuplicateCode($this->db), 'code');
$input->addFilterSet($filter);
$input->addValidatorSet($validate);
if ($clean = $this->processInput($input))
{
require_once 'models/Vendors.php';
require_once 'Zend/Db/Expr.php';
$vendor = new Bag_Vendors();
$clean['created'] = new Zend_Db_Expr('NOW()');
try {
$vendor->insert($clean);
$this->_redirect('vendors/view');
}
catch (Zend_Db_Statement_Exception $e) {
if (strstr($e->getMessage(), '1062')) //@todo: find a better way to do this
{
$this->view->formSetError('code', 'This code already exists');
}
}
}