Add new field to registration form

hi dears
i want to add some field to registration form
i add each of my need to this address lib/pkp/templates/frontend/components/registrationForm.tpl

for example

	<div class="Sex">
		<label>
			<span class="label">
				{translate key="user.sex"}
				<span class="required">*</span>
				<span class="pkp_screen_reader">
					{translate key="common.required"}
				</span>
			</span>
		
			<select name="Sex" id="Sex" required>
			    <option value="0">{translate key="user.sex.male"}</option>
			    <option value="1">{translate key="user.sex.female"}</option>
			    
			</select>
			
			
		</label>
	</div>

and add this code to lib/pkp/classes/user/form/RegistrationForm.inc.php

	function execute($request) {
	$requireValidation = Config::getVar('email', 'require_validation');
	$userDao = DAORegistry::getDAO('UserDAO');

	// New user
	$user = $userDao->newDataObject();

	$user->setUsername($this->getData('username'));

	// Set the base user fields (name, etc.)
	$user->setFirstName($this->getData('firstName'));
	$user->setMiddleName($this->getData('middleName'));
	$user->setLastName($this->getData('lastName'));
	$user->setInitials($this->getData('initials'));
	$user->setEmail($this->getData('email'));
	$user->setCountry($this->getData('country'));
	$user->setCity($this->getData('City'));
	$user->setAddress($this->getData('Address'));
	$user->setSex($this->getData('Sex'));
	$user->setNationalID($this->getData('NationalID'));
	$user->setPhone($this->getData('Phone'));
	$user->setMobile($this->getData('Mobile'));
	$user->setAffiliation($this->getData('affiliation'), null); // Localized

	$user->setDateRegistered(Core::getCurrentDate());
	$user->setInlineHelp(1); // default new users to having inline help visible.

	if (isset($this->defaultAuth)) {
		$user->setPassword($this->getData('password'));
		// FIXME Check result and handle failures
		$this->defaultAuth->doCreateUser($user);
		$user->setAuthId($this->defaultAuth->authId);
	}
	$user->setPassword(Validation::encryptCredentials($this->getData('username'), $this->getData('password')));

	if ($requireValidation) {
		// The account should be created in a disabled
		// state.
		$user->setDisabled(true);
		$user->setDisabledReason(__('user.login.accountNotValidated', array('email' => $this->getData('email'))));
	}

	parent::execute($user);

	$userDao->insertObject($user);
	$userId = $user->getId();
	if (!$userId) {
		return false;
	}

	// Associate the new user with the existing session
	$sessionManager = SessionManager::getManager();
	$session = $sessionManager->getUserSession();
	$session->setSessionVar('username', $user->getUsername());

	// Save the roles
	import('lib.pkp.classes.user.form.UserFormHelper');
	$userFormHelper = new UserFormHelper();
	$userFormHelper->saveRoleContent($this, $user);

	// Insert the user interests
	import('lib.pkp.classes.user.InterestManager');
	$interestManager = new InterestManager();
	$interestManager->setInterestsForUser($user, $this->getData('interests'));

	import('lib.pkp.classes.mail.MailTemplate');
	if ($requireValidation) {
		// Create an access key
		import('lib.pkp.classes.security.AccessKeyManager');
		$accessKeyManager = new AccessKeyManager();
		$accessKey = $accessKeyManager->createKey('RegisterContext', $user->getId(), null, Config::getVar('email', 'validation_timeout'));

		// Send email validation request to user
		$mail = new MailTemplate('USER_VALIDATE');
		$this->_setMailFrom($request, $mail);
		$context = $request->getContext();
		$mail->assignParams(array(
			'userFullName' => $user->getFullName(),
			'activateUrl' => $request->url($context->getPath(), 'user', 'activateUser', array($this->getData('username'), $accessKey))
		));
		$mail->addRecipient($user->getEmail(), $user->getFullName());
		$mail->send();
		unset($mail);
	}
	return $userId;
}

but when i clicked on submit button , see white page

in additional i add these property to db , on user table
can you help me?

@asmecher

Hi @amirhosein

I think you do not need to add that property to the DB table users - it could be saved in the DB table user_settings. For that you would need to add it also in the UsersDAO function getAdditionalFieldNames (https://github.com/pkp/pkp-lib/blob/master/classes/user/PKPUserDAO.inc.php#L464). In that case you would set the user metadata in the RegistrationForm something like this: $user->setData('sex', $this->getData('Sex'));
Else, if you want to use the new DB column, you would also need to add the getSex and setSex to the classes/users/Users.inc.php bzw. PKPUser.inc.php class and also consider it everywhere in the UsersDAO.inc.php i.e. PKPUsersDAO.inc.php. Thus the solution above needs less code change.

When you see a white page, it means that an error occurred. You can then take a look in the apache/server error log files to see what error occurred.

Best,
Bozana

thank you so much
i added data on my custom field

Does this process means, that, in general, if we want to add feature to PKP (OMP), we must to change the core code of PKP?

Or we must to develop plugin?

Or we must to extend the core classes in custom plugin?

How to use “the PKP way” to develop that routines?

ihave add the code in registrationform

  <div class="phone">
  	<label>
  		<span class="label">
  			{translate key="user.phone"}
  			<span class="required">*</span>
  			<span class="pkp_screen_reader">
  				{translate key="common.required"}
  			</span>
  		</span>
  		<input type="text" name="phone" id="phone" value="{$phone|escape}" maxlength="40" required>
  	</label>
  </div>

but the phone dosnt show at the user profile (not save to database)
Thanks

Dear @acahya

Did you check the added field on database or on the user profile?

i had checked both in the database or in the user profile is nothing saved.

i check in the database and user profile too, but nothing is saved there.

Hi @acahya

I had the same problem. There are 3 things to “edit”:

1 - Create the code in the registration form. [you already did]
2 - Add the string ‘phone’ (file: lib/pkp/classes/users/form/RegistrationForm.inc.php) in the array at the line 130(more os less)
3 - With the same file. Add $user->setPhone($this->getData(‘phone’)); Just after the $user = $userDao->newDataObject(); At line 170 (more os less)

Hope it helps!

It can be done from the theme plugin also. Particularly, the theme registration form template can be overridden by simply putting by corresponded path inside plugin’s template folder. RegistrationForm class has hooks in it that allows to add code inside costructor or methods; hooks are inherited from Form class (they can be checked there)

Thanks @rodrigo_snape
its work :smiley:

I am using 2.4.8.1. I did that .But I unable to show this field on registration page.please let me know.

I am using 2.4.8.1. I did that .But I unable to show this field on registration page.please let me know.

Do you have any ideas, is there any list of hook documentation that can be called ?
I checked on this page :

It turns out that this page is not relevant anymore.
Thank

Hi @navotera,

I don’t know if there is a list of hooks somewhere.
If talking about forms, hooks are called in methods of the parent class Form, e.g.: pkp-lib/Form.inc.php at e8ec9824d20f4b712cc25ccf35d5ddad365c8447 · pkp/pkp-lib · GitHub

Are you looking for something specific?

Thank you so much @Vitaliy

I just curious how you usually you can get what things that can be hooked to OJS system so I can use it to create any new improvement to my theme.