Joomla注册:删除不需要的字段


16

首先,我知道这个问题:

/programming/14799733/remove-name-field-from-joomla-registration-form

但是答案中的链接已消失,并且提到的文件路径在最新的Joomla(3.3版)中不存在,因此答案毫无用处。

我想要的很简单:Joomla的注册表格如下:

注册图像默认

我想确保对我的用户而言注册过程尽可能简单,以便他们实际完成注册,并且存在一些问题:

当然,这是一个更好的注册页面:

更好的注册图像

我修改了\ components \ com_users \ models \ forms \ registration.xml文件,使表单看起来像我想要的样子,并且可以正常工作!但是,无论何时提交表单,验证仍然会查找缺少的字段,因此它告知我需要提供一个名称:

“注册失败:请输入您的姓名。”

如何禁用对名称字段以及两个确认字段的检查?

由于名称是必需的,因此我想将名称设置为提供的用户名(因此,默认情况下,用户名和名称相同)。如果用户确实希望注册在其个人资料页面中设置真实姓名,则应该可以,但是不需要。

我会以错误的方式处理吗?我在某些地方读到,修改核心Joomla文件并不好,因为它可能会在更新中中断,因此应该改用“替代”。通常,这些响应只是链接到有关替代项的Joomla页面,而没有提供有关如何使用它们执行请求者所希望的进一步解释。我已尝试找出它们,但无法解决,因此,如果要使用替代方法,请说明如何使用它们进行操作,而不仅仅是链接到替代教程。

非常感谢您可以提供的任何帮助,我已经尝试解决了几个小时!


1
我想指出JED是做这样的扩展的可能来源。
Valentin Despa

@Lodder感谢您插入跳过跳过验证过程的信息,但是我担心它在我的模板中不起作用。您能检查一下代码并更新工作代码吗?当我跟踪@Rickster的过程时,它就像魅力一样工作。我是joomla的新手,因此需要您提供这方面的帮助。提前致谢。

我发现此Stackoverflow问题的答案可能是您想要的:如何在Joomla 3注册中禁用某些字段的验证
Rob L

Answers:


12

理想情况下,所有这些都将在用户插件中完成。我不确定很多人都意识到了这一点,但是您可以从插件中编辑表单(不需要核心技巧)。

同样,插件将在模型尝试保存条目之前看到输入,因此您可以模拟某些字段重复字段以强制其通过验证。

“用户”插件中的此功能使您可以更改表单:

function onContentPrepareForm($form, $data)
{
    if (!($form instanceof JForm))
    {
        $this->_subject->setError('JERROR_NOT_A_FORM');
        return false;
    }

    // Check we are manipulating a valid form, 
    // may also want to check whether this is frontend or admin depending on where all you want to affect
    // JFactory::getApplication()->isAdmin()
    $name = $form->getName();
    if (!in_array($name, array('com_admin.profile', 'com_users.user', 'com_users.profile', 'com_users.registration')))
    {
        return true;
    }

    // remove fields on frontend
    if (!JFactory::getApplication()->isAdmin()) {
        $form->removeField('password2');
        $form->removeField('email2');
    }

    return true;
}

然后,您可以使用onUserBeforeSave函数来重置需要重置以通过验证的任何字段。(这可能不是完全必要的,因此请尝试一下,看看会遇到什么错误。)

function onUserBeforeSave($user, $isnew, $new) {
    $user['password2'] = $user['password1'];
}

8

好的,所以我在考虑一种方法,而无需破解任何内核或安装任何第三方扩展。我的方法将涉及您需要为com_users组件(特别是“ 注册”视图)创建的Template Override

现在,对于字段(假设已设置所有模板覆盖),我们将不会删除它们,而是将它们隐藏起来,因为控制器和模式需要它们中的数据。因此,打开以下文件,该文件现在是您的模板替代:

模板/YOUR_TEMPLATE/html/com_users/registration/default.php

并将以下代码添加到文件顶部附近JHtml::_('behavior.formvalidation');

$doc = JFactory::getDocument();

