Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable the validation of some fields in Joomla 3 registration

I'm building the website based on Joomla 3. I need to disable the validation of the fields: name, username, password1 (the second, because the first is password2) and email2 and use email1 as username (I installed the plugin Email for user authorization). I have tryed to remove these fields in file components/com_users/models/forms/registration.xml but the validation is still remaining. If I don't remove them but only change the rows required="true" to false for these fields the registration doesn't work at all and any user stored in the DB. How can I disable these fields?

like image 889
stckvrw Avatar asked Jan 21 '14 20:01

stckvrw


1 Answers

It's not an easy workaround, and you will need some basic knowledge of Joomla and PHP, but I'll try to explain it to you as simple as i can.

>>> Creating view template override

First of all you will need to create your Registration view template override (to keep it Joomla update proof). To do so, create folder /templates/YOUT_TEMPLATE/html/com_users/registration and copy /components/com_users/views/registration/tmpl/default.php file there.

From now on you can modify registration output in your template folder.

>>> Modifying registration form output

By default Joomla takes all fields from form file /components/com_users/models/forms/registration.xml, where they are defined, and outputs them in a view. But if we don't want to use ALL the fields, we need to output fields manually. My example code only output E-mail and Password fields for registration. Here's a sample code to do so: (default.php file)

<?php
defined('_JEXEC') or die;
JHtml::_('behavior.keepalive');
?>
<div class="grid_8" id="register_block">
<div class="content_block">
<h1>Registracija</h1>
<div class="login<?php echo $this->pageclass_sfx?>">
<form id="member-registration" action="<?php echo JRoute::_('index.php?option=com_users&task=registration2.register'); ?>" method="post" enctype="multipart/form-data">
<div>
<div class="login-fields">
<label id="jform_email1-lbl" for="jform_email1">E-mail:</label>
<input type="text" name="jform[email1]" id="jform_email1" value="" size="30">
</div>
<div class="login-fields">
<label id="jform_password1-lbl" for="jform_password1">Password:</label>
<input type="password" name="jform[password1]" id="jform_password1" value="" autocomplete="off" size="30">
</div>
<button type="submit" class="button"><?php echo JText::_('JREGISTER');?></button>
<input type="hidden" name="option" value="com_users" />
<input type="hidden" name="task" value="registration2.register" />
<?php echo JHtml::_('form.token');?>
</div>
</form>
</div>
</div>
</div>

Please note, that I've also replaced task value from registration.register to registration2.register, I did this to bypass some of validation rules using my own controller.

>>> Creating controller override

Locate file /components/com_users/controllers/registration.php and create a copy of it called registration2.php in same folder.

Open file registration2.php and change It's class name from UsersControllerRegistration to UsersControllerRegistration2

From now on Joomla registration form will use this class to create a new user.

Find a method called register and find this line:

$requestData = JRequest::getVar('jform', array(), 'post', 'array');

This is where Joomla get's registration form data. Add the following lines:

$requestData['name'] = $requestData['email1'];
$requestData['username'] = $requestData['email1'];
$requestData['email2'] = $requestData['email1'];
$requestData['password2'] = $requestData['password1'];

It will add missing registration info, and help you pass validation.

NOTE: This is a sample code, to show the main logic. If anyone has a better solution, I'd be more than happy to hear it.

like image 70
di3sel Avatar answered Nov 08 '22 12:11

di3sel