I'm having some issues providing a validation handler for a custom action. I think I'm following the API documentation as written but for some reason I can't get a step into the function.

  $actions['property'] = array(
    'label' => t('Set an element property'),
    'group' => 'Rules Forms',
    'parameter' => array(
      'form' => array('type' => 'form', 'label' => t('Form')),
      'element' => array(
        'type' => 'form_element',
        'label' => t('Form element'),
        'restriction' => 'selector',
        'description' => t('The element that should be targeted.'),
      ),
      'property' => array(
        'type' => 'text',
        'label' => t('Property'),
        'description' =>
          t('Refer to the ') .
          l('Form API Reference', 'http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/7#properties', array('absolute' => TRUE, 'external' => TRUE)) .
          t(' for available properties. For example: #type'),
      ),
      'value' => array(
        'type' => 'text',
        'label' => t('Value'),
        'description' => t('The value to be set to the above property. For example: hidden'),
      ),
    ),
    'access callback' => 'rules_forms_integration_access',
    'base' => 'rules_forms_action_set_property',
    'callbacks' => array(
      'validate' => 'rules_forms_action_set_property_validate',
    ),
    'help' => t('An example use case of this action is to change a #type property from fieldset to container.'),
  );
function rules_forms_action_set_property_validate($form, &$form_state) {
  // Require property to begin with #.
  if (!preg_match('/^#/', $form_state['test'])) {
    form_set_error('property', t('Property values must begin with a #. For example: #type.'));
  }
  dsm($form_state, 'FORM_STATE');
} // Function never called?! :(

Any suggestions or insights would be greatly appreciated and help restore some sanity!

Thank you,

Comments

seandunaway’s picture

Component: Rules Core » Rules Engine

By the way, I just recently discovered Rules, and it is amazing! I love this module! Thank you!

itangalo’s picture

Category: support » bug

+1 on this one -- I've been having problems creating custom validation functions too.

westbywest’s picture

subscribing

tr’s picture

Component: Rules Engine » Rules Core
Category: Bug report » Support request
Status: Active » Fixed

The 'validate' callback is not a form validator - it only validates the RulesAbstractPlugin element, and takes only the element as an argument. So your function signature is wrong for rules_forms_action_set_property_validate().

The 'form_alter' callback is what you want if you want to add form validators. Implement the 'form_alter' callback then you can add a form validator to the form in the same way you'd do it with hook_form_alter(). So you'll need two functions, one to alter the form and one to validate the form.

You can see examples of the 'validate' callback in modules/data.rules.inc.
You can also see examples of the 'form_alter' callback in modules/data.rules.inc, and you can see where some of these form_alter callbacks add a submit handler to the form.

tr’s picture

Status: Fixed » Closed (fixed)