<commit_before>package controllers

import javax.inject.Inject

import play.api._
import play.api.data._
import play.api.db.DB
import play.api.i18n.{MessagesApi, I18nSupport}
import play.api.mvc._
import play.api.data.Forms._
import play.api.Play.current
import anorm._

class Application extends Controller{



  def validate(email:String, password:String)={
    var result:Boolean=false

    DB.withConnection{
      implicit c=>
        val findUser=SQL("Select * from users where email={email} and password={password}").on("email"->email,"password"->password).apply()
        if(findUser!=Stream.empty) {
          result=true
        }
    }

    result
  }

  val loginForm = Form(
    tuple(
      "email"-> email,
      "password"-> nonEmptyText
    ) verifying("Failed to validate from DB", fields=>fields match {
      case loginData => validate(loginData._1,loginData._2)
    })
  )


  def login = Action {
    implicit request =>

    Ok(views.html.login())
  }

  def loginsubmit = Action{

    implicit request =>

      var result:Result=NoContent

    loginForm.bindFromRequest.fold(
      formWithErrors =>{
        var error="";
        formWithErrors.errors.foreach(
          e=> error = error+e.key+" "+e.message+", "
        )
        result=Redirect(routes.Application.login()).flashing("error"->error.dropRight(2))
      },
      loginData => {
        val session:Session=request.session+("email"->loginData._1)
        result=Redirect(routes.Home.dashboard()).withSession(session)
      }
    )
    result
  }

  def index = Action {
    Redirect(routes.Application.login())
  }



  def logout = Action{
    Redirect(routes.Application.login()).withNewSession
  }


}
<commit_msg>add Custom constraints during validation. enhance error message capture.<commit_after>package controllers

import java.util.regex.Pattern
import javax.inject.Inject

import play.api._
import play.api.data._
import play.api.data.validation._
import play.api.db.DB
import play.api.i18n.{MessagesApi, I18nSupport}
import play.api.mvc._
import play.api.data.Forms._
import play.api.Play.current
import anorm._

import scala.util.matching.Regex

class Application extends Controller{



  def validate(email:String, password:String)={
    var result:Boolean=false

    DB.withConnection{
      implicit c=>
        val findUser=SQL("Select * from users where email={email} and password={password}").on("email"->email,"password"->password).apply()
        if(findUser!=Stream.empty) {
          result=true
        }
    }
    result
  }

  val loginForm = Form(
    tuple(
      "email"-> text.verifying(FormConstraint.emailCheckConstraint),
      "password"-> text.verifying("Password required",!_.isEmpty)
    ) verifying("Login failed", fields=>fields match {
      case loginData => validate(loginData._1,loginData._2)
    })
  )


  def login = Action {
    implicit request =>
    Ok(views.html.login())
  }

  def loginsubmit = Action{
    implicit request =>
    loginForm.bindFromRequest.fold(
      formWithErrors =>{
        val error=formWithErrors.errors.foldLeft("")( (a,b)=>a+b.message+", " ).dropRight(2)
        Redirect(routes.Application.login()).flashing("error"->error)
      },
      loginData => {
        val session:Session=request.session+("email"->loginData._1)
        Redirect(routes.Home.dashboard()).withSession(session)
      }
    )
  }

  def index = Action {
    Redirect(routes.Application.login())
  }



  def logout = Action{
    Redirect(routes.Application.login()).withNewSession
  }


}

{'fraction_non_alphanumeric': 0.13080407701019253, 'fraction_numerical': 0.004530011325028313, 'mean_word_length': 3.0986078886310904, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1862172', 'n_tokens_mistral': 1091, 'n_tokens_neox': 1062, 'n_words': 202}