1

如何使用我自己的验证在注册表单中添加额外的字段。我想从安全社交代码重写所有视图、控制器吗?

我尝试按如下方式添加我的自定义登录表单。这样做是否正确?

views.custom.SecuFocusTemplatesPlugin

val contactForm = Form(
      mapping(      
        "firstname" -> nonEmptyText(minLength=2, maxLength=64),
        "lastname" -> nonEmptyText(minLength=2, maxLength=64),
        "jobtitle" -> nonEmptyText(minLength=2, maxLength=64),
        "phone" -> nonEmptyText(minLength=2, maxLength=20),      
        "email" -> (email verifying nonEmpty),

        "password" -> tuple(
            "main" -> nonEmptyText(minLength = 6),
            "confirm" -> nonEmptyText(minLength = 6)
        ).verifying(
            // Add an additional constraint: both passwords must match
            "Passwords don't match", ps => ps._1 == ps._2
          ).transform[String]({case (p, _) => p}, {p => p -> p}),

        "companyname" -> nonEmptyText(minLength=2, maxLength=64),
        "employeescount" -> number
      )

      (Contact.apply)(Contact.unapply)
  )

 /**
   * Returns the html for the login page
   * @param request
   * @tparam A
   * @return
   */
  override def getLoginPage[A](implicit request: Request[A], form: Form[(String, String)],
                               msg: Option[String] = None): Html =
  {
    views.html.login(contactForm, msg)
    //securesocial.views.html.login(form, msg)
  }

login.scala.html

@(loginForm: Form[Contact], errorMsg: Option[String] = None)(implicit request: RequestHeader)


@import helper._
@import securesocial.core.Registry
@import securesocial.core.AuthenticationMethod._
@import securesocial.core.providers.UsernamePasswordProvider.UsernamePassword
@import securesocial.views._



@main(Messages("securesocial.login.title")) {
    <div class="page-header">
        <h1>@Messages("securesocial.login.title")</h1>
    </div>

    @errorMsg.map { msg =>
        <div class="alert alert-error">
            @Messages(msg)
        </div>
    }

    @request.flash.get("success").map { msg =>
        <div class="alert alert-info">
            @msg
        </div>
    }

    @request.flash.get("error").map { msg =>
        <div class="alert alert-error">
            @msg
        </div>
    }


@defining( Registry.providers.all.values.filter( _.id != UsernamePassword) ) { externalProviders =>

        @if( externalProviders.size > 0 ) {
            <div class="clearfix">
                <p>@Messages("securesocial.login.instructions")</p>
                <p>
                    @for(p <- externalProviders) {
                        @provider(p.id)
                    }
                </p>
            </div>
        }

        @Registry.providers.get(UsernamePassword).map { up =>
            <div class="clearfix">
                @if( externalProviders.size > 0 ) {
                    <p>@Messages("securesocial.login.useEmailAndPassword")</p>
                } else {
                    <p>@Messages("securesocial.login.useEmailAndPasswordOnly")</p>
                }

               @provider("userpass", Some(loginForm))
            </div>
        }
    }
}
4

1 回答 1

2

截至撰写本文时,SecureSocial 不允许您指定用于注册的自定义字段。您有 2 个选项:

  1. 您现在可以做的是当用户登录时(成功注册后)将他重定向到他可以完成所需信息的页面。我知道这在所有情况下可能都不理想,但这是最简单的方法。

  2. 编写您自己的注册控制器来处理您的自定义页面流。您必须查看内置的注册控制器并将其用作指南。

于 2013-10-25T21:15:56.023 回答