play.api.data.Form Scala Examples
The following examples show how to use play.api.data.Form.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: ResidencyChoiceDto.scala From pertax-frontend with Apache License 2.0 | 7 votes |
package models.dto import controllers.bindable._ import play.api.data.Form import play.api.data.Forms._ import play.api.data.validation.ValidationError import play.api.libs.json._ case class ResidencyChoiceDto(residencyChoice: AddrType) object ResidencyChoiceDto { implicit val formats = { implicit val addrTypeReads: Reads[AddrType] = new Reads[AddrType] { override def reads(json: JsValue): JsResult[AddrType] = json match { case JsString("sole") => JsSuccess(SoleAddrType) case JsString("primary") => JsSuccess(PrimaryAddrType) case JsString("postal") => JsSuccess(PostalAddrType) case _ => JsError(Seq(JsPath() -> Seq(JsonValidationError("error.expected.jsString(addrType)")))) } } implicit val addrTypeWrites: Writes[AddrType] = new Writes[AddrType] { override def writes(o: AddrType): JsValue = o match { case SoleAddrType => JsString("sole") case PrimaryAddrType => JsString("primary") case PostalAddrType => JsString("postal") } } Json.format[ResidencyChoiceDto] } val form = Form( mapping( "residencyChoice" -> optional(text) .verifying("error.you_must_select_an_answer", e => e.flatMap(a => AddrType(a)).isDefined) .transform[AddrType](x => AddrType(x.fold("")(_.toString)).getOrElse(SoleAddrType), ad => Some(ad.toString)) //getOrElse here will never fall back to default because of isDefined above )(ResidencyChoiceDto.apply)(ResidencyChoiceDto.unapply) ) }
Example 2
Source File: ExtendedController.scala From bay-scalajs.g8 with Apache License 2.0 | 5 votes |
package controllers import cats.data.EitherT import play.api.data.Form import play.api.mvc.Controller import play.api.mvc.Request import play.api.mvc.Result import shared.utils.Codecs import shared.utils.Implicits import cats.syntax.either._ import play.api.i18n.MessagesApi import services.Services import scala.concurrent.ExecutionContext import scala.concurrent.Future trait ExtendedController extends Controller with Implicits with Codecs { val services: Services implicit val ec: ExecutionContext type HttpResult[A] = EitherT[Future, Result, A] // Constructors for our result type object HttpResult { def point[A](a: A): HttpResult[A] = EitherT[Future, Result, A](Future.successful(Right(a))) def fromFuture[A](fa: Future[A]): HttpResult[A] = EitherT[Future, Result, A](fa.map(Right(_))) def fromEither[A](va: Either[Result, A]): HttpResult[A] = EitherT[Future, Result, A](Future.successful(va)) def fromEither[A, B](failure: B => Result)(va: Either[B, A]): HttpResult[A] = EitherT[Future, Result, A](Future.successful(va.leftMap(failure))) def fromOption[A](failure: Result)(oa: Option[A]): HttpResult[A] = EitherT[Future, Result, A](Future.successful(oa.toRight(failure))) def fromFOption[A](failure: Result)(foa: Future[Option[A]]): HttpResult[A] = EitherT[Future, Result, A](foa.map(_.toRight(failure))) def fromFEither[A, B](failure: B => Result)(fva: Future[Either[B, A]]): HttpResult[A] = EitherT[Future, Result, A](fva.map(_.leftMap(failure))) def fromForm[FormType](failure: Form[FormType] => Result)(form: Form[FormType])( implicit request: Request[_]): HttpResult[FormType] = EitherT[Future, Result, FormType]( form.bindFromRequest.fold(errorForm => Left(failure(errorForm)).asFuture, formEntity => Right(formEntity).asFuture)) } def constructResult(result: HttpResult[Result]): Future[Result] = result.value.map(_.merge) implicit class ExtResult(e: Result) { def pureResult: HttpResult[Result] = EitherT[Future, Result, Result](Future.successful(Right(e))) } implicit class EnrichedOps[T](t: T) { def |>[R](f: T => R): R = f(t) } def messagesApi: MessagesApi = services.messagesApi }
Example 3
Source File: Authentication.scala From cave with MIT License | 5 votes |
package controllers import com.gilt.cavellc.errors.FailedRequest import play.api.Logger import play.api.data.Form import play.api.data.Forms._ import play.api.i18n.Messages import play.api.mvc._ import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future import scala.util.control.NonFatal class Authentication extends AbstractCaveController { val loginForm = Form(mapping("username" -> email, "password" -> text, "landingUrl" -> text) (UserLogin.apply)(UserLogin.unapply) ) def login = Action { implicit request => val loginFormWithLandingPage = loginForm.fill(UserLogin(EMPTY_STRING, EMPTY_STRING, request.flash.get("requestedUrl").getOrElse("/"))) Ok(views.html.loginscreen.signIn(loginFormWithLandingPage)) } def logout = Action.async { implicit request => Future.successful(Redirect(routes.Authentication.login()).withNewSession.flashing( "success" -> "You've been logged out" )) } def authenticate = Action.async { implicit request => withCaveClient { client => loginForm.bindFromRequest().fold( formWithErrors => Future.successful(BadRequest(views.html.loginscreen.signIn(formWithErrors))), loginForm => client.Users.postLogin(loginForm.email, loginForm.password) flatMap { case user: com.gilt.cavellc.models.Auth => withExplicitTokenClient(user.token) { signedClient => signedClient.Users.getInfo().flatMap { case Some(apiUser) => Logger.debug(s"Login Succeeded for ${loginForm.email}") val landingUrl: String = if (loginForm.landingUrl.nonEmpty) loginForm.landingUrl else routes.Application.index().url Logger.debug(s"landing url ${loginForm.landingUrl}") Future.successful(Redirect(landingUrl).withSession( "sessionToken" -> user.token, "userName" -> s"${apiUser.firstName} ${apiUser.lastName}" )) case _ => Logger.error(s"Unable to find user details for user: ${loginForm.email}") Future.successful(InternalServerError(views.html.errorpages.errorPage(Messages("cave.login.login.internalError")))) } } } recover { case error: FailedRequest if error.responseCode == BAD_REQUEST => Logger.debug(s"Incorrect username or password for user ${loginForm.email}", error) Redirect(routes.Authentication.login()).flashing("error" -> Messages("cave.login.login.incorrectUsernameOrPassword")) case NonFatal(e) => Logger.error(s"Unable to authenticate user: ${loginForm.email}", e) InternalServerError(views.html.errorpages.errorPage(Messages("cave.login.login.internalError"))) } ) } } } object Authentication extends Authentication case class UserLogin(email: String, password: String, landingUrl: String)
Example 4
Source File: Application.scala From bay-scalajs.g8 with Apache License 2.0 | 5 votes |
package controllers import cats.implicits._ import com.google.inject.Inject import jp.t2v.lab.play2.auth.LoginLogout import jp.t2v.lab.play2.auth.OptionalAuthElement import models.forms.LoginForm import play.api.data.Form import play.api.data.Forms._ import play.api.i18n.I18nSupport import play.api.mvc._ import services._ import shared.models.WiredApiModel.UnauthorizedApi import shared.services.WiredApi import upickle.default._ import scala.concurrent.ExecutionContext class Application @Inject()(val services: Services)(implicit val ec: ExecutionContext) extends ExtendedController with AuthConfigImpl with OptionalAuthElement with LoginLogout with I18nSupport { def index(path: String) = StackAction { implicit request => loggedIn match { case Some(_) => Ok(views.html.index()) case None => Ok(views.html.login(loginForm.fill(LoginForm("[email protected]", "testpw")))) } } val loginForm = Form( mapping( "email" -> nonEmptyText, "password" -> nonEmptyText )(LoginForm.apply)(LoginForm.unapply) ) def login: Action[AnyContent] = Action.async { implicit request => val result = for { form <- loginForm.bindFromRequest() |> HttpResult.fromForm(e => BadRequest(views.html.login(e))) userId <- services.userDao.maybeLogin(form) |> HttpResult.fromFOption( BadRequest(views.html.login(loginForm.fill(form).withGlobalError("bad.password")))) loginResult <- gotoLoginSucceeded(userId) |> HttpResult.fromFuture } yield loginResult constructResult(result) } def logout: Action[AnyContent] = Action.async { implicit request => gotoLogoutSucceeded } def api(s: String) = AsyncStack(parse.multipartFormData) { implicit request => val path = s.split("/") loggedIn match { case Some(user) => val dataStr = request.body.dataParts .get("data") .flatMap(_.headOption) .getOrElse("") AutowireRouter .route[WiredApi](new WiredApiService(user, services, request.body.files)) { val json = read[Map[String, String]](dataStr) autowire.Core.Request(path, json) } .map { responseData => Ok(responseData).as("application/json") } case None => Ok(write(Left(UnauthorizedApi))).as("application/json").asFuture } } }
Example 5
Source File: SnapshotCheckController.scala From DataQuality with GNU Lesser General Public License v3.0 | 5 votes |
package controllers.checks import javax.inject.Inject import com.codahale.jerkson.Json.generate import controllers.ControllerUtils._ import controllers.utils.MyDBSession import controllers.utils.ValidationConstraints._ import models.checks.Check.CheckType import models.checks.{Check, CheckParameter, SnapshotCheck} import models.meta.CheckMeta import models.metrics.Metric import models.targets.TargetToChecks import org.squeryl.PrimitiveTypeMode.inTransaction import play.api.Play.current import play.api.data.Form import play.api.data.Forms.{list, mapping, optional, text, tuple} import play.api.i18n.Messages.Implicits._ import play.api.mvc.{Action, Controller} import scala.util.Try class SnapshotCheckController @Inject()(session: MyDBSession) extends Controller { private def getMainForm(currId: Option[String] = None): Form[(Check, SnapshotCheck, List[CheckParameter])] = { def valFunc: (String) => Boolean = (t:String) => inTransaction(validateId(t, Check.getIdList(), currId)) def valName: (String) => Boolean = (t:String) => inTransaction(validateRefList(t, CheckMeta.getShortList(Some(CheckType.snapshot)))) def valMetric: (String) => Boolean = (t:String) => inTransaction(validateRefList(t, Metric.getIdList())) Form( mapping( "id" -> text.verifying(errorUsed, valFunc), "subtype" -> text.verifying(errorNotFound("Check type"), valName), "description" -> optional(text), "metric" -> text.verifying(errorNotFound("Metric"), valMetric), "parameters" -> list( tuple( "name" -> text, "value" -> text ) ) )(SnapshotCheck.applyWithCheck)(SnapshotCheck.unapplyWithCheck).verifying(validateSnapshotCheck) ) } def addSnapshotCheck() = Action { implicit request => val form = getMainForm().bindFromRequest form.value map { check => try { val json = inTransaction({ val met = check._1.insert() val colMet = check._2.insert() check._3.foreach(_.insert()) generate(SnapshotCheck.tupleToMap((met,colMet))) }) Created(json).as(JSON) } catch { case e: Exception => InternalServerError(e.toString) } } getOrElse BadRequest(form.errorsAsJson).as(JSON) } def getCheckDetails(id: String) = Action { inTransaction { Try{ val check: (Check, SnapshotCheck) = SnapshotCheck.getById(id).single generate(SnapshotCheck.tupleToMap(check)) }.toOption match { case Some(json) => Ok(json).as(JSON) case None => val json = generate(Map("error"->"Check not found!")) BadRequest(json).as(JSON) } } } def updateCheck(id: String) = Action { implicit request => val form = getMainForm(Some(id)).bindFromRequest form.value map { check => try { val json = inTransaction{ if(check._1.id == id) { CheckParameter.deleteByOwner(id) check._1.update() check._2.update() } else { val c = check._1.insert() check._2.insert() TargetToChecks.updateChecks(id, c.id) SnapshotCheck.deleteById(id) } check._3.foreach(_.insert()) generate(SnapshotCheck.tupleToMap((check._1,check._2))) } Ok(json).as(JSON) } catch { case e: Exception => InternalServerError(e.toString) } } getOrElse BadRequest(form.errorsAsJson).as(JSON) } }
Example 6
Source File: SqlCheckController.scala From DataQuality with GNU Lesser General Public License v3.0 | 5 votes |
package controllers.checks import javax.inject.Inject import com.codahale.jerkson.Json.generate import controllers.ControllerUtils._ import controllers.utils.MyDBSession import controllers.utils.ValidationConstraints._ import models.checks.Check.CheckType import models.checks.{Check, SqlCheck} import models.meta.CheckMeta import models.sources.Database import models.targets.TargetToChecks import org.squeryl.PrimitiveTypeMode.inTransaction import play.api.Play.current import play.api.data.Form import play.api.data.Forms.{mapping, optional, text} import play.api.i18n.Messages.Implicits._ import play.api.mvc.{Action, Controller} import scala.util.Try class SqlCheckController @Inject()(session: MyDBSession) extends Controller { private def getMainForm(currId: Option[String] = None): Form[(Check, SqlCheck)] = { def valFunc: (String) => Boolean = (t:String) => inTransaction(validateId(t, Check.getIdList(), currId)) def valDB: (String) => Boolean = (t:String) => inTransaction(validateRefList(t, Database.getIdList())) def valName: (String) => Boolean = (t:String) => inTransaction(validateRefList(t, CheckMeta.getShortList(Some(CheckType.sql)))) Form( mapping( "id" -> text.verifying(errorUsed, valFunc), "subtype" -> text.verifying(errorNotFound("Check type"), valName), "description" -> optional(text), "database" -> text.verifying(errorNotFound("Database"), valDB), "query" -> text )(SqlCheck.applyWithCheck)(SqlCheck.unapplyWithCheck) ) } def addSqlCheck() = Action { implicit request => val form = getMainForm().bindFromRequest form.value map { check => try { inTransaction({ val chk = check._1.insert() val sql = check._2.insert() val json = generate(SqlCheck.tupleToMap((chk,sql))) Created(json).as(JSON) }) } catch { case e: Exception => InternalServerError(e.toString) } } getOrElse BadRequest(form.errorsAsJson).as(JSON) } def getCheckDetails(id: String) = Action { inTransaction(Try{ val check: (Check, SqlCheck) = SqlCheck.getById(id) generate(SqlCheck.tupleToMap(check)) }.toOption match { case Some(json) => Ok(json).as(JSON) case None => val json = generate(Map("error"->"Check not found!")) BadRequest(json).as(JSON) }) } def updateCheck(id: String) = Action { implicit request => val form = getMainForm(Some(id)).bindFromRequest form.value map { check => try { val json = inTransaction{ if(check._1.id == id) { check._1.update() check._2.update() } else { val c = check._1.insert() check._2.insert() TargetToChecks.updateChecks(id, c.id) SqlCheck.deleteById(id) } generate(SqlCheck.tupleToMap((check._1,check._2))) } Ok(json).as(JSON) } catch { case e: Exception => InternalServerError(e.toString) } } getOrElse BadRequest(form.errorsAsJson).as(JSON) } }
Example 7
Source File: FileMetricController.scala From DataQuality with GNU Lesser General Public License v3.0 | 5 votes |
package controllers.metrics import javax.inject.Inject import com.codahale.jerkson.Json.generate import controllers.ControllerUtils._ import controllers.utils.MyDBSession import controllers.utils.ValidationConstraints._ import models.meta.MetricMeta import models.metrics.Metric.MetricType import models.metrics.{FileMetric, Metric} import models.sources.Source import org.squeryl.PrimitiveTypeMode.inTransaction import play.api.Play.current import play.api.data.Form import play.api.data.Forms.{mapping, text} import play.api.i18n.Messages.Implicits._ import play.api.mvc.{Action, Controller} import scala.util.Try class FileMetricController @Inject()(session: MyDBSession) extends Controller { private def getMainForm(currId: Option[String] = None): Form[(Metric, FileMetric)] = { def valFunc: (String) => Boolean = (t:String) => inTransaction(validateId(t, Metric.getIdList(), currId)) def valSrc: (String) => Boolean = (t:String) => inTransaction(validateRefList(t, Source.getIdList())) def valName: (String) => Boolean = (t:String) => inTransaction(validateRefList(t, MetricMeta.getShortList(Some(MetricType.file)))) Form( mapping( "id" -> text.verifying(errorUsed, valFunc), "name" -> text.verifying(errorNotFound("Metric name"), valName), "description" -> text, "source" -> text.verifying(errorNotFound("Source"), valSrc) )(FileMetric.applyWithMetric)(FileMetric.unapplyWithMetric).verifying(validateFileMetric) ) } def addFileMetric() = Action { implicit request => val form = getMainForm().bindFromRequest form.value map { metric => try { inTransaction({ val met = metric._1.insert() val fmet = metric._2.insert() val json = generate(FileMetric.tupleToMap((met,fmet))) Created(json).as(JSON) }) } catch { case e:Exception => InternalServerError(e.toString) } } getOrElse BadRequest(form.errorsAsJson).as(JSON) } def getMetricDetails(id: String) = Action { inTransaction( Try{ val metric: (Metric, FileMetric) = FileMetric.getById(id) generate(FileMetric.tupleToMap(metric)) }.toOption match { case Some(json) => Ok(json).as(JSON) case None => val json = generate(Map("error"->"Metric not found!")) BadRequest(json).as(JSON) }) } def updateMetric(id: String) = Action { implicit request => val form = getMainForm(Some(id)).bindFromRequest form.value map { metric => inTransaction(try { metric._1.id match { case `id` => metric._1.update() metric._2.update() case _ => metric._1.insert() metric._2.rebase(id) } val json = generate(FileMetric.tupleToMap(metric)) Ok(json).as(JSON) } catch { case e: Exception => InternalServerError(e.toString) }) } getOrElse BadRequest(form.errorsAsJson).as(JSON) } }
Example 8
Source File: ComposedMetricController.scala From DataQuality with GNU Lesser General Public License v3.0 | 5 votes |
package controllers.metrics import javax.inject.Inject import com.codahale.jerkson.Json.generate import controllers.ControllerUtils._ import controllers.utils.MyDBSession import controllers.utils.ValidationConstraints._ import models.metrics._ import org.squeryl.PrimitiveTypeMode.inTransaction import play.api.Play.current import play.api.data.Form import play.api.data.Forms.{mapping, text} import play.api.i18n.Messages.Implicits._ import play.api.mvc.{Action, Controller} import scala.util.Try class ComposedMetricController @Inject()(session: MyDBSession) extends Controller { private def getMainForm(currId: Option[String] = None): Form[(Metric, ComposedMetric)] = { def validateIdFunc: (String) => Boolean = (t:String) => inTransaction(validateId(t, Metric.getIdList(), currId)) def validateFormulaFunc: (String) => Boolean = (t:String) => inTransaction(ComposedMetricValidator.validateFormula(t, currId)) Form( mapping( "id" -> text.verifying(errorUsed, validateIdFunc), "name" -> text, "description" -> text, "formula" -> text.verifying(errorFormula, validateFormulaFunc) )(ComposedMetric.applyWithMetric)(ComposedMetric.unapplyWithMetric) ) } def addComposedMetric() = Action { implicit request => val form = getMainForm().bindFromRequest form.value map { metric => try { inTransaction({ val met = metric._1.insert() val fmet = metric._2.insert() val json = generate(ComposedMetric.tupleToMap((met,fmet))) Created(json).as(JSON) }) } catch { case e: Exception => InternalServerError(e.toString) } } getOrElse BadRequest(form.errorsAsJson).as(JSON) } def getMetricDetails(id: String) = Action { inTransaction( Try{ val metric: (Metric, ComposedMetric) = ComposedMetric.getById(id) generate(ComposedMetric.tupleToMap(metric)) }.toOption match { case Some(json) => Ok(json).as(JSON) case None => val json = generate(Map("error"->"Metric not found!")) BadRequest(json).as(JSON) }) } def updateMetric(id: String) = Action { implicit request => val form = getMainForm(Some(id)).bindFromRequest form.value map { metric => inTransaction(try { metric._1.id match { case `id` => metric._1.update() metric._2.update() case _ => metric._1.insert() metric._2.rebase(id) } val json = generate(ComposedMetric.tupleToMap(metric)) Ok(json).as(JSON) } catch { case e: Exception => InternalServerError(e.toString) }) } getOrElse BadRequest(form.errorsAsJson).as(JSON) } }
Example 9
Source File: TargetAttachmentsController.scala From DataQuality with GNU Lesser General Public License v3.0 | 5 votes |
package controllers.targets import javax.inject.Inject import com.codahale.jerkson.Json.generate import models.targets.{Mail, Target, TargetToChecks} import org.squeryl.PrimitiveTypeMode.inTransaction import play.api.Play.current import play.api.data.Form import play.api.data.Forms._ import play.api.i18n.Messages.Implicits._ import play.api.mvc.{Action, Controller} import scala.util.Try class TargetAttachmentsController @Inject()() extends Controller { private val mailForm = Form( "address" -> email ) def getTargetMailList(id: String) = Action { Try(inTransaction { val mails: Iterator[Mail] = Target.getDetailed(id).mails generate(mails) }).toOption match { case Some(json) => Ok(json).as(JSON) case None => BadRequest("There are no emails associated with this target!") } } def addTargetMail(id: String) = Action { implicit request => val form = mailForm.bindFromRequest form.value map { mail => inTransaction(Mail.apply(mail, id).insert) Created } getOrElse BadRequest(form.errorsAsJson).as(JSON) } def deleteTargetMail(id: String) = Action { implicit request => mailForm.bindFromRequest.value map (mail => { inTransaction( Mail.deleteById(mail, id) ) }) Ok } private val checkForm = Form( "checkId" -> text ) def getTargetCheckList(id: String) = Action { Try(inTransaction { val checks: Iterator[TargetToChecks] = Target.getDetailed(id).checks generate(checks) }).toOption match { case Some(json) => Ok(json).as(JSON) case None => BadRequest("There are no checks associated with this target!") } } def addTargetCheck(id: String) = Action { implicit request => checkForm.bindFromRequest.value map (check => inTransaction( TargetToChecks.apply(check, id).insert ) ) Created } def deleteTargetCheck(id: String) = Action { implicit request => checkForm.bindFromRequest.value map (check => { inTransaction( TargetToChecks.deleteById(check, id) ) }) Ok } }
Example 10
Source File: VirtualSourceController.scala From DataQuality with GNU Lesser General Public License v3.0 | 5 votes |
package controllers.sources import javax.inject.Inject import com.codahale.jerkson.Json.generate import controllers.ControllerUtils._ import controllers.utils.MyDBSession import controllers.utils.ValidationConstraints._ import models.sources._ import org.squeryl.PrimitiveTypeMode.inTransaction import play.api.Play.current import play.api.data.Form import play.api.data.Forms.{mapping, optional, seq, text} import play.api.i18n.Messages.Implicits._ import play.api.mvc.{Action, Controller} import scala.util.Try class VirtualSourceController @Inject()(session: MyDBSession) extends Controller { private def getVSRCForm(currId: Option[String] = None): Form[(Source, VirtualSource)] = { def valFunc: (String) => Boolean = (t:String) => inTransaction(validateId(t, Source.getIdList(), currId)) def valSrc: (String) => Boolean = (t:String) => inTransaction(validateRefList(t, Source.getIdList())) Form( mapping( "id" -> text.verifying(errorUsed, valFunc), "keyFields" -> seq(text), "tipo" -> text, "left" -> text.verifying(errorNotFound("Source"), valSrc), "right" -> optional(text), "query" -> text )(VirtualSource.applyWithSource)(VirtualSource.unapplyWithSource) ) } def addVSRC() = Action { implicit request => val form = getVSRCForm().bindFromRequest form.value map { file => try { inTransaction{ file._1.insert() file._2.insert() val json = generate(VirtualSource.fileToMap(file)) Ok(json).as(JSON) } } catch { case e: Exception => InternalServerError(e.toString) } } getOrElse BadRequest(form.errorsAsJson).as(JSON) } def getVSRCDetails(id: String) = Action { inTransaction{ Try{ val file: (Source, VirtualSource) = VirtualSource.getDetailed(id) generate(VirtualSource.fileToMap(file)) }.toOption match { case Some(json) => Ok(json).as(JSON) case None => val json = generate(Map("error" -> "Virtual source not found!")) BadRequest(json).as(JSON) }} } def updateVSRC(id: String) = Action { implicit request => val form = getVSRCForm(Some(id)).bindFromRequest form.value map { file => inTransaction(try { file._1.id match { case `id` => file._1.update() file._2.update() case _ => file._1.insert() file._2.rebase(id) } val json = generate(VirtualSource.fileToMap(file)) Ok(json).as(JSON) } catch { case e: Exception => InternalServerError(e.toString) }) } getOrElse BadRequest(form.errorsAsJson).as(JSON) } }
Example 11
Source File: FormData.scala From template-site with MIT License | 5 votes |
package models import play.api.data.{Mapping, Form} import play.api.data.Forms._ case class FormDataLogin(email: String, password: String) case class FormDataAccount(name:String, email: String, password: String, passwordAgain:String) object FormData { val login = Form( mapping( "email" -> email, "password" -> nonEmptyText )(FormDataLogin.apply)(FormDataLogin.unapply) ) val addMessage = Form( mapping( "content" -> nonEmptyText, "tags" -> text )(Message.formApply)(Message.formUnapply) ) private[this] def accountForm(passwordMapping:Mapping[String]) = Form( mapping( "name" -> nonEmptyText, "email" -> email, "password" -> passwordMapping, "passwordAgain" -> passwordMapping )(FormDataAccount.apply)(FormDataAccount.unapply) ) val updateAccount = accountForm(text) val addAccount = accountForm(nonEmptyText) }
Example 12
Source File: ProfileCtrl.scala From gospeak with Apache License 2.0 | 5 votes |
package gospeak.web.pages.user.profile import cats.effect.IO import com.mohiva.play.silhouette.api.Silhouette import gospeak.core.domain.User import gospeak.core.services.storage.{UserGroupRepo, UserProposalRepo, UserTalkRepo, UserUserRepo} import gospeak.web.AppConf import gospeak.web.auth.domain.CookieEnv import gospeak.web.domain.Breadcrumb import gospeak.web.pages.published.speakers.routes.{SpeakerCtrl => PublishedSpeakerRoutes} import gospeak.web.pages.user.UserCtrl import gospeak.web.pages.user.profile.ProfileCtrl._ import gospeak.web.utils.{GsForms, UICtrl, UserReq} import play.api.data.Form import play.api.mvc.{Action, AnyContent, ControllerComponents, Result} class ProfileCtrl(cc: ControllerComponents, silhouette: Silhouette[CookieEnv], conf: AppConf, groupRepo: UserGroupRepo, proposalRepo: UserProposalRepo, talkRepo: UserTalkRepo, userRepo: UserUserRepo) extends UICtrl(cc, silhouette, conf) { def edit(): Action[AnyContent] = UserAction { implicit req => editView(GsForms.user) } def doEdit(): Action[AnyContent] = UserAction { implicit req => GsForms.user.bindFromRequest.fold( formWithErrors => editView(formWithErrors), data => userRepo.edit(data) .map(_ => Redirect(PublishedSpeakerRoutes.detail(req.user.slug)).flashing("success" -> "Profile updated")) ) } private def editView(form: Form[User.Data])(implicit req: UserReq[AnyContent]): IO[Result] = { val filledForm = if (form.hasErrors) form else form.fill(req.user.data) IO(Ok(html.edit(filledForm)(editBreadcrumb))) } } object ProfileCtrl { def breadcrumb(implicit req: UserReq[AnyContent]): Breadcrumb = UserCtrl.breadcrumb.add("Profile" -> PublishedSpeakerRoutes.detail(req.user.slug)) def editBreadcrumb(implicit req: UserReq[AnyContent]): Breadcrumb = breadcrumb.add("Edit" -> routes.ProfileCtrl.edit()) }
Example 13
Source File: CfpCtrl.scala From gospeak with Apache License 2.0 | 5 votes |
package gospeak.web.pages.orga.cfps import cats.data.OptionT import cats.effect.IO import com.mohiva.play.silhouette.api.Silhouette import gospeak.core.domain.utils.OrgaCtx import gospeak.core.domain.{Cfp, Event, Group} import gospeak.core.services.storage._ import gospeak.web.AppConf import gospeak.web.auth.domain.CookieEnv import gospeak.web.domain.Breadcrumb import gospeak.web.pages.orga.GroupCtrl import gospeak.web.pages.orga.cfps.CfpCtrl._ import gospeak.web.pages.orga.events.routes.{EventCtrl => EventRoutes} import gospeak.web.utils.{GsForms, OrgaReq, UICtrl} import gospeak.libs.scala.domain.Page import play.api.data.Form import play.api.mvc.{Action, AnyContent, ControllerComponents, Result} class CfpCtrl(cc: ControllerComponents, silhouette: Silhouette[CookieEnv], conf: AppConf, userRepo: OrgaUserRepo, val groupRepo: OrgaGroupRepo, cfpRepo: OrgaCfpRepo, eventRepo: OrgaEventRepo, proposalRepo: OrgaProposalRepo) extends UICtrl(cc, silhouette, conf) with UICtrl.OrgaAction { def list(group: Group.Slug, params: Page.Params): Action[AnyContent] = OrgaAction(group) { implicit req => val customParams = params.withNullsFirst cfpRepo.list(customParams).map(cfps => Ok(html.list(cfps)(listBreadcrumb))) // TODO listWithProposalCount } def create(group: Group.Slug, event: Option[Event.Slug]): Action[AnyContent] = OrgaAction(group) { implicit req => createView(group, GsForms.cfp, event) } def doCreate(group: Group.Slug, event: Option[Event.Slug]): Action[AnyContent] = OrgaAction(group) { implicit req => GsForms.cfp.bindFromRequest.fold( formWithErrors => createView(group, formWithErrors, event), data => (for { // TODO check if slug not already exist cfpElt <- OptionT.liftF(cfpRepo.create(data)) redirect <- OptionT.liftF(event.map { e => eventRepo.attachCfp(e, cfpElt.id) .map(_ => Redirect(EventRoutes.detail(group, e))) // TODO recover and redirect to cfp detail }.getOrElse { IO.pure(Redirect(routes.CfpCtrl.detail(group, data.slug))) }) } yield redirect).value.map(_.getOrElse(groupNotFound(group))) ) } private def createView(group: Group.Slug, form: Form[Cfp.Data], event: Option[Event.Slug])(implicit req: OrgaReq[AnyContent]): IO[Result] = { val b = listBreadcrumb.add("New" -> routes.CfpCtrl.create(group)) IO.pure(Ok(html.create(form, event)(b))) } def detail(group: Group.Slug, cfp: Cfp.Slug, params: Page.Params): Action[AnyContent] = OrgaAction(group) { implicit req => (for { cfpElt <- OptionT(cfpRepo.find(cfp)) proposals <- OptionT.liftF(proposalRepo.listFull(cfp, params)) speakers <- OptionT.liftF(userRepo.list(proposals.items.flatMap(_.users).distinct)) userRatings <- OptionT.liftF(proposalRepo.listRatings(cfp)) b = breadcrumb(cfpElt) } yield Ok(html.detail(cfpElt, proposals, speakers, userRatings)(b))).value.map(_.getOrElse(cfpNotFound(group, cfp))) } def edit(group: Group.Slug, cfp: Cfp.Slug, redirect: Option[String]): Action[AnyContent] = OrgaAction(group) { implicit req => editView(group, cfp, GsForms.cfp, redirect) } def doEdit(group: Group.Slug, cfp: Cfp.Slug, redirect: Option[String]): Action[AnyContent] = OrgaAction(group) { implicit req => GsForms.cfp.bindFromRequest.fold( formWithErrors => editView(group, cfp, formWithErrors, redirect), data => (for { cfpOpt <- OptionT.liftF(cfpRepo.find(data.slug)) res <- OptionT.liftF(cfpOpt match { case Some(duplicate) if data.slug != cfp => editView(group, cfp, GsForms.cfp.fillAndValidate(data).withError("slug", s"Slug already taken by cfp: ${duplicate.name.value}"), redirect) case _ => cfpRepo.edit(cfp, data).map { _ => redirectOr(redirect, routes.CfpCtrl.detail(group, data.slug)) } }) } yield res).value.map(_.getOrElse(groupNotFound(group))) ) } private def editView(group: Group.Slug, cfp: Cfp.Slug, form: Form[Cfp.Data], redirect: Option[String])(implicit req: OrgaReq[AnyContent], ctx: OrgaCtx): IO[Result] = { (for { cfpElt <- OptionT(cfpRepo.find(cfp)) filledForm = if (form.hasErrors) form else form.fill(cfpElt.data) b = breadcrumb(cfpElt).add("Edit" -> routes.CfpCtrl.edit(group, cfp)) } yield Ok(html.edit(cfpElt, filledForm, redirect)(b))).value.map(_.getOrElse(cfpNotFound(group, cfp))) } } object CfpCtrl { def listBreadcrumb(implicit req: OrgaReq[AnyContent]): Breadcrumb = GroupCtrl.breadcrumb.add("CFPs" -> routes.CfpCtrl.list(req.group.slug)) def breadcrumb(cfp: Cfp)(implicit req: OrgaReq[AnyContent]): Breadcrumb = listBreadcrumb.add(cfp.name.value -> routes.CfpCtrl.detail(req.group.slug, cfp.slug)) }
Example 14
Source File: InputLocalTimeSpec.scala From gospeak with Apache License 2.0 | 5 votes |
package gospeak.web.pages.partials.form import gospeak.web.testingutils.TwirlSpec import play.api.data.Forms._ import play.api.data.{Field, Form} class InputLocalTimeSpec extends TwirlSpec { private val form: Form[String] = Form(mapping( "text" -> text )(identity)(Some(_))) private val field: Field = form("text") describe("pages.partials.form.inputLocalTime.scala.html") { it("should render HTML") { val res = html.inputLocalTime(field).body.trim res should include("input-time") } } }
Example 15
Source File: DisplayGlobalErrorsSpec.scala From gospeak with Apache License 2.0 | 5 votes |
package gospeak.web.pages.partials.form import gospeak.web.testingutils.TwirlSpec import play.api.data.Form import play.api.data.Forms._ class DisplayGlobalErrorsSpec extends TwirlSpec { private val form: Form[String] = Form(mapping( "text" -> text )(identity)(Some(_))) describe("pages.partials.form.displayGlobalErrors.scala.html") { it("should display nothing when no errors") { val res = html.displayGlobalErrors(form)(userReq).body.trim res shouldBe "" } it("should display the error text when just one error") { val f = form.withGlobalError("error alone") val res = html.displayGlobalErrors(f)(userReq).body.trim res should include("error alone") } it("should display the list of errors when multiple errors") { val f = form.withGlobalError("error 1").withGlobalError("error 2") val res = html.displayGlobalErrors(f)(userReq).body.trim res should include("error 1") res should include("error 2") } } }
Example 16
Source File: ResetPasswordController.scala From recogito2 with Apache License 2.0 | 5 votes |
package controllers.landing import javax.inject.{Inject, Singleton} import services.user.UserService import services.generated.tables.records.UserRecord import play.api.data.Form import play.api.data.Forms._ import play.api.i18n.I18nSupport import play.api.libs.mailer._ import play.api.mvc.{Action, AbstractController, ControllerComponents} import scala.concurrent.{ExecutionContext, Future} case class ResetPasswordData(email: String) @Singleton class ResetPasswordController @Inject() ( val components: ControllerComponents, val users: UserService, val mailerClient: MailerClient, implicit val ctx: ExecutionContext ) extends AbstractController(components) with I18nSupport { val resetPasswordForm = Form( mapping("email" -> email)(ResetPasswordData.apply)(ResetPasswordData.unapply) ) private def sendMail(user: UserRecord, newPassword: String) = Future { val name = Option(user.getRealName).getOrElse(user.getUsername) val message = s"""Dear $name, | |Your Recogito password was reset to | | $newPassword | |Please log in to Recogito with this password. You can change it |in your personal Account Settings. | |Cheers, |the Recogito Team""".stripMargin // TODO this now hard-wires "[email protected]" as reply address // TODO see if we can take this directly from the config file instead val email = Email( "Your Recogito password was reset", "Recogito Team <[email protected]>", Seq(users.decryptEmail(user.getEmail)), Some(message) ) mailerClient.send(email) } recover { case t: Throwable => t.printStackTrace } def showResetForm = Action { implicit request => Ok(views.html.landing.resetPassword(resetPasswordForm)) } def resetPassword = Action.async { implicit request => resetPasswordForm.bindFromRequest.fold( formWithErrors => Future.successful(BadRequest(views.html.landing.resetPassword(formWithErrors))), resetData => users.findByEmail(resetData.email).flatMap { case Some(user) => users.resetPassword(user.getUsername).map { password => sendMail(user, password) Ok(views.html.landing.resetPasswordOk()) } case None => Future.successful(Redirect(routes.ResetPasswordController.showResetForm()).flashing("error" -> "Can't find that e-mail, sorry.")) } ) } }
Example 17
Source File: LoginLogoutController.scala From recogito2 with Apache License 2.0 | 5 votes |
package controllers.landing import com.mohiva.play.silhouette.api.{LoginInfo, Silhouette} import controllers.{HasConfig, HasUserService, Security} import java.util.UUID import javax.inject.{Inject, Singleton} import services.announcement.AnnouncementService import services.user.UserService import play.api.Configuration import play.api.data.Form import play.api.data.Forms._ import play.api.i18n.I18nSupport import play.api.mvc.{AbstractController, ControllerComponents} import scala.concurrent.{ ExecutionContext, Future } case class LoginData(usernameOrPassword: String, password: String) @Singleton class LoginLogoutController @Inject() ( val announcements: AnnouncementService, val components: ControllerComponents, val config: Configuration, val silhouette: Silhouette[Security.Env], val users: UserService, implicit val ctx: ExecutionContext ) extends AbstractController(components) with HasConfig with HasUserService with I18nSupport { private val MESSAGE = "message" private val INVALID_LOGIN = "Invalid Username or Password" private val auth = silhouette.env.authenticatorService val loginForm = Form( mapping( "username" -> nonEmptyText, "password" -> nonEmptyText )(LoginData.apply)(LoginData.unapply) ) def showLoginForm(destination: Option[String]) = Action { implicit request => destination match { case None => Ok(views.html.landing.login(loginForm)) case Some(dest) => Ok(views.html.landing.login(loginForm)).withSession("access_uri" -> dest) } } def processLogin = silhouette.UserAwareAction.async { implicit request => loginForm.bindFromRequest.fold( formWithErrors => Future(BadRequest(views.html.landing.login(formWithErrors))), loginData => users.validateUser(loginData.usernameOrPassword, loginData.password).flatMap { case Some(validUser) => val destination = request.session.get("access_uri").getOrElse(routes.LandingController.index.toString) users.updateLastLogin(validUser.getUsername) val fAuthentication = auth.create(LoginInfo(Security.PROVIDER_ID, validUser.getUsername)) .flatMap(auth.init(_)) fAuthentication.flatMap { authentication => auth.embed(authentication, Redirect(destination).withSession(request.session - "access_uri")) } case None => Future(Redirect(routes.LoginLogoutController.showLoginForm()).flashing(MESSAGE -> INVALID_LOGIN)) } ) } def confirmServiceAnnouncement(id: UUID, response: String, destination: Option[String]) = silhouette.SecuredAction.async { implicit request => announcements.confirm(id, request.identity.username, response).map { success => if (success) destination.map(Redirect(_)).getOrElse(Redirect(routes.LandingController.index)) else InternalServerError } } def logout = silhouette.SecuredAction.async { implicit request => auth.discard(request.authenticator, Redirect(routes.LandingController.index)) } }
Example 18
Source File: AccountSettingsController.scala From recogito2 with Apache License 2.0 | 5 votes |
package controllers.my.settings import com.mohiva.play.silhouette.api.{Silhouette, LoginInfo} import controllers.{HasUserService, HasConfig, Security} import javax.inject.Inject import services.announcement.AnnouncementService import services.annotation.AnnotationService import services.contribution.ContributionService import services.user.Roles._ import services.user.UserService import services.upload.UploadService import services.document.DocumentService import org.webjars.play.WebJarsUtil import play.api.Configuration import play.api.data.Form import play.api.data.Forms._ import play.api.i18n.I18nSupport import play.api.mvc.{AbstractController, ControllerComponents} import scala.concurrent.{Await, ExecutionContext, Future} import scala.concurrent.duration._ import controllers.HasAccountRemoval case class AccountSettingsData( email : String, name : Option[String], bio : Option[String], website: Option[String]) class AccountSettingsController @Inject() ( val components: ControllerComponents, val config: Configuration, val silhouette: Silhouette[Security.Env], implicit val announcements: AnnouncementService, implicit val annotations: AnnotationService, implicit val contributions: ContributionService, implicit val ctx: ExecutionContext, implicit val documents: DocumentService, implicit val uploads: UploadService, implicit val users: UserService, implicit val webjars: WebJarsUtil ) extends AbstractController(components) with HasUserService with HasConfig with HasAccountRemoval with I18nSupport { val accountSettingsForm = Form( mapping( "email" -> email, "name" -> optional(text(maxLength=80)), "bio" -> optional(text(maxLength=256)), "website" -> optional(text(maxLength=256)) )(AccountSettingsData.apply)(AccountSettingsData.unapply) ) def index() = silhouette.SecuredAction { implicit request => val u = request.identity val form = accountSettingsForm.fill(AccountSettingsData( users.decryptEmail(u.email), u.realName, u.bio, u.website)) Ok(views.html.my.settings.account(form, u)) } def updateAccountSettings() = silhouette.SecuredAction.async { implicit request => accountSettingsForm.bindFromRequest.fold( formWithErrors => Future.successful(BadRequest(views.html.my.settings.account(formWithErrors, request.identity))), f => users.updateUserSettings(request.identity.username, f.email, f.name, f.bio, f.website) .map { success => if (success) Redirect(routes.AccountSettingsController.index).flashing("success" -> "Your settings have been saved.") else Redirect(routes.AccountSettingsController.index).flashing("error" -> "There was an error while saving your settings.") }.recover { case t:Throwable => { t.printStackTrace() Redirect(routes.AccountSettingsController.index).flashing("error" -> "There was an error while saving your settings.") }} ) } def deleteAccount() = silhouette.SecuredAction.async { implicit request => deleteUserAccount(request.identity.username).flatMap { _ => silhouette.env.authenticatorService.discard( request.authenticator, Redirect(controllers.landing.routes.LandingController.index)) } } }
Example 19
Source File: PasswordSettingsController.scala From recogito2 with Apache License 2.0 | 5 votes |
package controllers.my.settings import com.mohiva.play.silhouette.api.Silhouette import controllers.{ HasConfig, HasUserService, Security } import javax.inject.Inject import services.user.UserService import services.user.Roles._ import play.api.Configuration import play.api.data.Form import play.api.data.Forms._ import play.api.data.validation._ import play.api.i18n.{ I18nSupport, MessagesApi } import play.api.mvc.{AbstractController, ControllerComponents} import scala.concurrent.{ ExecutionContext, Future } case class PasswordSettingsData(currentPassword: String, newPassword: String, verifyPassword: String) class PasswordSettingsController @Inject() ( val components: ControllerComponents, val config: Configuration, val users: UserService, val silhouette: Silhouette[Security.Env], implicit val ctx: ExecutionContext ) extends AbstractController(components) with HasConfig with HasUserService with I18nSupport { private val matchingPasswords: Constraint[PasswordSettingsData] = Constraint("constraints.valid") { d => if (d.newPassword == d.verifyPassword) Valid else Invalid("Passwords must match") } val passwordSettingsForm = Form( mapping( "current" -> nonEmptyText, "new" -> nonEmptyText, "verify" -> nonEmptyText )(PasswordSettingsData.apply)(PasswordSettingsData.unapply).verifying(matchingPasswords) ) def index() = silhouette.SecuredAction { implicit request => Ok(views.html.my.settings.password(passwordSettingsForm, request.identity)) } def updatePassword() = silhouette.SecuredAction.async { implicit request => passwordSettingsForm.bindFromRequest.fold( formWithErrors => Future.successful(BadRequest(views.html.my.settings.password(formWithErrors, request.identity))), f => { users.updatePassword(request.identity.username, f.currentPassword, f.newPassword) .map { _ match { case Right(_) => Redirect(routes.PasswordSettingsController.index).flashing("success" -> "Your password has been updated.") case Left(errorMessage) => Redirect(routes.PasswordSettingsController.index).flashing("error" -> errorMessage) }}.recover { case t:Throwable => { t.printStackTrace() Redirect(routes.PasswordSettingsController.index).flashing("error" -> "There was an error while updating your password.") }} } ) } }
Example 20
Source File: Index.scala From Mastering-Spark-for-Data-Science with MIT License | 5 votes |
package controllers import com.typesafe.config.ConfigFactory import models.{Library, Songs} import org.apache.commons.lang3.StringUtils import play.api.Logger import play.api.data.Form import play.api.data.Forms._ import play.api.mvc._ import svc.{AnalyzerSvc, CassandraDao, SparkSvc} object Index extends Controller { val config = ConfigFactory.load() val minTime = config.getInt("gzet.min.time") val maxTime = config.getInt("gzet.max.time") val cassandraHost = config.getString("cassandra.host") val cassandraPort = config.getInt("cassandra.port") val sampleSize = config.getDouble("gzet.sample.size") val minMatch = config.getDouble("gzet.min.match") val dao = new CassandraDao(cassandraHost, cassandraPort) val analyzer = new AnalyzerSvc() val spark = new SparkSvc() val indexForm: Form[Library] = Form(mapping("path" -> text)(Library.apply)(Library.unapply)) def index = Action { implicit request => val songs = Songs(dao.getSongs) Logger.info(s"Database is currently ${songs.songs.size} songs long") Ok(views.html.index(indexForm)(songs)) } def submit = Action { implicit request => indexForm.bindFromRequest.fold( errors => Redirect(routes.Index.index()).flashing("error" -> s"Missing path"), index => try { if(StringUtils.isNotEmpty(index.path)) { Logger.info("Dropping database") dao.dropSongs dao.dropPlaylist Logger.info("Submitting job") val jobId = spark.index(index.path) Redirect(routes.Index.index()).flashing("success" -> jobId) } else { Redirect(routes.Index.index()).flashing("error" -> s"Missing path") } } catch { case e: Exception => Redirect(routes.Index.index()).flashing("error" -> e.getMessage) } ) } }
Example 21
Source File: PasswordReset.scala From cave with MIT License | 5 votes |
package controllers import play.api.Logger import play.api.data.Form import play.api.data.Forms._ import play.api.i18n.Messages import play.api.mvc._ import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future import scala.util.control.NonFatal class PasswordReset extends AbstractCaveController { val startResetPasswordForm = Form( "email" -> email ) val confirmResetPasswordForm = Form( tuple( "password1" -> text, "password2" -> text ) verifying(Messages("cave.login.signup.passwordDoesntMatch"), fields => fields match { case (password1: String, password2: String) => password1 == password2 }) ) def startResetPassword = Action { implicit request => Ok(views.html.loginscreen.startResetPassword(startResetPasswordForm)) } def handleStartResetPassword = Action.async { implicit request => withCaveClient { client => startResetPasswordForm.bindFromRequest().fold( formWithErrors => Future.successful(BadRequest(views.html.loginscreen.startResetPassword(formWithErrors))), email => { client.Users.postForgotPassword(email) map { _ => Redirect(routes.Authentication.login).flashing("success" -> Messages("cave.login.passwordReset.checkYourEmail", email)) } recover { case NonFatal(e) => Logger.error(s"Password reset error for $email", e) Redirect(routes.Authentication.login).flashing("error" -> Messages("cave.login.passwordReset.error")) } } ) } } def resetPassword(mailToken: String) = Action { implicit request => Ok(views.html.loginscreen.resetPasswordPage(confirmResetPasswordForm, mailToken)) } def handleResetPassword(mailToken: String) = Action.async { implicit request => withCaveClient { client => confirmResetPasswordForm.bindFromRequest().fold( formWithErrors => Future.successful(BadRequest(views.html.loginscreen.resetPasswordPage(formWithErrors, mailToken))), values => { client.Users.postResetPassword(values._1, mailToken) map { _ => debug(s"Password changed") Redirect(routes.Authentication.login).flashing("success" -> Messages("cave.login.password.reset.success")) } recover { case NonFatal(e) => Logger.error("Password change error", e) Redirect(routes.Authentication.login).flashing("error" -> Messages("cave.login.passwordReset.error")) } } ) } } }
Example 22
Source File: PasswordChange.scala From cave with MIT License | 5 votes |
package controllers import com.gilt.cavellc.errors.FailedRequest import play.api.Logger import play.api.data.Form import play.api.data.Forms._ import play.api.i18n.Messages import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future import scala.util.control.NonFatal class PasswordChange extends AbstractCaveController { val passwordChange = Form( tuple( "newPassword.password1" -> text, "newPassword.password2" -> text ) verifying(Messages("cave.login.signup.passwordDoesntMatch"), fields => fields match { case (password1: String, password2: String) => password1 == password2 }) ) def page = caveAsyncAction { implicit request => Future.successful(Ok(views.html.loginscreen.passwordChange(passwordChange))) } def handlePasswordChange = caveAsyncAction { implicit request => withCaveClient { client => passwordChange.bindFromRequest().fold( formWithErrors => Future.successful(BadRequest(views.html.loginscreen.passwordChange(formWithErrors))), values => client.Users.patchInfo(None, None, Some(values._1)) map { case user => Redirect(routes.UserProfile.profile()).flashing("success" -> s"${user.firstName}, your password has been changed") } recover { case error: FailedRequest if error.responseCode == FORBIDDEN => Logger.debug("Your API session token has expired. Please login again.", error) Redirect(routes.Authentication.login).flashing("error" -> Messages("cave.login.sessionTokenExpired")) case NonFatal(e) => Logger.error("Unable to change password", e) InternalServerError(views.html.errorpages.errorPage(Messages("cave.errors.5xx.passwordChange"))) } ) } } }
Example 23
Source File: UserProfile.scala From cave with MIT License | 5 votes |
package controllers import play.api.data.Form import play.api.data.Forms._ import play.api.i18n.Messages import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future class UserProfile extends AbstractCaveController { val changeDetails = Form( mapping( "firstName" -> nonEmptyText, "lastName" -> nonEmptyText )(UserProfileChange.apply)(UserProfileChange.unapply) ) def update = caveAsyncAction { implicit request => withCaveClient { client => changeDetails.bindFromRequest().fold( formWithErrors => Future.successful(Redirect(routes.UserProfile.profile).flashing("error" -> buildFormValidationErrorMessage(formWithErrors))), change => client.Users.patchInfo(Some(change.firstName), Some(change.lastName), None) map { case user => Redirect(routes.UserProfile.profile).flashing("success" -> Messages("cave.profile.changeDetails.modal.success")). withSession(request.session + ("userName" -> s"${user.firstName} ${user.lastName}")) } ) } } def profile = caveAsyncAction { implicit request => withCaveClient { client => client.Users.getInfo().map { case Some(user) => Ok(views.html.profile.profile(user, userToken, changeDetails.fill(UserProfileChange(user.firstName, user.lastName)))) case None => InternalServerError("Unable to get user information") } } } } case class UserProfileChange(firstName: String, lastName: String)
Example 24
Source File: Registration.scala From cave with MIT License | 5 votes |
package controllers import play.api.Logger import play.api.data.Form import play.api.i18n.Messages import play.api.mvc._ import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future import play.api.data.Forms._ import scala.util.control.NonFatal case class SignUpFormData(firstName: String, lastName: String, password1: String, password2: String) class Registration extends AbstractCaveController { val startSignUpForm = Form( "email" -> email ) val confirmSignUpForm = Form( mapping( "firstName" -> text, "lastName" -> text, "password1" -> text, "password2" -> text )(SignUpFormData.apply)(SignUpFormData.unapply) verifying(Messages("cave.login.signup.passwordDoesntMatch"), fields => fields match { case data: SignUpFormData => data.password1 == data.password2 }) ) def startSignUp = Action { implicit request => Ok(views.html.loginscreen.startSignUp(startSignUpForm)) } def handleStartSignUp = Action.async { implicit request => withCaveClient { client => startSignUpForm.bindFromRequest().fold( formWithErrors => Future.successful(BadRequest(views.html.loginscreen.startSignUp(formWithErrors))), email => { client.Users.postRegister(email) map { _ => Redirect(routes.Authentication.login).flashing("success" -> Messages("cave.login.signup.checkYourEmail", email)) } recover { case NonFatal(e) => Logger.error("New user registration error", e) Redirect(routes.Authentication.login).flashing("error" -> Messages("cave.login.signup.error")) } } ) } } def signUp(mailToken: String) = Action { implicit request => Ok(views.html.loginscreen.signUp(confirmSignUpForm, mailToken)) } def handleSignUp(mailToken: String) = Action.async { implicit request => withCaveClient { client => confirmSignUpForm.bindFromRequest().fold( formWithErrors => Future.successful(BadRequest(views.html.loginscreen.signUp(formWithErrors, mailToken))), formData => { client.Users.postConfirm(formData.firstName, formData.lastName, formData.password1, mailToken) map { _ => Redirect(routes.Authentication.login).flashing("success" -> Messages("cave.login.signup.youCanLoginNow", email)) } recover { case NonFatal(e) => Logger.error("Registration confirmation error", e) Redirect(routes.Authentication.login).flashing("error" -> Messages("cave.login.signup.error")) } } ) } } } object Registration extends Registration
Example 25
Source File: InternationalAddressChoiceDto.scala From pertax-frontend with Apache License 2.0 | 5 votes |
package models.dto import play.api.data.Form import play.api.data.Forms._ import play.api.libs.json.Json case class InternationalAddressChoiceDto(value: Boolean) object InternationalAddressChoiceDto { implicit val formats = Json.format[InternationalAddressChoiceDto] val form = Form( mapping( "internationalAddressChoice" -> optional(boolean) .verifying("error.you_must_select_an_answer", _.isDefined) .transform[Boolean](_.getOrElse(false), Some(_)) )(InternationalAddressChoiceDto.apply)(InternationalAddressChoiceDto.unapply) ) }
Example 26
Source File: SingleCheckboxSpec.scala From play-ui with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.play.views.helpers import org.jsoup.Jsoup import org.scalatest.{Matchers, WordSpec} import play.api.data.Form import play.api.data.Forms.{mapping, _} import play.api.test.Helpers._ import uk.gov.hmrc.play.MessagesSupport import uk.gov.hmrc.play.views.html.helpers.SingleCheckbox class SingleCheckboxSpec extends WordSpec with Matchers with MessagesSupport { case class DummyFormData(exampleCheckbox: Option[Boolean]) def dummyForm = Form( mapping( "exampleCheckbox" -> optional(boolean) )(DummyFormData.apply)(DummyFormData.unapply)) val singleCheckbox = new SingleCheckbox() "Have the checked attribute when value is 'true'" in { val WithTrueCheckboxValueForm = dummyForm.fill(DummyFormData(Some(true))) val doc = Jsoup.parse( contentAsString( singleCheckbox( WithTrueCheckboxValueForm("exampleCheckbox"), '_label -> "exampleLabel", '_inputClass -> "inputClass", '_labelClass -> "labelClass" ) ) ) val checkboxElement = doc.getElementById("exampleCheckbox") val labelElement = doc.select("label") checkboxElement.hasAttr("checked") shouldBe true checkboxElement.attr("checked") shouldBe "checked" checkboxElement.hasClass("inputClass") shouldBe true labelElement.text() shouldBe "exampleLabel" labelElement.hasClass("labelClass") shouldBe true labelElement.hasClass("selected") shouldBe true } "The Single CheckBox" should { "Not have the checked attribute when value is 'None'" in { val doc = Jsoup.parse( contentAsString( singleCheckbox( dummyForm("exampleCheckbox"), '_label -> "exampleLabel", '_inputClass -> "inputClass", '_labelClass -> "labelClass" ) ) ) val checkboxElement = doc.getElementById("exampleCheckbox") val labelElement = doc.select("label") checkboxElement.hasAttr("checked") shouldBe false checkboxElement.hasClass("inputClass") shouldBe true labelElement.text() shouldBe "exampleLabel" labelElement.hasClass("labelClass") shouldBe true labelElement.hasClass("selected") shouldBe false } "Not have the checked attribute when value is 'false'" in { val WithFalseCheckboxValueForm = dummyForm.fill(DummyFormData(Some(false))) val doc = Jsoup.parse( contentAsString( singleCheckbox( WithFalseCheckboxValueForm("exampleCheckbox"), '_label -> "exampleLabel", '_inputClass -> "inputClass", '_labelClass -> "labelClass" ) ) ) val checkboxElement = doc.getElementById("exampleCheckbox") val labelElement = doc.select("label") checkboxElement.hasAttr("checked") shouldBe false checkboxElement.hasClass("inputClass") shouldBe true labelElement.text() shouldBe "exampleLabel" labelElement.hasClass("labelClass") shouldBe true labelElement.hasClass("selected") shouldBe false } } }
Example 27
Source File: AddressFinderDto.scala From pertax-frontend with Apache License 2.0 | 5 votes |
package models.dto import play.api.data.Form import play.api.data.Forms._ import play.api.libs.json.Json import util.PertaxValidators._ case class AddressFinderDto(postcode: String, filter: Option[String]) object AddressFinderDto { implicit val formats = Json.format[AddressFinderDto] val form = Form( mapping( "postcode" -> text .verifying( "error.enter_a_valid_uk_postcode", e => e match { case PostcodeRegex(_*) => true case _ => false }), "filter" -> optional(nonEmptyText) .verifying("error.enter_valid_characters", e => validateAddressLineCharacters(e)) )(AddressFinderDto.apply)(AddressFinderDto.unapply) ) }
Example 28
Source File: ClosePostalAddressChoiceDto.scala From pertax-frontend with Apache License 2.0 | 5 votes |
package models.dto import play.api.data.Form import play.api.data.Forms._ import play.api.libs.json.Json case class ClosePostalAddressChoiceDto(value: Boolean) object ClosePostalAddressChoiceDto { implicit val formats = Json.format[ClosePostalAddressChoiceDto] val form = Form( mapping( "onPageLoad" -> optional(boolean) .verifying("error.you_must_select_an_answer", _.isDefined) .transform[Boolean](_.getOrElse(false), Some(_)) )(ClosePostalAddressChoiceDto.apply)(ClosePostalAddressChoiceDto.unapply) ) }
Example 29
Source File: DateDto.scala From pertax-frontend with Apache License 2.0 | 5 votes |
package models.dto import org.joda.time.LocalDate import play.api.data.Form import play.api.data.Forms._ import play.api.libs.json.Json import uk.gov.hmrc.play.mappers.DateTuple._ import play.api.libs.json.JodaWrites._ import play.api.libs.json.JodaReads._ case class DateDto( startDate: LocalDate ) object DateDto { implicit val formats = Json.format[DateDto] def build(day: Int, month: Int, year: Int) = DateDto(new LocalDate(year, month, day)) def form(today: LocalDate) = Form( mapping( "startDate" -> mandatoryDateTuple("error.enter_a_date") .verifying("error.date_in_future", !_.isAfter(today)) .verifying("error.enter_valid_date", !_.isBefore(new LocalDate("1000-01-01"))) )(DateDto.apply)(DateDto.unapply) ) }
Example 30
Source File: SAWrongCredentialsDto.scala From pertax-frontend with Apache License 2.0 | 5 votes |
package models.dto import play.api.data.Form import play.api.data.Forms._ import play.api.libs.json.Json final case class SAWrongCredentialsDto(value: Boolean) object SAWrongCredentialsDto { implicit val formats = Json.format[SAWrongCredentialsDto] val form = Form( mapping( "wrongCredentialsFormChoice" -> optional(boolean) .verifying("error.you_must_select_an_answer", _.isDefined) .transform[Boolean](_.getOrElse(false), Some(_)) )(SAWrongCredentialsDto.apply)(SAWrongCredentialsDto.unapply) ) }
Example 31
Source File: TaxCreditsChoiceDto.scala From pertax-frontend with Apache License 2.0 | 5 votes |
package models.dto import play.api.data.Form import play.api.data.Forms._ import play.api.libs.json.Json case class TaxCreditsChoiceDto(value: Boolean) object TaxCreditsChoiceDto { implicit val formats = Json.format[TaxCreditsChoiceDto] val form = Form( mapping( "taxCreditsChoice" -> optional(boolean) .verifying("error.you_must_select_an_answer", _.isDefined) .transform[Boolean](_.getOrElse(false), Some(_)) //getOrElse here will never fall back to default because of isDefined above )(TaxCreditsChoiceDto.apply)(TaxCreditsChoiceDto.unapply) ) }
Example 32
Source File: StartDateController.scala From pertax-frontend with Apache License 2.0 | 5 votes |
package controllers.address import com.google.inject.Inject import config.ConfigDecorator import controllers.auth.{AuthJourney, WithActiveTabAction} import controllers.bindable.{AddrType, PostalAddrType, ResidentialAddrType} import controllers.controllershelpers.AddressJourneyCachingHelper import models.dto.DateDto import models.{Address, SubmittedStartDateId} import play.api.data.Form import play.api.mvc.{Action, AnyContent, MessagesControllerComponents, Result} import uk.gov.hmrc.renderer.TemplateRenderer import util.{LanguageHelper, LocalPartialRetriever} import views.html.interstitial.DisplayAddressInterstitialView import views.html.personaldetails.{CannotUpdateAddressView, EnterStartDateView} import scala.concurrent.{ExecutionContext, Future} class StartDateController @Inject()( authJourney: AuthJourney, withActiveTabAction: WithActiveTabAction, cc: MessagesControllerComponents, cachingHelper: AddressJourneyCachingHelper, enterStartDateView: EnterStartDateView, cannotUpdateAddressView: CannotUpdateAddressView, displayAddressInterstitialView: DisplayAddressInterstitialView)( implicit partialRetriever: LocalPartialRetriever, configDecorator: ConfigDecorator, templateRenderer: TemplateRenderer, ec: ExecutionContext) extends AddressController(authJourney, withActiveTabAction, cc, displayAddressInterstitialView) { def onPageLoad(typ: AddrType): Action[AnyContent] = authenticate.async { implicit request => addressJourneyEnforcer { _ => personDetails => nonPostalJourneyEnforcer(typ) { cachingHelper.gettingCachedJourneyData(typ) { journeyData => val newPostcode = journeyData.submittedAddressDto.map(_.postcode).getOrElse("").toString val oldPostcode = personDetails.address.flatMap(add => add.postcode).getOrElse("") journeyData.submittedAddressDto map { _ => val postcodesMatch = if (newPostcode.replace(" ", "").equalsIgnoreCase(oldPostcode.replace(" ", ""))) { journeyData.submittedStartDateDto.fold(dateDtoForm)(dateDtoForm.fill) } else { dateDtoForm } Future.successful(Ok(enterStartDateView(postcodesMatch, typ))) } getOrElse { Future.successful(Redirect(routes.PersonalDetailsController.onPageLoad())) } } } } } def onSubmit(typ: AddrType): Action[AnyContent] = authenticate.async { implicit request => addressJourneyEnforcer { _ => personDetails => nonPostalJourneyEnforcer(typ) { dateDtoForm.bindFromRequest.fold( formWithErrors => { Future.successful(BadRequest(enterStartDateView(formWithErrors, typ))) }, dateDto => { cachingHelper.addToCache(SubmittedStartDateId(typ), dateDto) map { _ => val proposedStartDate = dateDto.startDate personDetails.address match { case Some(Address(_, _, _, _, _, _, _, Some(currentStartDate), _, _)) => if (!currentStartDate.isBefore(proposedStartDate)) { BadRequest( cannotUpdateAddressView(typ, LanguageHelper.langUtils.Dates.formatDate(proposedStartDate))) } else { Redirect(routes.AddressSubmissionController.onPageLoad(typ)) } case _ => Redirect(routes.AddressSubmissionController.onPageLoad(typ)) } } } ) } } } private def dateDtoForm: Form[DateDto] = DateDto.form(configDecorator.currentLocalDate) private def nonPostalJourneyEnforcer(typ: AddrType)(block: => Future[Result]): Future[Result] = typ match { case _: ResidentialAddrType => block case PostalAddrType => Future.successful(Redirect(controllers.address.routes.UpdateAddressController.onPageLoad(typ))) } }
Example 33
Source File: TableOfContentController.scala From play-table-of-contents with MIT License | 5 votes |
package controllers import java.net.URL import javax.inject.Inject import context.MyExecutionContext import models.ReadmeForm import play.api.Logger import play.api.data.Form import play.api.data.Forms._ import play.api.mvc._ import readme.TableOfContentHelper import util.HtmlUtil import scala.concurrent.Future class TableOfContentController @Inject()(ec: MyExecutionContext, cc: ControllerComponents) extends AbstractController(cc) { val logger = Logger(this.getClass) implicit val xc: MyExecutionContext = ec val userForm = Form( mapping( "description" -> text, "github_url" -> text )(ReadmeForm.apply)(ReadmeForm.unapply) ) val startContent: String = """Example : |# Title 1 |## Title 2 |### Title 3""".stripMargin('|') def readme = Action { Ok(HtmlUtil.prettify(views.html.readme(startContent))) } private def getGithubReadmeUrl(url: String): String = { val githubUrl = new URL(url) val path = githubUrl.getPath.substring(1) val endIndex = path.indexOf("/", path.indexOf("/") + 1) val userNproject = if (endIndex == -1) path else path.substring(0, endIndex) s"https://raw.githubusercontent.com/${userNproject}/master/README.md" } private def readContentFromUrl(mdUrl: String): Future[String] = Future { val f = scala.io.Source.fromURL(mdUrl) try f.mkString finally f.close() } }
Example 34
Source File: ComputerForm.scala From Aton with GNU General Public License v3.0 | 5 votes |
package model.form import model.form.data.ComputerFormData import play.api.data.Form import play.api.data.Forms._ object ComputerForm { val form = Form( mapping( "computer.ip" -> nonEmptyText, "room.name" -> optional(text), "computer.SSHUser" -> nonEmptyText, "computer.SSHPassword" -> nonEmptyText, "description" -> optional(text), "roomID" -> optional(longNumber) )(ComputerFormData.apply)(ComputerFormData.unapply) ) }
Example 35
Source File: InputSpec.scala From play-ui with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.play.views package helpers import org.scalatest.{Matchers, WordSpec} import play.api.data.Forms.{mapping, text} import play.api.data.{Field, Form, FormError} import play.api.test.Helpers._ import uk.gov.hmrc.play.MessagesSupport import uk.gov.hmrc.play.views.html.helpers.Input import scala.collection.JavaConverters._ class InputSpec extends WordSpec with Matchers with MessagesSupport { case class DummyFormData(inputBoxValue: String) def dummyForm = Form( mapping( "inputBoxValue" -> text )(DummyFormData.apply)(DummyFormData.unapply)) val input = new Input() "@helpers.input" should { "render an input box" in { val doc = jsoupDocument(input(field = dummyForm("inputValue"), '_inputClass -> "myInputClass", '_label -> "myLabel")) val inputBox = doc.getElementById("inputValue") inputBox.attr("type") shouldBe "text" inputBox.attr("name") shouldBe "inputValue" inputBox.attr("value") shouldBe "" inputBox.attr("class") shouldBe "myInputClass" inputBox.parent().text() shouldBe "myLabel" } "render error notification when errors are present" in { val doc = jsoupDocument( input( field = Field(dummyForm, "", Seq.empty, None, Seq(FormError("inputBoxValue", "Enter a value")), Some("")), '_inputClass -> "myInputClass", '_label -> "myLabel", '_error_id -> "myError" )) val errorMessage = doc.getElementById("myError").text errorMessage shouldBe "Enter a value" } "render input box with errors in the right order" in { val doc = jsoupDocument( input( field = Field(dummyForm, "", Seq.empty, None, Seq(FormError("inputBoxValue", "Form Error Text")), Some("")), '_inputClass -> "inputClass", '_label -> "Label Text", '_error_id -> "errorId", '_inputHint -> "Input Hint Text", '_labelClass -> "myLabelClass" )) val inputBox = doc.getElementsByClass("myLabelClass").first val listOfInputBoxTextInOrder = inputBox.getElementsByTag("span").asScala.toList.map(_.text) listOfInputBoxTextInOrder shouldBe List("Label Text", "Input Hint Text", "Form Error Text") } } }
Example 36
Source File: DropDownSpec.scala From play-ui with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.play.views package helpers import org.scalatest.{Matchers, WordSpec} import play.api.data.Form import play.api.data.Forms.{mapping, _} import play.api.test.Helpers._ import uk.gov.hmrc.play.MessagesSupport import uk.gov.hmrc.play.views.html.helpers.Dropdown class DropDownSpec extends WordSpec with Matchers with MessagesSupport { case class DummyFormData(country: String) def dummyForm = Form( mapping( "country" -> text )(DummyFormData.apply)(DummyFormData.unapply)) val dropdown = new Dropdown() "@helpers.dropDown" should { "render element options" in { val countries = Seq("AU" -> "Australia", "JP" -> "Japan") val doc = jsoupDocument(dropdown(dummyForm("country"), countries, displayEmptyValue = false)) doc.getElementById("country-AU").outerHtml() shouldBe "<option id=\"country-AU\" value=\"AU\">Australia</option>" doc.getElementById("country-JP").outerHtml() shouldBe "<option id=\"country-JP\" value=\"JP\">Japan</option>" } } }
Example 37
Source File: DateFieldsSpec.scala From play-ui with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.play.views package helpers import org.scalatest.{Matchers, WordSpec} import play.api.data.Form import play.api.data.Forms.{mapping, of => fieldOf} import play.api.data.format.Formats._ import play.api.test.Helpers._ import play.twirl.api.Html import uk.gov.hmrc.play.MessagesSupport import uk.gov.hmrc.play.views.html.helpers._ class DateFieldsSpec extends WordSpec with Matchers with MessagesSupport { val months = Seq( "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December") case class DummyFormData(day: Int, month: Int, year: Int) def dummyForm = Form( mapping( "dummyField.day" -> fieldOf[Int], "dummyField.month" -> fieldOf[Int], "dummyField.year" -> fieldOf[Int] )(DummyFormData.apply)(DummyFormData.unapply)) "The Date Fields with a freeform year input box" should { "Display months using long nouns" in { val dateFieldsFreeYearInline = new DateFieldsFreeYearInline(new Input(), new Dropdown()) val doc = jsoupDocument(dateFieldsFreeYearInline(dummyForm, "dummyField", Html("label"))) months.zipWithIndex.foreach { case (month: String, i: Int) => doc.getElementById(s"dummyField.month-${i + 1}").text shouldBe month } } } "The Date Fields with a limited year input box" should { "Display months using long nouns" in { val dateFieldsInline = new DateFieldsInline(new Dropdown()) val doc = jsoupDocument(dateFieldsInline(dummyForm, "dummyField", Html("label"), 1 to 2, None)) months.zipWithIndex.foreach { case (month: String, i: Int) => doc.getElementById(s"dummyField.month-${i + 1}").text shouldBe month } } } }
Example 38
Source File: MandatoryIfExists.scala From play-conditional-form-mapping with Apache License 2.0 | 5 votes |
package uk.gov.voa.play.form import org.scalatest.{Matchers, FlatSpec} import play.api.data.Form import play.api.data.Forms._ class MandatoryIfExists extends FlatSpec with Matchers { import ConditionalMappings._ behavior of "mandatory if exists" it should "mandate the target field if any value for the source field is supplied" in { val data = Map("source" -> "anyrandomvalue") val res = form.bind(data) assert(res.errors.head.key === "target") } it should "not mandate the target field is no value for the source field is supplied" in { val res = form.bind(Map.empty[String, String]) assert(res.errors.isEmpty) } lazy val form = Form(mapping( "source" -> optional(nonEmptyText), "target" -> mandatoryIfExists("source", nonEmptyText) )(Model.apply)(Model.unapply)) case class Model(source: Option[String], target: Option[String]) }
Example 39
Source File: SignUpForm.scala From silhouette-vuejs-app with Apache License 2.0 | 5 votes |
package forms import models.services.CredentialsSingUpData import play.api.data.Form import play.api.data.Forms._ val form = Form( mapping( "firstName" -> nonEmptyText, "lastName" -> nonEmptyText, "email" -> email, "password" -> nonEmptyText, "captchaResponse" -> nonEmptyText )(CredentialsSingUpData.apply)(CredentialsSingUpData.unapply) ) }
Example 40
Source File: ProblemWithPlayFrameworkMappings.scala From play-conditional-form-mapping with Apache License 2.0 | 5 votes |
package uk.gov.voa.play.form import org.scalatest.{Matchers, FlatSpec} import play.api.data.Form import play.api.data.Forms._ class ProblemWithPlayFrameworkMappings extends FlatSpec with Matchers { behavior of "vanilla play conditional mapping" it should "not contain an error for the conditional validation when there is a field-level error" in { val data = Map("nonUkResident" -> "true") val res = form.bind(data) assert(res.errors.length == 1) assert(res.errors.head.key === "email") } it should "not allow an field-level error message for a conditional validation" in { val data = Map("nonUkResident" -> "true", "email" -> "[email protected]") val res = form.bind(data) assert(res.errors.length == 1) assert(res.errors.head.key === "") } lazy val form = Form(mapping( "nonUkResident" -> boolean, "country" -> optional(nonEmptyText), "email" -> nonEmptyText )(Model.apply)(Model.unapply).verifying("Error.countryRequired", x => x.nonUkResident && x.country.isDefined)) } case class Model(nonUkResident: Boolean, country: Option[String], email: String) class SolutionUsingConditionalMappings extends FlatSpec with Matchers { import ConditionalMappings._ behavior of "conditional mappings" it should "contain a field level errors for the field and conditional mappings" in { val data = Map("nonUkResident" -> "true") val res = form.bind(data) assert(res.errors.length == 2) assert(res.errors.head.key === "country") assert(res.errors.tail.head.key === "email") } lazy val form = Form(mapping( "nonUkResident" -> boolean, "country" -> mandatoryIfTrue("nonUkResident", nonEmptyText), "email" -> nonEmptyText )(Model.apply)(Model.unapply)) }
Example 41
Source File: onlyIfAny.scala From play-conditional-form-mapping with Apache License 2.0 | 5 votes |
package uk.gov.voa.play.form import org.scalatest.{FlatSpec, Matchers} import org.scalatest.OptionValues._ import play.api.data.Form import play.api.data.Forms._ class onlyIfAny extends FlatSpec with Matchers { import ConditionalMappings._ behavior of "only if any" it should "apply the mapping to the target field if any of the source fields have their required value" in { val data = Map("s1" -> "abc", "s2" -> "abc", "s3" -> "abc", "target" -> "magic") Seq("s1", "s2", "s3") foreach { f => val d = data.updated(f, "magicValue") val res = form.bind(d) assert(res.value.value.target.value === "magic") } } it should "not apply the mapping to the target field neither of the source fields have the required value" in { val data = Map("s1" -> "abc", "s2" -> "abc", "s3" -> "abc", "target" -> "magic") val res = form.bind(data) assert(res.value.value.target === None) } lazy val form = Form(mapping( "s1" -> nonEmptyText, "s2" -> nonEmptyText, "s3" -> nonEmptyText, "target" -> onlyIfAny(Seq("s1" -> "magicValue", "s2" -> "magicValue", "s3" -> "magicValue"), optional(nonEmptyText)) )(Model.apply)(Model.unapply)) case class Model(s1: String, s2: String, s3: String, target: Option[String]) }
Example 42
Source File: MandatoryIfNot.scala From play-conditional-form-mapping with Apache License 2.0 | 5 votes |
package uk.gov.voa.play.form import org.scalatest.{Matchers, FlatSpec} import play.api.data.Form import play.api.data.Forms._ class MandatoryIfNot extends FlatSpec with Matchers { import ConditionalMappings._ it should "mandate the target field if the source field DOES not match the specified value" in { val data = Map("source" -> "NotTheMagicValue") val res = form.bind(data) assert(res.errors.head.key === "target") } it should "not mandate the target field if the source field DOES NOT match the specified value" in { val data = Map("source" -> "magicValue") val res = form.bind(data) assert(res.errors.isEmpty) } lazy val form = Form(mapping( "source" -> nonEmptyText, "target" -> mandatoryIfNot("source", "magicValue", nonEmptyText) )(Model.apply)(Model.unapply)) case class Model(source: String, target: Option[String]) }
Example 43
Source File: MandatoryIfAnyAreTrue.scala From play-conditional-form-mapping with Apache License 2.0 | 5 votes |
package uk.gov.voa.play.form import org.scalatest.{Matchers, FlatSpec} import play.api.data.Form import play.api.data.Forms._ class MandatoryIfAnyAreTrue extends FlatSpec with Matchers { import ConditionalMappings._ behavior of "mandatory if any are true" it should "mandate the target field if any of the source fields are true" in { Seq("f1", "f2", "f3") foreach { f => val data = Map(f -> "true") val res = form.bind(data) assert(res.errors.head.key === "target") } } it should "not mandate the target field if neither of the source fields are true" in { val res = form.bind(Map.empty[String, String]) assert(res.errors.isEmpty) } lazy val form = Form(mapping( "f1" -> boolean, "f2" -> boolean, "f3" -> boolean, "target" -> mandatoryIfAnyAreTrue(Seq("f1", "f2", "f3"), nonEmptyText) )(Model.apply)(Model.unapply)) case class Model(f1: Boolean, f2: Boolean, f3: Boolean, target: Option[String]) }
Example 44
Source File: ChainedConditions.scala From play-conditional-form-mapping with Apache License 2.0 | 5 votes |
package uk.gov.voa.play.form import org.scalatest.{Matchers, FlatSpec} import play.api.data.Form import play.api.data.Forms._ class ChainedConditions extends FlatSpec with Matchers { import ConditionalMappings._ behavior of "chained mappings" it should "apply mappings if all of the chained criteria are satisfied" in { val data = Map("name" -> "Francoise", "age" -> "21") val res = form.bind(data) assert(res.errors.head.key === "favouriteColour") } it should "not apply mappings if any part of the chained critieria is not satisfied" in { val data = Map("name" -> "Francoise", "age" -> "20") val res = form.bind(data) assert(res.errors.isEmpty) } lazy val form = Form(mapping( "name" -> nonEmptyText, "age" -> number, "favouriteColour" -> mandatoryIf( isEqual("name", "Francoise") and isEqual("age", "21"), nonEmptyText ) )(Model.apply)(Model.unapply)) case class Model(name: String, age: Int, favouriteColour: Option[String]) }
Example 45
Source File: MandatoryIfFalse.scala From play-conditional-form-mapping with Apache License 2.0 | 5 votes |
package uk.gov.voa.play.form import org.scalatest.{Matchers, FlatSpec} import play.api.data.Form import play.api.data.Forms._ class MandatoryIfFalse extends FlatSpec with Matchers { import ConditionalMappings._ behavior of "mandatory if false" it should "mandate the target field if the source field is false, with field-level errors" in { val data = Map("source" -> "false") val res = form.bind(data) assert(res.errors.head.key === "target") } it should "not mandate the target field if the source field is not false" in { val res = form.bind(Map.empty[String, String]) assert(res.errors.isEmpty) } lazy val form = Form(mapping( "source" -> boolean, "target" -> mandatoryIfFalse("source", nonEmptyText) )(Model.apply)(Model.unapply)) case class Model(source: Boolean, target: Option[String]) }
Example 46
Source File: OnlyIfTrue.scala From play-conditional-form-mapping with Apache License 2.0 | 5 votes |
package uk.gov.voa.play.form import org.scalatest.{Matchers, FlatSpec} import org.scalatest.OptionValues._ import play.api.data.Form import play.api.data.Forms._ class OnlyIfTrue extends FlatSpec with Matchers { import ConditionalMappings._ behavior of "isTrue and isFalse" it should "apply a mapping with value TRUE is string in a case-variation of TRUE" in { isTrue("source")(Map("source" -> "true")) should be(true) isTrue("source")(Map("source" -> "TRUE")) should be(true) isTrue("source")(Map("source" -> "TRuE")) should be(true) } it should "apply a mapping with value FALSE is string in a case-variation of FALSE" in { isTrue("source")(Map("source" -> "false")) should be(false) isTrue("source")(Map("source" -> "FALSE")) should be(false) isTrue("source")(Map("source" -> "fAlSe")) should be(false) } it should "apply a mapping with value TRUE is string in a case-variation of FALSE" in { isFalse("source")(Map("source" -> "false")) should be(true) isFalse("source")(Map("source" -> "FALSE")) should be(true) isFalse("source")(Map("source" -> "fAlSe")) should be(true) } it should "apply a mapping with value FALSE is string in a case-variation of TRUE" in { isFalse("source")(Map("source" -> "true")) should be(false) isFalse("source")(Map("source" -> "TRUE")) should be(false) isFalse("source")(Map("source" -> "TRuE")) should be(false) } it should "apply a mapping with value FALSE is string in not a case-variation of TRUE or FALSE" in { isTrue("source")(Map("source" -> "non-sensical")) should be(false) isFalse("source")(Map("source" -> "non-sensical")) should be(false) } behavior of "only if true" it should "apply the mapping to the target field if the source field is true" in { val data = Map("source" -> "true", "target" -> "Bonjour") val res = form.bind(data) assert(res.value.value === Model(true, Some("Bonjour"))) } it should "ignore the mapping and set the default value if the source field is not true" in { val data = Map("source" -> "false", "target" -> "Bonjour") val res = form.bind(data) assert(res.value.value === Model(false, None)) } it should "not mandate the target field even if the source field is true" in { val data = Map("source" -> "true") val res = form.bind(data) assert(res.errors.isEmpty) } lazy val form = Form(mapping( "source" -> boolean, "target" -> onlyIfTrue("source", optional(nonEmptyText)) )(Model.apply)(Model.unapply)) case class Model(source: Boolean, target: Option[String]) }
Example 47
Source File: MandatoryIfAllEqual.scala From play-conditional-form-mapping with Apache License 2.0 | 5 votes |
package uk.gov.voa.play.form import org.scalatest.{Matchers, FlatSpec} import play.api.data.Form import play.api.data.Forms._ class MandatoryIfAllEqual extends FlatSpec with Matchers { import ConditionalMappings._ behavior of "mandatory if all equal" it should "mandate the target field if all of the source fields match their required value" in { val data = Map("s1" -> "s1val", "s2" -> "s2val", "s3" -> "s3val") val res = form.bind(data) assert(res.errors.head.key === "target") } it should "not mandate the target fields if any of the source fields do not match their required value" in { val data = Map("s1" -> "s1val", "s2" -> "s2val", "s3" -> "s3val") Seq("s1", "s2", "s3") foreach { f => val data2 = data.updated(f, "notrequiredvalue") val res = form.bind(data2) assert(res.errors.isEmpty) } } lazy val form = Form(mapping( "s1" -> nonEmptyText, "s2" -> nonEmptyText, "s3" -> nonEmptyText, "target" -> mandatoryIfAllEqual(Seq("s1" -> "s1val", "s2" -> "s2val", "s3" -> "s3val"), nonEmptyText) )(Model.apply)(Model.unapply)) case class Model(s1: String, s2: String, s3: String, target: Option[String]) }
Example 48
Source File: MandatoryIfEqual.scala From play-conditional-form-mapping with Apache License 2.0 | 5 votes |
package uk.gov.voa.play.form import org.scalatest.{Matchers, FlatSpec} import play.api.data.Form import play.api.data.Forms._ class MandatoryIfEqual extends FlatSpec with Matchers { import ConditionalMappings._ behavior of "mandatory if equal" it should "mandate the target field if the source has the required value" in { val data = Map("country" -> "England") val res = form.bind(data) assert(res.errors.head.key === "town") } it should "not mandate the target field if the source field does not have the required value" in { val data = Map("country" -> "Scotland") val res = form.bind(data) assert(res.errors.isEmpty) } lazy val form = Form(mapping( "country" -> nonEmptyText, "town" -> mandatoryIfEqual("country", "England", nonEmptyText) )(Model.apply)(Model.unapply)) case class Model(country: String, town: Option[String]) }