Is it possible to inject into an scala object with scaldi dependecy injection? If so, how can I get the injector?
I have an object SignUpForm which has to be a singleton...
object SignUpForm {
val form = Form(
mapping(
"firstName" -> nonEmptyText,
"lastName" -> nonEmptyText,
"email" -> email.verifying(Constraints.userExists),
"password" -> mapping (
"main" -> nonEmptyText.verifying(Constraints.passwordLenth),
"confirm" -> nonEmptyText
)(Password.apply)(Password.unapply).verifying(Constraints.passwordConfirmation)
)(Data.apply)(Data.unapply)
)
case class Password(
main: String,
confirm: String
)
case class Data(
firstName: String,
lastName: String,
email: String,
password: Password
)
}
... and an object constraints
object Constraints {
val userService = inject[UserService]
def passwordConfirmation: Constraint[Password] = Constraint("password.confirm"){ password =>
if (password.main.equals(password.confirm)) Valid else Invalid(Seq(ValidationError("password doesnt equal the confirmation password", "password.main", "confirm")))
}
def passwordLenth: Constraint[String] = Constraint("password.length"){ password =>
if (password.length >= 8) Valid else Invalid(Seq(ValidationError("The minimum password length is " + 8 + " characters", "password.main", "length")))
}
def userExists: Constraint[String] = Constraint("user.exists"){ email =>
if (userExistsWithEmail(email.toLowerCase)) Valid else Invalid(Seq(ValidationError("The user with email " + email + " already exists", "user", "email")))
} ...
here is the problem -> userservice needs to be injected but it cant be injected as long as there is no implicit Injector passed as constructor argument (what is not possible because of the fact that we have an object here)
...
private def userExistsWithEmail(email: String):Boolean = {
val result = Await.result(userService.retrieve(LoginInfo(CredentialsProvider.Credentials, email)),1 seconds)
result match {
case Some(u) => true
case None => false
}
}
}
... how can I solve this problem?
Thanks in advance