5

I am using Drupal7. I also use ife module. I also wrote a custom module

Here is my code

function mymodule_validate($form, &$form_state) {
    $name=$form_state['input']['name'];
    $pass=$form_state['input']['pass'];
    if(empty($pass) || $pass=='') {
           form_set_error('name', t('dummy message'));
        $errors=form_get_errors();
        print_r($errors);
        return $form;
    }

I also did some RND. when I check $form_state under complete form I got this array

 [complete form] => Array
        (
            [#action] => /logintest/node?destination=node
            [#id] => user-login-form
            [#validate] => Array
                (
                    [0] => user_login_name_validate
                    [1] => user_login_authenticate_validate
                    [2] => user_login_final_validate
                    [3] => mymodule_validate
                    [4] => ife_form_validator
                )

Here means the sequence of module working functions.

Now when I print $errors, it is showing

Array
(
    [pass] => Password field is required.
    [name] => Sorry, unrecognized username or password. <a href="/logintest/user/password">Have you forgotten your password?</a>
)

But I am accepting

Array
(
    [pass] => Password field is required.
    [name] => dummy message
)

But I don't know why it is not overwriting custom message.. Can anybody tell me how to resolve this issue.

Thanks a lot.

5

You can try this under your hook_validate

Here i added new function name mymodule_soft_block_validate

 function mymodule_validators() {
        return array('user_login_name_validate', 'user_login_authenticate_validate', 'mymodule_soft_block_validate','user_login_final_validate','mymodule_validate');
    }

function  mymodule_soft_block_validate($form, &$form_state) {
        $pass=$form_state['values']['pass'];
        if(empty($pass) || $pass=='') {
            form_set_error('name', t('dummy message'));
            return $form;
        }           

}
  • not working/ but message is displaying . – Rashmi Aug 7 '13 at 11:20
  • jst edited the answer again – Hitu Bansal Aug 7 '13 at 11:26
  • yeah its working now – Rashmi Aug 7 '13 at 11:27
0

I think you should use

$name=$form_state['values']['name'];
$pass=$form_state['values']['pass'];

Instead of:

$name=$form_state['input']['name'];
$pass=$form_state['input']['pass'];

For the rest, I am not sure exactly what is the error. Can you clarify a little bit the use case. Regards !

  • i jst want to override form_set_message. – Rashmi Aug 7 '13 at 8:58
  • and above code is not working – Rashmi Aug 7 '13 at 8:59
0

You can try this one for setting the error message.

function hook_validate($form, &$form_state) {
  if (strlen($form_state['values']['name'])==0) { 
    form_set_error('name', t('word not long enough'));
  }
}
  • it is showing Username field is required. this message – Rashmi Aug 7 '13 at 10:36
  • not working :). – Rashmi Aug 7 '13 at 10:36
  • 1
    you can re order your validate function to be called last in your custom module. – Ram Aug 7 '13 at 10:44
  • just done still not working – Rashmi Aug 7 '13 at 10:58
0
[#validate] => Array
                (
                    [0] => user_login_name_validate
                    [1] => user_login_authenticate_validate
                    [2] => user_login_final_validate
                    [3] => mymodule_validate
                    [4] => ife_form_validator
                )

As I can see from your RND that ife_form_validator is overriding your messages. For this purpose, you will have to increase the weight of module.

Drupal 7:

/**
 * Implements hook_enable()
 */
function MODULE_NAME_enable() {
    db_update('system')
    ->fields(array('weight' => 100))
    ->condition('type', 'module')
    ->condition('name', 'MODULE_NAME')
    ->execute();
}

Drupal 6:

/**
 * Implementation of hook_install()
 */
function MODULE_NAME_install() {
  drupal_install_schema('devel');

  // New module weights in core: put devel as the very last in the chain.
  db_query("UPDATE {system} SET weight = 100 WHERE name = 'MODULE_NAME'");

  ...
}

I would suggest that you verify the weight of ife_form_validator module first. Then, increase the weight of the module in which you are using the following code:

function mymodule_validate($form, &$form_state) {
  $name=$form_state['input']['name'];
  $pass=$form_state['input']['pass'];
  if(empty($pass) || $pass=='') {
    form_set_error('name', t('dummy message'));
    $errors=form_get_errors();
    print_r($errors);
    return $form;
}
  • jst tried. given weight 2000 to mymodule as ife having weigth of 1000.. still not working . – Rashmi Aug 7 '13 at 10:59
  • [#validate] => Array ( [0] => user_login_name_validate [1] => user_login_authenticate_validate [2] => user_login_final_validate [3] => ife_form_validator [4] => loginsecurity_validate ) – Rashmi Aug 7 '13 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.