Fieldmanager_Field::presave PHP Method

presave() public method

Presave function, which handles sanitization and validation
public presave ( mixed $value, $current_value = [] ) : sanitized
$value mixed If a single field expects to manage an array, it must override presave()
return sanitized values.
    public function presave($value, $current_value = array())
    {
        // It's possible that some elements (Grid is one) would be arrays at
        // this point, but those elements must override this function. Let's
        // make sure we're dealing with one value here.
        if (is_array($value)) {
            $this->_unauthorized_access(__('presave() in the base class should not get arrays, but did.', 'fieldmanager'));
        }
        foreach ($this->validate as $func) {
            if (!call_user_func($func, $value)) {
                $this->_failed_validation(sprintf(__('Input "%1$s" is not valid for field "%2$s" ', 'fieldmanager'), (string) $value, $this->label));
            }
        }
        return call_user_func($this->sanitize, $value);
    }

Usage Example

 /**
  * Presave function, which handles sanitization and validation
  *
  * @param int|array $values This will either be a post ID or array of
  *                          post IDs.
  * @return int|array Sanitized values.
  */
 public function presave($values, $current_values = array())
 {
     if (is_array($values)) {
         // Fieldmanager_Field doesn't like it when $values is an array,
         // so we need to replicate what it does here.
         foreach ($values as $value) {
             foreach ($this->validate as $func) {
                 if (!call_user_func($func, $value)) {
                     $this->_failed_validation(sprintf(__('Input "%1$s" is not valid for field "%2$s" ', 'fm-zones'), (string) $value, $this->label));
                 }
             }
         }
         return array_map($this->sanitize, $values);
     } else {
         return parent::presave($values, $current_values);
     }
 }