$js = "
        jQuery(document).ready(function($){

            // Define the variables
            var regForm     = $('#member-registration');
            var name        = regForm.find('#jform_name');
            var password    = regForm.find('#jform_password1');
            var password2   = regForm.find('#jform_password2');
            var email       = regForm.find('#jform_email1');
            var email2      = regForm.find('#jform_email2');

            // Hide the required field, star, name, confirm pass and confirm email
            regForm.find('.spacer').parents('.control-group').hide();
            regForm.find('.star').hide();
            name.parents('.control-group').hide();
            password2.parents('.control-group').hide();
            email2.parents('.control-group').hide();

            // Add a default value to the name field
            name.val('Anonymous');

            // Clone password and email values to the confirm fields
            email.on('keyup', function() {
                email2.val( this.value );
            });
            password.on('keyup', function() {
                password2.val( this.value );
            });

        });    
    ";    
$doc->addScriptDeclaration($js);

我添加了一些注释,以便您了解每个代码段的功能。

希望这可以帮助 :)


1

@DavidFritsch的回答非常有帮助。我发现的几个问题是:

A)您不能完全删除某些必填字段,因为它会导致表单提交时的数据过滤出现问题(请参见下面的代码中的注释)。要解决此问题,请隐藏而不是从表单对象中删除字段。B)只有在为时已晚以防止注册验证逻辑拒绝表单提交时才触发onUserBeforeSave事件。而是使用onUserBeforeDataValidation事件。

在我的特定情况下,我想要的只是电子邮件地址和密码。但是,Joomla带来了一个麻烦,因为电子邮件地址是在密码之后显示的(由registration.xml文件中声明的字段顺序决定),从用户体验的角度来看,这似乎很愚蠢。要解决此问题,我将用户名字段重新标记为“电子邮件地址”,而是隐藏了电子邮件地址字段。然后,在表单提交时默认使用用户名发送电子邮件。

(要注意的一点:与David的回答也考虑其他形式相比,出于我的目的,我已将该插件限制为只能在“ com_users.registration”表单上运行。)

class PlgUserSimpleRegistration extends JPlugin
{

function onContentPrepareForm($form, $data)
{
    if (!($form instanceof JForm))
    {
        $this->_subject->setError('JERROR_NOT_A_FORM');
        return false;
    }

    // Check we are manipulating the registration form
    if ($form->getName() != 'com_users.registration')
    {
        return true;
    }

    // Check whether this is frontend or admin
    if (JFactory::getApplication()->isAdmin()) {
        return true;
    }

    // Remove/Hide fields on frontend
    // Note: since the onContentPrepareForm event gets fired also on
    // submission of the registration form, we need to hide rather than
    // remove the mandatory fields. Otherwise, subsequent filtering of the data
    // from within JModelForm.validate() will result in the required fields
    // being stripped from the user data prior to attempting to save the user model,
    // which will trip an error from inside the user object itself on save!
    $form->removeField('password2');
    $form->removeField('email2');

    $form->setFieldAttribute('name', 'type', 'hidden');
    $form->setValue('name', null, 'placeholder');
    $form->setFieldAttribute('email1', 'type', 'hidden');
    $form->setValue('email1', null, JUserHelper::genRandomPassword(10) . '@invalid.nowhere');

    // Re-label the username field to 'Email Address' (the Email field
    // ordinarily appears below the password field on the default Joomla
    // registration form)
    $form->setFieldAttribute('username', 'label', 'COM_USERS_REGISTER_EMAIL1_LABEL');

    return true;
}

function onUserBeforeDataValidation($form, &$user) {
    if ($form->getName() != 'com_users.registration') {
        return true;
    }

    if (!$user['username']) {
        // Keep up the pretense from above!
        $form->setFieldAttribute('username', 'label', 'COM_USERS_REGISTER_EMAIL1_LABEL');
        return true;
    }

    if (!$user['name'] or $user['name'] === 'placeholder') {
        $user['name'] = $user['username'];
        $user['email1'] = $user['email2'] = $user['username'];
        $user['password2'] = $user['password1'];
    }
}

}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.