Search code examples
javaspringspring-mvcspring-form

binding single sf:input spring form tag with multiple bean attribute


i want to make a form using spring tags such that user can login with his email or mobile number or user name and password.

<sf:form method="POST" modelAttribute="loginBean">
    <label for="user_email">User Name/Email/Mobile:</label>
    <sf:input path="email,mobile,username" value="${user.email}" />

    <label for="user_password">Password:</label>
    <sf:input path="password" />

    <input type="submit" value="submit" />
</sf:form>

LoginBean

public class LoginBean {
private String email;
private String password;
private String mobile;
private String username;
//getter setter 
}

I am stuck here to bind a single input tag with multiple bean attribute.

Please help me out.


Solution

  • There could be 2 approached:

    1. Use single String login field in the LoginBean and one form field with login path. On the server side you should somehow recognize what is entered and do 3 attempts to login depending on the recognized way.

    -

    tryLoginWithEmail(loginBeanInstance.getLogin(), loginBeanInstance.getPassoword()); 
    tryLoginWithMobile(loginBeanInstance.getLogin(), loginBeanInstance.getPassoword());
    tryLoginWithUsername(loginBeanInstance.getLogin(), loginBeanInstance.getPassoword());
    
    1. Create a 3 form fields and add some logic to allow only one field to be filled. The login according to the not empty field.

    UPDATE:

    1. Create one method tryComplexLogin(login, password) where you can try to find user with the password and login checking all the 3 field values in DB

    like

    select * from users
    where
      (name =:loginNameParam and password=:passwordParam)
    OR
      (email =:loginNameParam and password=:passwordParam)
    OR
      (mobile =:loginNameParam and password=:passwordParam)