/**
* Verifies that the value for the specified field is valid.
*
* @param array $field
* @param mixed $value
* @param mixed $error Returned error message
*
* @return boolean
*/
public function verifyCustomFieldValue(array $field, &$value, &$error = '')
{
$error = false;
switch ($field['field_type'])
{
case 'textbox':
$value = preg_replace('/\r?\n/', ' ', strval($value));
// break missing intentionally
case 'textarea':
case 'bbcode':
$value = trim(strval($value));
if ($field['field_type'] == 'bbcode')
{
$value = XenForo_Helper_String::autoLinkBbCode($value);
}
if ($field['max_length'] && utf8_strlen($value) > $field['max_length'])
{
$error = new XenForo_Phrase('please_enter_value_using_x_characters_or_fewer', array('count' => $field['max_length']));
return false;
}
$matched = true;
if ($value !== '')
{
switch ($field['match_type'])
{
case 'number':
$matched = preg_match('/^[0-9]+(\.[0-9]+)?$/', $value);
break;
case 'alphanumeric':
$matched = preg_match('/^[a-z0-9_]+$/i', $value);
break;
case 'email':
$matched = Zend_Validate::is($value, 'EmailAddress');
break;
case 'url':
if ($value === 'http://')
{
$value = '';
break;
}
if (substr(strtolower($value), 0, 4) == 'www.')
{
$value = 'http://' . $value;
}
$matched = Zend_Uri::check($value);
break;
case 'regex':
$matched = preg_match('#' . str_replace('#', '\#', $field['match_regex']) . '#sU', $value);
break;
case 'callback':
$matched = call_user_func_array(
array($field['match_callback_class'], $field['match_callback_method']),
array($field, &$value, &$error)
);
default:
// no matching
}
}
if (!$matched)
{
if (!$error)
{
$error = new XenForo_Phrase('please_enter_value_that_matches_required_format');
}
return false;
}
break;
case 'radio':
case 'select':
$choices = unserialize($field['field_choices']);
$value = strval($value);
if (!isset($choices[$value]))
{
$value = '';
}
break;
case 'checkbox':
case 'multiselect':
$choices = unserialize($field['field_choices']);
if (!is_array($value))
{
$value = array();
}
$newValue = array();
foreach ($value AS $key => $choice)
{
$choice = strval($choice);
if (isset($choices[$choice]))
{
$newValue[$choice] = $choice;
}
}
$value = $newValue;
break;
}
return true;
}