对 Drupal 7 感到困惑 - 通过 hook_form_alter 添加 user_profile_form #validation 回调
Stumped on Drupal 7 - adding user_profile_form #validation callback via hook_form_alter
我已经搜索、阅读和重读。我什至将测试模块剥离到最基本的元素。
我认为这段代码应该可以工作,但从未调用过验证回调。我将回调添加到表单,但它在接下来的步骤中消失了。
最终,我的目标是 "validate" 提交用户名,以满足特定业务 objective。我在添加表单验证回调时遇到了困难,但我相信一旦克服了这个障碍,其余的功能就很简单了。
比我聪明的人能给我指明正确的方向以正确添加验证回调吗?
<?php
/*
Implements hook_form_alter();
*/
function mymod_form_alter($form, $form_state, $form_id) {
// catch the user profile form.
if('user_profile_form' == $form_id) {
// set debug message
drupal_set_message(t('adding validation callback in hook_form_alter()...'), 'warning');
// add a validation callback
$form['#validate'][] = 'mymod_user_profile_form_validate';
}
}
/*
Implements hook_form_<form_id>_alter();
If mymod_form_alter() was successful, I wouldn't need this hook, it's just here for verification.
*/
function mymod_form_user_profile_form_alter($form, $form_state, $form_id) {
// check to see if our validation callback is present
if(!in_array('mymod_user_profile_form_validate', $form['#validate'])) {
// our validatiation callback is not present
drupal_set_message(t('The validation callback is missing from #validate in hook_form_[form_id]_alter()'), 'error');
// since it's not there, try re-adding it?
$form['#validate'][] = 'mymod_user_profile_form_validate';
} else {
// We should see this message, but don't.
drupal_set_message(t('The validation callback exists!'), 'status');
}
}
/*
?? Implements hook_form_validate(); (or not...) ??
*/
function mymod_user_profile_form_validate($form, $form_state) {
// why is mymod_user_profile_form_validate() never called??
drupal_set_message(t('Validation callback called! Whoopee! Success!'), 'status'); // This never happens!
// if this was working, we would do a bit of logic here on the username.
//for this test, let's assume we want to return an error on the username field.
form_set_error('name', t('Blah, blah, blah, something about your username...'));
}
变量 $form
和 $form_state
应该是 passed by reference 这样你的钩子函数就可以实际修改它们(否则函数只是得到一个副本)。
您需要在函数签名中使用在变量参数之前添加的 &
(与号)符号:
function mymod_form_alter(&$form, &$form_state, $form_id) {
# $form is referenced from the global scope using '&' symbol
$form['#validate'][] = 'mymod_user_profile_form_validate';
}
我已经搜索、阅读和重读。我什至将测试模块剥离到最基本的元素。
我认为这段代码应该可以工作,但从未调用过验证回调。我将回调添加到表单,但它在接下来的步骤中消失了。
最终,我的目标是 "validate" 提交用户名,以满足特定业务 objective。我在添加表单验证回调时遇到了困难,但我相信一旦克服了这个障碍,其余的功能就很简单了。
比我聪明的人能给我指明正确的方向以正确添加验证回调吗?
<?php
/*
Implements hook_form_alter();
*/
function mymod_form_alter($form, $form_state, $form_id) {
// catch the user profile form.
if('user_profile_form' == $form_id) {
// set debug message
drupal_set_message(t('adding validation callback in hook_form_alter()...'), 'warning');
// add a validation callback
$form['#validate'][] = 'mymod_user_profile_form_validate';
}
}
/*
Implements hook_form_<form_id>_alter();
If mymod_form_alter() was successful, I wouldn't need this hook, it's just here for verification.
*/
function mymod_form_user_profile_form_alter($form, $form_state, $form_id) {
// check to see if our validation callback is present
if(!in_array('mymod_user_profile_form_validate', $form['#validate'])) {
// our validatiation callback is not present
drupal_set_message(t('The validation callback is missing from #validate in hook_form_[form_id]_alter()'), 'error');
// since it's not there, try re-adding it?
$form['#validate'][] = 'mymod_user_profile_form_validate';
} else {
// We should see this message, but don't.
drupal_set_message(t('The validation callback exists!'), 'status');
}
}
/*
?? Implements hook_form_validate(); (or not...) ??
*/
function mymod_user_profile_form_validate($form, $form_state) {
// why is mymod_user_profile_form_validate() never called??
drupal_set_message(t('Validation callback called! Whoopee! Success!'), 'status'); // This never happens!
// if this was working, we would do a bit of logic here on the username.
//for this test, let's assume we want to return an error on the username field.
form_set_error('name', t('Blah, blah, blah, something about your username...'));
}
变量 $form
和 $form_state
应该是 passed by reference 这样你的钩子函数就可以实际修改它们(否则函数只是得到一个副本)。
您需要在函数签名中使用在变量参数之前添加的 &
(与号)符号:
function mymod_form_alter(&$form, &$form_state, $form_id) {
# $form is referenced from the global scope using '&' symbol
$form['#validate'][] = 'mymod_user_profile_form_validate';
}