error msg (set_errors()) private $error_msg='Please correct errors below:'; function __Construct($name, $enctype, $method, $action, &$fields, $custom_error_msg='') { $this->name = $name; $this->enctype = $enctype; $this->method = $method; $this->action = $action; $this->fields = $fields; // the array of fields passed in from the client is used to define and initialize them, this property then adds a value field and validates and writes form based on that if($custom_error_msg != '') { $this->error_msg = $custom_error_msg; } } public function get_sql_fields($query_type='insert') { // returns array of fieldname=>fieldvalue to be used in sql insert or update $fields = array(); foreach($this->fields as $field_name => $field_info) { switch($field_info['type']['tag']) { case 'input_submit': break; case 'input_file': if($query_type == 'insert' || $field_info['value'] != '') { $fields[$field_name] = $field_info['value']; } // don't update w/ blank for file upload because will incorrectly overwrite existing data break; default: $fields[$field_name] = $field_info['value']; } } return($fields); } public function get_field_value($fieldName,$fieldIndex='') { return ($fieldIndex == '') ? $this->fields[$fieldName]['value'] : $this->fields[$fieldName][$fieldIndex]['value']; // index is for array fields such as color_swatch[$item_color_id] } public function is_error() { return($this->error); } /* need descriptions on these 3 error fns. if you just set_field_error you don't have to set error or error_msg so those must be to display an error at the top of the form */ public function set_error($error) { $this->error = $error; } public function set_error_msg($error_msg) { $this->error_msg = $error_msg; } public function set_field_error($fieldName, $error_msg) { //echo '
'.$fieldName.': '.$error_msg; $this->fields[$fieldName]['error'] = $error_msg; } public function set_errors() { // loops thru all form fields foreach($this->fields as $fieldName => $fieldInfo) { if(isset($fieldInfo['error'])) { $this->errors[$fieldName] = $fieldInfo['error']; } } } private function validate_field($fieldName, $validate_arr) { //echo '

'.$fieldName.' '.$this->fields[$fieldName]['value'].' validate: ';print_r($validate_arr); if(in_array('required', $validate_arr) && trim($this->fields[$fieldName]['value']) == '') { $this->set_field_error($fieldName, 'required'); return(FALSE); } if(in_array('numeric', $validate_arr) && trim($this->fields[$fieldName]['value']) != '' && !is_numeric(trim($this->fields[$fieldName]['value']))) { $this->set_field_error($fieldName, 'must be numeric'); return(FALSE); } if(in_array('numeric_positive', $validate_arr) && trim($this->fields[$fieldName]['value']) != '' && (!is_numeric(trim($this->fields[$fieldName]['value'])) || trim($this->fields[$fieldName]['value']) <= 0) ) { $this->set_field_error($fieldName, 'must be a positive number'); return(FALSE); } if(in_array('email', $validate_arr) && trim($this->fields[$fieldName]['value']) != '' && !Utilities::is_valid_email(trim($this->fields[$fieldName]['value']))) { $this->set_field_error($fieldName, 'invalid'); return(FALSE); } /* gotta get the reg ex right if(in_array('integer', $validate_arr) && trim($this->fields[$fieldName]['value']) != '' && !preg_match("/[0-9]/", $this->fields[$fieldName]['value']))) { $this->set_field_error($fieldName, 'must be integer'); return(FALSE); } */ if(in_array('mysql_date', $validate_arr) && trim($this->fields[$fieldName]['value']) != '' && !Utilities::is_mysql_date($this->fields[$fieldName]['value'])) { $this->set_field_error($fieldName, 'invalid');// date format, must by YYYY-MM-DD ie '.date("Y-m-d"); return(FALSE); } if(in_array('cc', $validate_arr) && trim($this->fields[$fieldName]['value']) != '' && !Utilities::is_cc(Utilities::remove_non_int($this->fields[$fieldName]['value']))) { $this->set_field_error($fieldName, 'invalid'); return(FALSE); } if(in_array('cvv2', $validate_arr) && trim($this->fields[$fieldName]['value']) != '' && !Utilities::is_cvv2($this->fields[$fieldName]['value'])) { $this->set_field_error($fieldName, 'invalid'); return(FALSE); } // any regex if(array_key_exists('regex', $validate_arr) && trim($this->fields[$fieldName]['value']) != '' && preg_match($validate_arr['regex'], $this->fields[$fieldName]['value']) == 0) { $this->set_field_error($fieldName, 'invalid'); return(FALSE); } return(TRUE); } function _validate() { foreach($this->fields as $fieldName => $fieldInfo) { // note currently validating by 1 criteria at a time from $fieldInfo['validation'] array - false is returned immediately for a failure (as opposed to collecting errors in an array) // note if some external fn (ie page specific validation) has already set 'error' for this field then don't validate for other criteria - let that error take precedence if(!isset($this->fields[$fieldName]['error']) && isset($fieldInfo['validation']) && is_array($fieldInfo['validation']) && count($fieldInfo['validation']) > 0) { if(!$this->validate_field($fieldName, $fieldInfo['validation'])) { // $this->fields[$fieldName]['error'] gets set in fn $this->error = TRUE; } } } // note if $this->error was set true by some external fn (ie page specific validation) then this returns false - form doesn't validate return ($this->error) ? FALSE : TRUE; } function set_field_values($submitStatus='posted') { foreach($this->fields as $fieldName => $fieldInfo) { $this->field_value($fieldName, $fieldInfo, $submitStatus); } } function clear_field_values($keep_default=TRUE) { foreach($this->fields as $fieldName => $fieldInfo) { if($keep_default) { $this->fields[$fieldName]['value'] = (isset($fieldInfo['default'])) ? $fieldInfo['default'] : ''; } else { $this->fields[$fieldName]['value'] = ''; } } } public function set_field_value($fieldName, $value) { $this->fields[$fieldName]['value'] = $value; } private function field_value($fieldName, $fieldInfo, $submitStatus) { // a lot of duplication with posted and got if(strstr($fieldName, '[')) // for array fields such as color[item_color_id] { $fieldNameBefore = substr($fieldName, 0, strpos($fieldName, '[')); $fieldNameAfter = substr($fieldName, strpos($fieldName, '[')+1, strpos($fieldName, ']')-strpos($fieldName, '[')-1); //echo '
'.$fieldName.': '.$fieldNameBefore.'['.$fieldNameAfter.'] :: value: '.$_POST[$fieldNameBefore][$fieldNameAfter]; } switch($submitStatus) { case 'posted': if(isset($fieldNameBefore) && isset($_POST[$fieldNameBefore][$fieldNameAfter])) { $this->fields[$fieldName]['value'] = $_POST[$fieldNameBefore][$fieldNameAfter]; } elseif(isset($_POST[$fieldName])) { $this->fields[$fieldName]['value'] = $_POST[$fieldName]; } elseif(isset($this->fields[$fieldName]['type']['disabled']) && $this->fields[$fieldName]['type']['disabled']) { $this->fields[$fieldName]['value'] = (isset($this->fields[$fieldName]['default'])) ? $this->fields[$fieldName]['default'] : ''; } elseif($this->fields[$fieldName]['type']['tag'] == 'input_submit') { // if there's more than 1 submit button this prevents the ones that weren't pressed from coming back blank (ie a cancel button) $this->fields[$fieldName]['value'] = $this->fields[$fieldName]['default']; } elseif($this->fields[$fieldName]['type']['tag'] == 'input_file') { //print_r($_FILES); if(isset($fieldNameBefore) && $_FILES[$fieldNameBefore]['name'][$fieldNameAfter] != '') { // files array works oddly - need 'name' //echo $_FILES[$fieldNameBefore]['name'][$fieldNameAfter]; $this->fields[$fieldName]['value'] = $_FILES[$fieldNameBefore]['name'][$fieldNameAfter]; } elseif(isset($_FILES[$fieldName])) { $this->fields[$fieldName]['value'] = $_FILES[$fieldName]['name']; } else { $this->fields[$fieldName]['value'] = ''; } } else { $this->fields[$fieldName]['value'] = ''; } break; case 'got': if(isset($_GET[$fieldName])) { $this->fields[$fieldName]['value'] = $_GET[$fieldName]; } elseif($this->fields[$fieldName]['type']['disabled']) { $this->fields[$fieldName]['value'] = $this->fields[$fieldName]['default']; } elseif($this->fields[$fieldName]['type']['tag'] == 'input_submit') { // if there's more than 1 submit button this prevents the ones that weren't pressed from coming black blank (ie a cancel button) $this->fields[$fieldName]['value'] = $this->fields[$fieldName]['default']; } else { $this->fields[$fieldName]['value'] = ''; } break; default: $body = $this->submitStatus."\n"; foreach($this->fields as $name => $info) { $body .= "\n$name"; // enough to debug hopefully } Utilities::notify(Config::$tech_email, 'invalid $submitStatus passed to Form::__Construct', $body); } } function _html($Forms, $table_css='', $table_spacing=0, $top_text='', $row1_css='', $row2_css='', $vert_row_spacing=0, $table_width='', $error_css='error') { $hiddenFields = ''; // stores a string of hidden fields that are added just before end of form $form = 'enctype != '') { $form .= ' enctype="'.$this->enctype.'"'; } $form .= ' name="'.$this->name.'" method="'.$this->method.'" action="'.$this->action.'"> '; } if($this->error) { $form .= ' '.$this->error_msg.' '; } $i = 0; foreach($this->fields as $fieldName=>$fieldInfo) { if(isset($fieldInfo['skip']) && $fieldInfo['skip'] === TRUE) { } else { ++$i; if(!isset($fieldInfo['value'])) { // values are not set for new (not submitted) form fields - set to either default or blank for display purposes (note, not actually setting the value of the property - just the temp var in this loop) if(isset($fieldInfo['default'])) { $fieldInfo['value'] = $fieldInfo['default']; } else { $fieldInfo['value'] = ''; } } $row_css = ($i%2) ? $row1_css : $row2_css; // last field's row should not have background color - submit button if($fieldInfo['type']['tag'] == 'input_submit') { $row_css = ''; } $rowStart = ''; // init if(isset($fieldInfo['start_row']) && !$fieldInfo['start_row']) { $rowStart .= '  '; } else { if(isset($fieldInfo['blank_space_vertical']) && is_numeric($fieldInfo['blank_space_vertical'])) { $rowStart .= ''; } if($vert_row_spacing > 0) { $rowStart .= ''; } $rowStart .= ' '.strtoupper($fieldInfo['error']).' >   '; } if(isset($fieldInfo['label']) && $fieldInfo['label'] != '') { $rowStart .= (isset($fieldInfo['validation']) && in_array('required', $fieldInfo['validation'])) ? ''.$fieldInfo['label'].'' : $fieldInfo['label']; $rowStart .= ':  '; } else { $rowStart .= ' '; } $rowStart .= ' 0) { $rowEnd .= ''; } } switch($fieldInfo['type']['tag']) { case 'input_text': $formField = ''; break; case 'input_hidden': $hiddenFields .= ''; break; case 'textarea': $formField = '