Commit 350f8acb authored by targoo's avatar targoo

fix validation and errors messages

parent 07260430
......@@ -51,7 +51,7 @@ function color_field_field_info() {
/**
* Implements hook_field_validate().
*
* We want to verify that the items only contain RGB hex values like
* We want to verify that the color items only contain RGB hex values like
* this: #RRGGBB. If the item validates, we do nothing. If it doesn't
* validate, we add our own error notification to the $errors parameter.
*
......@@ -60,34 +60,47 @@ function color_field_field_info() {
function color_field_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
foreach ($items as $delta => $item) {
// Test rgb field.
if (!empty($item['rgb'])) {
if (!preg_match('@^#[0-9a-fA-F]{6}$@', $item['rgb'])) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'color_field_invalid',
'message' => t('Color must be in the hexadecimal format #abcdef.'),
);
}
$regexp = '@^#[0-9a-fA-F]{6}$@';
if (!preg_match($regexp, $item['rgb'])) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'color_invalid',
'message' => t('The value %value provided for %field is not a valid color.', array(
'%value' => trim($item['rgb']),
'%field' => $instance['label'],
)),
'error_element' => array('rgb' => TRUE, 'opacity' => FALSE),
);
}
// Test opacity field.
if (!empty($item['opacity']) && filter_var(
$item['opacity'],
FILTER_VALIDATE_FLOAT,
array(
'options' => array(
'min_range' => 0,
'max_range' => 1
)
)
) === false) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'color_field_invalid',
'message' => t('Opacity need to be a float value'),
);
$regexp = '@^[0]?(\.)(\d)*$|^[01]$@';
if (!preg_match($regexp, $item['opacity'])) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'opacity_invalid',
'message' => t('The value %value provided for %field is not a valid opacity.', array(
'%value' => trim($item['opacity']),
'%field' => $instance['label'],
)),
'error_element' => array('rgb' => FALSE, 'opacity' => TRUE),
);
}
}
}
/**
* Implements hook_field_widget_error().
*
* @see color_field_validate()
* @see form_error()
*/
function color_field_field_widget_error($element, $error, $form, &$form_state) {
if ($error['error_element']['rgb']) {
form_error($element['rgb'], $error['message']);
}
if ($error['error_element']['opacity']) {
form_error($element['opacity'], $error['message']);
}
}
/**
* Implements hook_field_is_empty().
*/
......@@ -104,20 +117,6 @@ function color_field_field_is_empty($item, $field) {
return FALSE;
}
/**
* Implements hook_field_widget_error().
*
* @see color_field_validate()
* @see form_error()
*/
function color_field_widget_error($element, $error, $form, &$form_state) {
switch ($error['error']) {
case 'color_field_invalid':
form_error($element, $error['message']);
break;
}
}
/**
* Implements hook_field_insert().
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment