When creating an image block where the description of the block is the same as an existing block, a PDO Exception is thrown as follows:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'block_name_here' for key 'info': INSERT INTO {imageblock} (body, info, format) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array ( [:db_insert_placeholder_0] => [:db_insert_placeholder_1] => block_name_here [:db_insert_placeholder_2] => filtered_html ) in imageblock_add_block_form_submit() (line 73 of /sites/all/modules/imageblock/imageblock.admin.inc).

This is confusing for a user, who usually doesn't know why her attempt to create a new image block has failed.

I think a validation handler should be written for the "add image" form which checks whether a block with the same info key exists, and throws a user-friendly form_set_error() if so.

Comments

diegosh’s picture

Assigned: Unassigned » diegosh

Check for duplicates block info to prevent a PDO exception and show a message if it's necessary.

function imageblock_configure_form_validate($form, &$form_state) {
  // TODO: make these settings, make this work
  // Check if already exists this block description to prevent a PDO exception.
  $info = $form_state['values']['info'];
  $duplicate_desc = db_query('SELECT count(info) FROM {imageblock} WHERE info = :info',array(':info'=>$info))->fetchField();
  if ($duplicate_desc) {
    form_set_error('imageblock', t("Failed to create the block; already exists one block with this description.Please choose an unused block description and try again.")); 
  }
diegosh’s picture

diegosh’s picture

Status: Active » Needs review
ericbobson’s picture

Looks great!

Suggest you could improve the wording of the error message, e.g.

form_set_error('imageblock', t("Failed to create block. A block with this description already exists. Please edit your block description and try again."));

Thanks!

guypaddock’s picture

Status: Needs review » Needs work

This prevents updating an existing image block.

diegosh’s picture

Version: 7.x-1.3 » 7.x-1.x-dev
StatusFileSize
new1.04 KB
diegosh’s picture

Status: Needs work » Needs review

  • diegosh committed 7f74ee1 on 7.x-1.x
    Issue #2291883 by diegosh: Better error message needed for duplicate...
diegosh’s picture

Status: Needs review » Fixed
guypaddock’s picture

Status: Fixed » Needs work

This still needs work. As I mentioned on 2014-12-22, with this applied, you can no longer update an existing image block -- it blocks the save saying you have a duplicate.

diegosh’s picture

Status: Needs work » Patch (to be ported)

You're right.
Iive just added this code snippet and soon i send the patch

if($form['#form_id'] == 'imageblock_add_block_form') {
$info = $form_state['values']['info'];
$duplicate_desc = db_query('SELECT count(info) FROM {imageblock} WHERE info = :info',array(':info'=>$info))->fetchField();
if ($duplicate_desc) {
form_set_error('imageblock_duplicate', t("Failed to create block. A block with this description already exists. Please edit your block description and try again."));
}
}

With this clause only check for new blocks

diegosh’s picture

Status: Patch (to be ported) » Needs review
StatusFileSize
new1.3 KB
diegosh’s picture

  • diegosh committed e62a7db on 7.x-1.x
    Issue #2291883 by diegosh: Only for new blocks Better error message...
diegosh’s picture

Status: Needs review » Fixed

I've just committed this change. Many thanks guypaddock feel free to suggest new ideas or improvements.

diegosh’s picture

Status: Fixed » Closed (fixed)