A.Wen
A.Wen

Reputation: 223

Add additional fields in registration using Jhipster 4.10.2

I need to add phone number to the registration page and need to save it in the db as well. I followed following link.

http://www.jhipster.tech/tips/022_tip_registering_user_with_additional_information.html

But since here Jhispter version is changed code is bit different than the code in above link. So I am bit confusing to go with it. According to the link instructions I did upto "Updating ManagedUserVM". Then after I need the help since code is differed.

Upvotes: 0

Views: 151

Answers (1)

Paul-Etienne
Paul-Etienne

Reputation: 1018

It really didn't change that much, and the logic remains the same.

The registerAccount function should look like this now :

public void registerAccount(@Valid @RequestBody ManagedUserVM managedUserVM) {
    if (!checkPasswordLength(managedUserVM.getPassword())) {
        throw new InvalidPasswordException();
    }
    userRepository.findOneByLogin(managedUserVM.getLogin().toLowerCase()).ifPresent(u -> {throw new LoginAlreadyUsedException();});
    userRepository.findOneByEmailIgnoreCase(managedUserVM.getEmail()).ifPresent(u -> {throw new EmailAlreadyUsedException();});
    User user = userService.registerUser(managedUserVM, managedUserVM.getPassword(), managedUserVM.getPhone());
    mailService.sendActivationEmail(user);
}

And the registerUser function in the UserService (which is a rename of the former createUser) :

public User registerUser(UserDTO userDTO, String password, String phone) {
    // JHipster code omitted for brevity
    ...

    // Create and save the UserExtra entity
    UserExtra newUserExtra = new UserExtra();
    newUserExtra.setUser(newUser);
    newUserExtra.setPhone(phone);
    userExtraRepository.save(newUserExtra);
    log.debug("Created Information for UserExtra: {}", newUserExtra);

    return newUser;
}

Just note that you may have to manually change your database changelog (if using a SQL database) to correctly link the ids of User and UserExtra, so it looks like this :

<createTable tableName="user_extra">
        <column name="phone" type="varchar(255)">
            <constraints nullable="true" />
        </column>
        <column name="user_id" type="bigint">
            <constraints primaryKey="true" nullable="false" />
        </column>
        <!-- jhipster-needle-liquibase-add-column - JHipster will add columns here, do not remove-->
    </createTable>

Upvotes: 1

Related Questions