16

First, I am aware of this question:

https://stackoverflow.com/questions/14799733/remove-name-field-from-joomla-registration-form

But the link in it's answer is dead, and the file path mentioned doesn't exist in the latest Joomla (version 3.3), so the answer is of no use whatsoever.

What I want is simple: The registration form for Joomla looks like this:

registration image default

I want to ensure the registration process is as simple as possible for my users so they actually complete it, and there's some problems:

Surely this is a far better registration page:

better registration image

I modified the file \components\com_users\models\forms\registration.xml to make the form look like what I want, and it worked! But whenever I submit the form, the validation still looks for the missing fields, so it informs me that I need to supply a name:

"Registration failed: Please enter your name."

How can I disable the checks for the name field, as well as the two confirm fields?

Since a name is required, I'd like to set the name to the provided username (so username and name are the same by default). If the user actually wants to go set a real name in their profile page after registration, they should be able to, but it shouldn't be required.

Am I going about this the wrong way? I've read in some places that modifying core Joomla files is not good, as it could break in an update, and that "overrides" should be used instead. Usually these responses simply link to Joomla's page on overrides, and provide no farther explanation about how to use them to do what the asker wants. I've tried to figure them out, but can't, so if overrides are the way to go, please explain how I would go about doing this with them, rather than just linking to the override tutorial.

I'd greatly appreciate any help you can provide, I've been trying to figure this out for a few hours now!

  • 1
    I would like to point out JED as a possible source of extensions doing something like this. – Valentin Despa Feb 1 '15 at 18:32
  • @Lodder thanks for the info you have inserted to skip the validation process, but I'm afraid that it is not working in my template. Would you please check the code and update with working one? As I followed up the process of @Rickster and that worked like charm. I'm novice in joomla and therefore, requesting your assistance in this regard. Thanks in advance. – user3857 Mar 11 '15 at 11:22
  • I found the answer to this Stackoverflow question might be what you want: How to disable the validation of some fields in Joomla 3 registration – Rob L Jul 1 '15 at 5:06
12

Ideally, this will all be done in a user plugin. I'm not sure that a lot of people realize this, but you can edit the form from a plugin (no core hack required).

Likewise, the plugin will see the input before the model attempts to save the entry, so you can mock some of the field duplication fields to force it to pass the validation.

This fuction in a "users" plugin allows you to alter the form:

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;
}

Then you can use an onUserBeforeSave function to reset any of the fields that you need to reset to pass validation. (This may not be totally necessary, so try without and see what errors you get.)

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

Ok so I was thinking about an approach without having to core hack anything or install any 3rd party extensions. My method will involve a Template Override which you will need to make for the com_users component, specifically the Registration view.

Now for the fields (assuming your template override is all setup), we won't be removing them, but instead hiding them as the controller and modal require data from them. So open the following file that is now your template override:

templates/YOUR_TEMPLATE/html/com_users/registration/default.php

and add the following code near the top of the file just after 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);

I've added in some comments so you know what each snippet of code is doing.

Hope this helps :)

1

@DavidFritsch's answer was very helpful. A couple of problems I found though were:

A) You can't completely remove certain mandatory fields as it causes problems with data filtering on form submission (see comments in the code below). To combat this, hide rather than remove the fields from the form object. B) The onUserBeforeSave event is not fired until it is too late to prevent the registration validation logic from rejecting the form submission. Instead, use the onUserBeforeDataValidation event.

In my particular case, all I wanted was email address and password. Joomla threw in a complication here though, since email address was appearing after password (dictated by the order of the fields declared in the registration.xml file), which would look daft from a user experience perspective. To work around that, I re-labelled the username field to 'Email Address' and instead hid the email address field. Email is then defaulted from the username on form submission.

(Minor note: I've restricted the plugin to only operate on the 'com_users.registration' form for my purposes, as compared to David's answer which considers other forms also.)

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'];
    }
}

}

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.