Beispiel #1
0
/** ==================== Form submission handler follows ====================
 *
 * A success handler for the form.
 *
 * Make sure the form's ->onSuccess() method is uncommented and pointing
 * here for it to get called
 **/
function mySuccessHandler(fxForm &$form)
{
    return "<h3>Thank you {$form->getValueOf('Name')}, your message has been sent.</h3>";
}
Beispiel #2
0
 /**
  * Validates the submitted value.
  * Override this in derived classes if needed.
  **/
 public function _validate(&$errors, fxForm &$f)
 {
     $submitted = $this->_value;
     $required = $this->_inData('required');
     $cb = $this->_validation_cb;
     if ($this->disabled) {
         return $this;
     }
     if (!$required && '' == $submitted) {
         // Not required and no input => always ok.
         return $this;
     }
     if ($required && '' == $submitted) {
         // A required value but no input => always a fail. If a custom fail message was supplied, use that.
         return $this->_addError($this->_meta['required_message'] ? $this->_meta['required_message'] : '* Requires your input', $errors);
     }
     try {
         $validation_errors = $this->_fvalidator->validate(TRUE, TRUE);
         // We are only getting errors for this element, so we can safely remove the names.
     } catch (fProgrammerException $e) {
         // If we get here then there were no fValidation rules on this element => ok so far; now to check our callback routine...
     }
     if (!empty($validation_errors)) {
         // fValidation found some error(s)...
         $this->_meta['errors'] = array_values($validation_errors);
         $errors[$this->name] = array_values($validation_errors);
         return $this;
     }
     $min = $this->min;
     // Could be null (if not present)
     $max = $this->max;
     // 	"
     // Check for programming mistakes in setting of min & max values...
     if ($this->_inData('min') && $this->_inData('max')) {
         if ($min > $max) {
             throw new fProgrammerException("Element {$this->name} has min[{$min}] > max[{$max}]");
         }
     }
     // Handle min value checking (if applicable)
     if ($this->_inData('min')) {
         if ($submitted < $min) {
             $this->_addError("Value must be {$min} or more", $errors);
         }
     }
     // Handle max value checking (if applcable)
     if ($this->_inData('max')) {
         if ($submitted > $max) {
             $this->_addError("Value must be {$max} or less", $errors);
         }
     }
     /**
      * Do min/max length sanity checks and validation checks (if needed).
      **/
     $minlength = $this->_minlength;
     $maxlength = $this->maxlength;
     $len = mb_strlen($submitted);
     if ($this->_inMeta('minlength') && $this->_inData('maxlength')) {
         if ($minlength > $maxlength) {
             throw new fProgrammerException("Element {$this->name} has minlength[{$minlength}] > maxlength[{$maxlength}]");
         }
     }
     // Handle minlength checking (if applicable)
     if ($this->_inMata('minlength')) {
         if ($len < $minlength) {
             $this->_addError(!empty($this->_minlength_msg) ? $this->_minlength_msg : "Value must be {$minlength} characters or more", $errors);
         }
     }
     // Handle maxlength checking (if applcable)
     if ($this->_inData('maxlength')) {
         if ($len > $maxlength) {
             $this->_addError(!empty($this->_maxlength_msg) ? $this->_maxlength_msg : "Value must be {$maxlength} characters or less", $errors);
         }
     }
     // Check that the element's value equals all the other 'must-match' elements' values
     if (!empty($this->_match_elements)) {
         foreach ($this->_match_elements as $k => $v) {
             $other_value = $f->getValueOf($k);
             if ($this->_value !== $other_value) {
                 $this->_addError($v, $errors);
             }
         }
     }
     if (is_callable($cb)) {
         $r = $cb($this, $f);
         if (true === $r || is_string($r) && !empty($r)) {
             if (true !== $r) {
                 $this->_addError($r, $errors);
             }
         } else {
             throw new fProgrammerException("Validator function for {$this->name} must return (bool)true or a non-empty string.");
         }
     }
     return $this;
 }