Hello everyone,

I have been converting a D6 module into D7, and I have not been able to figure out the way to perform the validation of the block configuration settings in D7.

In Drupal 6 I use the hook_form_block_admin_configure_alter, the code is:

/**
 * Implements hook_form_FORM-ID_alter().
 */
function fb_likebox_form_block_admin_configure_alter(&$form, $form_state) {
  //Only modify the form if it involve our form element this function will be called for every block configure form
  if (empty($form['block_settings']['fb_likebox_display_settings']) || empty($form['block_settings']['fb_likebox_theming_settings'])) {
    return;
  }
  //Add our validation function
  $form['#validate'][] = '_fb_likebox_validate_block_settings';
}

/**
 * Perform the validation of the block settings.
 */
function _fb_likebox_validate_block_settings(&$form, $form_state) {
  // Facebook display settings validation.
  $fb_url = $form_state['values']['fb_likebox_url'];
  if (!valid_url($fb_url, TRUE)) {
    form_set_error('fb_likebox_url', t('Please enter a valid url'));
  }
  // Facebook theming settings validation.
  $fb_width = $form_state['values']['fb_likebox_width'];
  if (!is_numeric($fb_width) || intval($fb_width) <= 0) {
    form_set_error('fb_likebox_width', t('Width should be a number bigger than 0'));
  }
  $fb_height = $form_state['values']['fb_likebox_height'];
  if (!is_numeric($fb_height) || intval($fb_height) <= 0) {
    form_set_error('fb_likebox_height', t('Height should be a number bigger than 0'));
  }
}

I found this solution for D6 at http://hddigitalworks.com/ja/block-configure-form-validation
I have been looking for a similar solution in D7, but I have not found any.

How should I call a validation function for the block config settings in D7?.

Thank you very much,

David

Comments

aalamaki’s picture

Been struggling with similar myself and what I've come across is that this doesn't seem to be possible because the block configuration uses a separate hook_block_save().

If you wish to add validation for the whole form, the best way I have managed to implement myself is to add a custom function to the code with $edit as a parameter and a boolean as a return value. Then call the function in the hook_block_save() before saving data in order to perform the extra checks.

Works fine with, you can set form_set_error in the custom function etc, the only exception is that it will not highlight in red values that are incorrect in the form. Also, it will still try to redirect to the blocks listing page but this can be bypassed by adding a drupal_goto() to the custom function.

folkertdv’s picture

This is the code I wrote, and it works just fine for me.

/**
 * Implements hook_block_configure().
 */
function mymodule_block_configure($delta = '') {
  if ($delta == 'myblock') {
    $form = array();
    $form['website'] = array(
      '#type'          => 'textfield',
      '#title'         => t('Website'),
      '#size'          => 60,
      '#required'      => TRUE,
      '#default_value' => variable_get('mymodule_myblock_website', ''),
    );
    return $form;
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Adds validation of block configuration custom fields.
 */
function mymodule_form_block_admin_configure_alter(&$form, &$form_state, $form_id) {
  if ($form['module']['#value'] == 'mymodule') {
    $form['#validate'][] = 'mymodule_block_validate';
  }
}

/**
 * Form validation callback of block configuration.
 */
function mymodule_block_validate(&$form, $form_state) {
  $delta = $form['delta']['#value'];
  if ($delta == 'myblock') {
    if (!valid_url($form_state['values']['website'], TRUE)) {
      form_set_error('website', t('The value provided for %field is not a valid URL.', array('%field' => 'Website')));
    }
  }
}
drozas’s picture

Thanks folkertdv!,

I can confirm your solution using this hook works. Another code example can be found at http://drupalcode.org/project/fb_likebox.git/blobdiff/7218b10b8c7ed13c11...

Cheers,

David

Tom.Camp’s picture

Another way, perhaps a better way, would be to add '#element_validate' => array( 'my_validation_function' ) to your form element. I came across a reference to this here.