instead of typing the same rule over and over again it would be nice to reuse it.

Comments

g089h515r806’s picture

Version: » 7.x-1.0-rc1
Status: Active » Closed (fixed)

Now, field validation rule is cloneable.If you want to reuse it, just clone it at page admin/structure/field_validation.

jordanmagnuson’s picture

Version: 7.x-1.0-rc1 » 7.x-2.x-dev
Status: Closed (fixed) » Active

Any plans for 2.x?

Seems silly to define the same rule over and over again, and name it something different every time, rather than just reusing the same validation rule on multiple fields...

jordanmagnuson’s picture

I see that we can indeed clone field validations in the 2.x branch, but what about simply reusing existing validations? That would seem like a much better solution...

g089h515r806’s picture

I have think about this feature when i start build this module.
In webform validation , one validation rule could attach to several webform components. I removed this feature.
(1) field is much complex than webform component, there are more field types, field coudl also be multi values, multi column.
(2) The module will become very complex that i could not control it.

The benifit we could earn from attach one validation rule to one field is:
(1) we could add several rules to one field.
(2) we could one validation rule to all field types, at leat it is much simpler to do this.

I also want to build a UI like Views UI.

rooby’s picture

It is kind of annoying you can't reuse rules and you can't reuse rule names when you do duplicate a rule.

Would it be feasible to have an "Add existing rule" dropdown, like drupal core provides the "Add existing field" one?
It would be on the admin/structure/types/manage/article/fields/body/validation page (and other equivalent pages).

There would just have to be a message to tell people that the settings are global settings and they will affect all fields that this validator is used on.

(1) we could add several rules to one field.

I don't see why this can't still be the case.

(2) we could one validation rule to all field types, at leat it is much simpler to do this.

I'm not sure I understand this one.

g089h515r806’s picture

It is possible. But it needs a lot works to do it.

I prefer following structure in the future:


$bundle_validation = array(
  field_myfield1 = array(
     validation_rule_0  = array(
        'validator' => 'unique',
		'column' => 'value',
		'description' => 'My field unique',
		'config' => array(
		  'scope' => 'global',
		  'bypass_validation' => 'administrator'
		),
		'error_message' => 'This field must be unique',
     ),
     validation_rule_1  = array(
        'validator' => 'integer',
		'column' => 'value',
		'description' => 'My field is integer',
		'config' => array(
		  'min' => 100,
		  'max' => 200,
		),
		'error_message' => 'This field must be integer',
     ),
  ),
  field_myfield2 = array(
     validation_rule_0  = array(
        'validator' => 'unique',
		'column' => 'value',
		'description' => 'My field unique',
		'config' => array(
		  'scope' => 'global',
		  'bypass_validation' => 'administrator'
		),
		'error_message' => 'This field must be unique',
     ),
     validation_rule_1  = array(
        'validator' => 'integer',
		'column' => 'value',
		'description' => 'My field is integer',
		'config' => array(
		  'min' => 100,
		  'max' => 200,
		),
		'error_message' => 'This field must be integer',
     ),
  ),
  ....
);

Then we do not need to enter a validation rule name. validation rules export/import base on bundle/entity name.

hnln’s picture

Issue summary: View changes

FYI, it is also possible to create your own validation plugin and use that for all the fields.

raulmuroc’s picture

Some steps on this? Oh As far as I understand, there are no plans to make it as you prefer to have a Field Validation perspective instead of Validation'sp erspective for fields. So each field has its own validation so no validation for many fields.

I'm not sure is the best... makes the module quite annoying to use and maintain. Imagine I appy 500 times the "numerical value" validation, with a custom message and I want everywhere the same message... must write 500 this message for each validation.

Really not maintainable/scalable.

hnln’s picture

That was my problem exactly. I created a custom validation plugin, you can do that by copying any of the module's validators and place it in your own module's plugins/validator directory (like any ctools plugin). You can probably extend on the existing numeric value validator and implement your own 'get_error_message' method. The only downside is that I had to alter the ctools_export_ui_edit_item form to remove the standard 'error_message' (as it's required). This might be something for the maintainers, to make it possible to let the validator decide if a custom error message is required.

The code I needed (beware that the naming of the class is important, otherwise it will complain that it doesn't extend field_validation_validator).

/**
 * @file
 * Contains yourmodule_telephone_format.inc.
 */

$plugin = array(
  'label' => t('Your module: telephone format'),
  'description' => t('Validate telephone format.'),
  'handler' => array(
    'class' => 'field_validation_yourmodule_telephone_format_validator',
  ),
);

class field_validation_yourmodule_telephone_format_validator extends field_validation_validator {

  /**
   * {@inheritdoc}
   */
  public function validate() {

    $pattern = '/^(\d{2} \d{3}|\d{3} \d{2}) \d{2} \d{2}$/';
    if ($this->value != '' && !preg_match($pattern, $this->value)) {
      $this->set_error();
    }

  }

  /**
   * {@inheritdoc}
   */
  public function get_error_message() {

    $field_name = strtolower(i18n_field_translate_property($this->instance, 'label'));
    $message = t('There is an error in the format of the telephone number for @field.', array('@field' => $field_name));
    return $message;

  }

}

and

/**
 * Implements hook_form_ctools_export_ui_edit_item_form_alter().
 */
function yourmodule_form_ctools_export_ui_edit_item_form_alter(&$form, $form_state) {

  // Modify the field validation settings form.
  if (isset($form_state['plugin']['name']) && $form_state['plugin']['name'] == 'field_validation_export_ui') {

    $validator = '';
    if (isset($form_state['values']['validator']) && $form_state['values']['validator']) {
      $validator = $form_state['values']['validator'];
    }
    elseif (isset($form_state['item']->validator) && $form_state['item']->validator) {
      $validator = $form_state['item']->validator;
    }

    if ($validator == 'field_validation_yourmodule_telephone_format_validator') {

      $form['error_message']['#access'] = FALSE;
      $form['token_help']['#access'] = FALSE;

    }

  }

}