You are here

function sarnia_schema_rule_form_validate in Sarnia 7

File

./sarnia.rules.inc, line 320

Code

function sarnia_schema_rule_form_validate($form, &$form_state) {
  $non_empty = array(
    'match_type' => t('Match type'),
    'match_value' => t('Match value'),
    'effect' => t('Rule effect'),
  );
  foreach ($form_state['values']['rules'] as $i => $values) {
    if (_sarnia_schema_rule_is_changed($values['rule'], $values)) {

      // Don't allow empty values in rules.
      foreach ($non_empty as $key => $label) {
        if (empty($values[$key])) {
          form_set_error("rules][{$i}][{$key}", t('!label cannot be empty.', array(
            '!label' => $label,
          )));
        }
      }

      // Disallow 'effect' == 'replace' when 'match_type' == 'type'
      if ($values['match_type'] == 'type' && ($values['effect'] = 'replace')) {
        form_set_error("rules][{$i}][effect", t("The 'replace' effect can only be used when matching on 'name' and 'dynamicBase'."));
      }

      // Require a 'replace' value when 'effect' == 'replace'
      if ($values['effect'] == 'replace' && empty($values['replacement'])) {
        form_set_error("rules][{$i}][replacement", t("When using a 'replace' rule, a value must be provided in the 'replacement' field."));
      }
    }
  }
}