1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-05-09 00:55:23 +02:00

[ticket/11201] Move error message generation to type class

PHPBB3-11201
This commit is contained in:
Joas Schilling 2014-01-14 12:04:02 +01:00
parent 190c2e989a
commit d57c43d397
7 changed files with 25 additions and 55 deletions

View File

@ -190,48 +190,8 @@ class profilefields
if (($cp_result = $profile_field->validate_profile_field($check_value, $row)) !== false)
{
// If not and only showing common error messages, use this one
$error = '';
switch ($cp_result)
{
case 'FIELD_INVALID_DATE':
case 'FIELD_INVALID_VALUE':
case 'FIELD_REQUIRED':
$error = $this->user->lang($cp_result, $row['lang_name']);
break;
case 'FIELD_TOO_SHORT':
case 'FIELD_TOO_SMALL':
$error = $this->user->lang($cp_result, (int) $row['field_minlen'], $row['lang_name']);
break;
case 'FIELD_TOO_LONG':
case 'FIELD_TOO_LARGE':
$error = $this->user->lang($cp_result, (int) $row['field_maxlen'], $row['lang_name']);
break;
case 'FIELD_INVALID_CHARS':
switch ($row['field_validation'])
{
case '[0-9]+':
$error = $this->user->lang($cp_result . '_NUMBERS_ONLY', $row['lang_name']);
break;
case '[\w]+':
$error = $this->user->lang($cp_result . '_ALPHA_ONLY', $row['lang_name']);
break;
case '[\w_\+\. \-\[\]]+':
$error = $this->user->lang($cp_result . '_SPACERS_ONLY', $row['lang_name']);
break;
}
break;
}
if ($error != '')
{
$cp_error[] = $error;
}
// If the result is not false, it's an error message
$cp_error[] = $cp_result;
}
}
$this->db->sql_freeresult($result);

View File

@ -89,7 +89,7 @@ class type_bool implements type_interface
if (!$field_value && $field_data['field_required'])
{
return 'FIELD_REQUIRED';
return $this->user->lang('FIELD_REQUIRED', $field_data['lang_name']);
}
return false;

View File

@ -114,17 +114,17 @@ class type_date implements type_interface
if ((!$day || !$month || !$year) && $field_data['field_required'])
{
return 'FIELD_REQUIRED';
return $this->user->lang('FIELD_REQUIRED', $field_data['lang_name']);
}
if ($day < 0 || $day > 31 || $month < 0 || $month > 12 || ($year < 1901 && $year > 0) || $year > gmdate('Y', time()) + 50)
{
return 'FIELD_INVALID_DATE';
return $this->user->lang('FIELD_INVALID_DATE', $field_data['lang_name']);
}
if (checkdate($month, $day, $year) === false)
{
return 'FIELD_INVALID_DATE';
return $this->user->lang('FIELD_INVALID_DATE', $field_data['lang_name']);
}
return false;

View File

@ -90,12 +90,12 @@ class type_dropdown implements type_interface
if (!isset($this->profilefields->options_lang[$field_data['field_id']][$field_data['lang_id']][$field_value]))
{
return 'FIELD_INVALID_VALUE';
return $this->user->lang('FIELD_INVALID_VALUE', $field_data['lang_name']);
}
if ($field_value == $field_data['field_novalue'] && $field_data['field_required'])
{
return 'FIELD_REQUIRED';
return $this->user->lang('FIELD_REQUIRED', $field_data['lang_name']);
}
return false;

View File

@ -80,11 +80,11 @@ class type_int implements type_interface
if ($field_value < $field_data['field_minlen'])
{
return 'FIELD_TOO_SMALL';
return $this->user->lang('FIELD_TOO_SMALL', (int) $row['field_minlen'], $row['lang_name']);
}
else if ($field_value > $field_data['field_maxlen'])
{
return 'FIELD_TOO_LARGE';
return $this->user->lang('FIELD_TOO_LARGE', (int) $row['field_maxlen'], $row['lang_name']);
}
return false;

View File

@ -50,7 +50,7 @@ interface type_interface
*
* @param mixed $field_value Field value to validate
* @param array $field_data Array with requirements of the field
* @return mixed String with key of the error language string, false otherwise
* @return mixed String with the error message
*/
public function validate_profile_field(&$field_value, $field_data);

View File

@ -44,16 +44,16 @@ abstract class type_string_common
}
else if (trim($field_value) === '' && $field_data['field_required'])
{
return 'FIELD_REQUIRED';
return $this->user->lang('FIELD_REQUIRED', $field_data['lang_name']);
}
if ($field_data['field_minlen'] && utf8_strlen($field_value) < $field_data['field_minlen'])
{
return 'FIELD_TOO_SHORT';
return $this->user->lang('FIELD_TOO_SHORT', (int) $row['field_minlen'], $row['lang_name']);
}
else if ($field_data['field_maxlen'] && utf8_strlen($field_value) > $field_data['field_maxlen'])
{
return 'FIELD_TOO_LONG';
return $this->user->lang('FIELD_TOO_LONG', (int) $row['field_maxlen'], $row['lang_name']);
}
if (!empty($field_data['field_validation']) && $field_data['field_validation'] != '.*')
@ -61,7 +61,17 @@ abstract class type_string_common
$field_validate = ($field_type != 'text') ? $field_value : bbcode_nl2br($field_value);
if (!preg_match('#^' . str_replace('\\\\', '\\', $field_data['field_validation']) . '$#i', $field_validate))
{
return 'FIELD_INVALID_CHARS';
switch ($row['field_validation'])
{
case '[0-9]+':
return $this->user->lang('FIELD_INVALID_CHARS_NUMBERS_ONLY', $row['lang_name']);
case '[\w]+':
return $this->user->lang('FIELD_INVALID_CHARS_ALPHA_ONLY', $row['lang_name']);
case '[\w_\+\. \-\[\]]+':
return $this->user->lang('FIELD_INVALID_CHARS_SPACERS_ONLY', $row['lang_name']);
}
}
}