Question

I've made an extra custom field called esrnumber. I've added a onUserBeforeSave()function to the php file within the plugin. This is supposed to take the esrnumber from my custom field as well as the name from the registration form, check a database, and return true if they match. Thus allowing the user to register.

The problem is that i can't seem to get the field values from the form into this php script. Below is my code.

function onUserBeforeSave($user, $isnew, $new){
//sql code removed for example
    $foundesr = false;
    if ($_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET['esrnumber'])) {
    $test = $_GET['esrnumber'];
    } else if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['esrnumber']))         {
        $test = $_POST['esrnumber'];
    } else {
        $test = "booo";
    }
   if($foundesr == false){
        JError::raiseWarning(1000, JText::_('There is a problem with your ESR number, ' . JRequest::getVar('username') . JRequest::getVar('esrnumber') . JRequest::getVar('jform_username') . $test . 'it does not match that name in our records.'));
        return false;
    }
    return true;
}

As you can see most of that is unnecessary, I've just tried several different methods to retrieve the value from the registration form fields, everyone of them returns empty. Where am i going wrong? How can i simply get the values from my form into this method?

Here's the HTML code Joomla generates for the form (asfaik this is all default stuff)

<div class="registration">
<form id="member-registration" action="/index.php/component/users/?task=registration.register" method="post" class="form-validate">
            <fieldset>
                <legend>User Registration</legend>
                <dl>
                                <dt>
                <span class="spacer"><span class="before"></span><span class="text"><label id="jform_spacer-lbl" class=""><strong class="red">*</strong> Required field</label></span><span class="after"></span></span>                                    </dt>
            <dd>&#160;</dd>
                                            <dt>
                <label id="jform_name-lbl" for="jform_name" class="hasTip required" title="Name::Enter your full name">Name:<span class="star">&#160;*</span></label>                                   </dt>
            <dd><input type="text" name="jform[name]" id="jform_name" value="pirateballoon@gmail.com" class="required" size="30"/></dd>
                                            <dt>
                <label id="jform_username-lbl" for="jform_username" class="hasTip required" title="Username::Enter your desired user name">Username:<span class="star">&#160;*</span></label>                                   </dt>
            <dd><input type="text" name="jform[username]" id="jform_username" value="pirateballoon@gmail.com" class="validate-username required" size="30"/></dd>
                                            <dt>
                <label id="jform_password1-lbl" for="jform_password1" class="hasTip required" title="Password::Enter your desired password - Enter a minimum of 4 characters">Password:<span class="star">&#160;*</span></label>                                    </dt>
            <dd><input type="password" name="jform[password1]" id="jform_password1" value="" autocomplete="off" class="validate-password required" size="30"/></dd>
                                            <dt>
                <label id="jform_password2-lbl" for="jform_password2" class="hasTip required" title="Confirm Password::Confirm your password">Confirm Password:<span class="star">&#160;*</span></label>                                    </dt>
            <dd><input type="password" name="jform[password2]" id="jform_password2" value="" autocomplete="off" class="validate-password required" size="30"/></dd>
                                            <dt>
                <label id="jform_email1-lbl" for="jform_email1" class="hasTip required" title="Email Address::Enter your email address">Email Address:<span class="star">&#160;*</span></label>                                 </dt>
            <dd><input type="text" name="jform[email1]" class="validate-email required" id="jform_email1" value="pirateballoon@gmail.com" size="30"/></dd>
                                            <dt>
                <label id="jform_email2-lbl" for="jform_email2" class="hasTip required" title="Confirm email Address::Confirm your email address">Confirm email Address:<span class="star">&#160;*</span></label>                                   </dt>
            <dd><input type="text" name="jform[email2]" class="validate-email required" id="jform_email2" value="pirateballoon@gmail.com" size="30"/></dd>
                                                                            </dl>
    </fieldset>
                <fieldset>
                <legend>User ESR Profile</legend>
                <dl>
                                <dt>
                <label id="jform_esrprofile_esrnumber-lbl" for="jform_esrprofile_esrnumber" class=" required">ESR Number<span class="star">&#160;*</span></label>                                   </dt>
            <dd><input type="text" name="jform[esrprofile][esrnumber]" id="jform_esrprofile_esrnumber" value="" class="required"/></dd>
                            </dl>
    </fieldset>
        <div>
        <button type="submit" class="validate">Register</button>
        or          <a href="/" title="Cancel">Cancel</a>
        <input type="hidden" name="option" value="com_users" />
        <input type="hidden" name="task" value="registration.register" />
        <input type="hidden" name="b44564159b9c7ebe3b7caf93cc5ce8de" value="1" />       </div>
</form>

I personally never called that new method i made, i just created the method and Joomla seems to automatically call it upon submitting the form.

Cheers.

Was it helpful?

Solution 3

SOLVED. If anybody finds this thread and needs to know, this is how i did it in the onUserBeforeSave($user, $isnew, $new) function.

JArrayHelper::getValue($new, 'name', 0, 'STRING')

Gives you your variable. Just change where it says name to get whichever user details you want. To get the ESR number (my custom field) i had to do this:

        foreach ($new['esrprofile'] as $k => $v) {
             $esrnumber = $v;
        }

There's probably a more efficient way of getting that esr number out of that array seeing as how i think there's only one instance of the number in there, but this works for now.

OTHER TIPS

$jinput = JFactory::getApplication()->input;
$fields = $jinput->get('jform','','array');

First of all JRequest is deprecated and should not be used in 2.5 and it is completely removed in the new Joomla 3.0. Instead use JInput like so:

$jinput = JFactory::getApplication()->input;
$fields = $jinput->getArray('jform');

Now you should be able to reference yout fields like so:

$fields['name'];
$fields['username'];
etc.

Instead of using JRequest to retrieve the input you can use the $new variable instead:

echo $new->username;
echo $new->email;

However, the reason JRequest::getVar is not working is simply because you are using the wrong names. It seems like you are trying to use the IDs of the input fields and not the name.

The input names is set to jform[name] etc. So to retrieve them using JRequest get the whole array:

$jform = JRequest::getVar('jform');
$username = $jform['username'];

Also, if you are using a Joomla version > 2.5 see JInput: http://docs.joomla.org/Retrieving_request_data_using_JInput

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top