Drupal Studs 帮助我使用 form_alter 钩子(我就快到了)
所以我认为我在概念上已经差不多了,但需要一些缺失的指导。
目标是向普通用户注册表单添加一些字段,对其进行一些样式设置,然后将其提交并将额外的字段存储在表中。
这是我到目前为止所拥有的。有人可以给我最后的推动并让我继续前进吗?请帮我。另外,如何应用一些小的样式,例如对齐新的表单字段?
太感谢了 !!!!!!!!!
function module_menu() {
$items = array();
$items['school/registration'] = array(
'title' => 'Upgraded Registration Form',
'page callback' =>'module_school_register',
'type' => MENU_CALLBACK
);
返回$items;
}//函数结束
function module_school_register(){
返回 drupal_get_form('form_school_register');
}//函数结束
function module_school_form_alter(&$form, $form_state, $form_id)
{
dsm($form_id);
if ($form_id == '用户注册表单')
{
// 通过在前面添加另一个提交处理程序数组来修改“#submit”表单属性
$form['#submit'] = array_merge(
array('_module_registration_submit' => array()),
$表单['#提交']
);
}
}
函数_module_registration_submit($form_id, $form_values) {
// 将额外的数据存储在不同的表中
}
函数 module_registration_validate($form, &$form_state)
{
$错误=0;
//这里的验证内容,如果出现问题,或者您想要执行此操作,则将 $error 设置为 true。完全取决于你如何设置错误。
如果($错误)
{
form_set_error('new_field_name', '啊,出了什么问题!');
}
}
So I think I am almost there conceptually but need some missing pointers.
Objective is to add a few more fields to the normal user registration form, style it a little, then submit it with storing the extra fields in a table.
This is what I have so far. Can someone give me the final nudge and get me going. Please help me. Also how do I apply some minor styling like aligning the new form fields ?
Thank you so much !!!!!!!!!
function module_menu() {
$items = array();
$items['school/registration'] = array(
'title' => 'Upgraded Registration Form',
'page callback' =>'module_school_register',
'type' => MENU_CALLBACK
);
return $items; }//end of the function
function module_school_register(){ return drupal_get_form('form_school_register'); }//end of the function
function module_school_form_alter(&$form, $form_state, $form_id) {
dsm($form_id);
if ($form_id == 'user_registration_form') { // modify the "#submit" form property by prepending another submit handler array $form['#submit'] = array_merge( array('_module_registration_submit' => array()), $form['#submit'] );
} }
function _module_registration_submit($form_id, $form_values) { // store extra data in different table }
function module_registration_validate($form, &$form_state) { $error=0; //Validation stuff here, set $error to true if something went wrong, or however u want to do this. Completely up to u in how u set errors. if ($error) { form_set_error('new_field_name', 'AHH SOMETHING WRONG!'); } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您在推出自己的解决方案之前先看看内容配置文件模块。
您可以为学校注册定义自定义内容类型(节点),添加 cck 字段,并将其激活为内容配置文件。在内容配置文件设置中,您可以在用户注册表单上将其激活。零代码!
I suggest you have a look at content profile module before rolling your own solution.
You define your a custom content type (node) for your school registration, add in you cck fields, and activate it as a content profile. On content profile settings, you then activate it on user registration form. Zero code !
RedBen 是对的,内容配置文件可能是更好的解决方案。
看起来您正在以一种奇怪的方式添加第二个提交处理程序 - 它只是一个函数的名称,而不是一个数组。您是否检查过您的提交处理程序正在运行?
因为对处理程序的引用是一个简单的字符串,所以
如果
您需要它在标准处理程序,使用
array_unshift
将其推送到#submit
数组的开头。RedBen is right, content profile may be a better solution.
It seems like you're adding the second submit handler in a strange way - it's simply the name of a function, not an array. Have you checked that your submit handler is running?
Because the reference to the handler is a simple string, you just need to append it to the array using
$form['#submit'][] = '_module_registration_submit'
If you need it to run before the standard handler, use
array_unshift
to push it onto the beginning of the#submit
array.