Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9299049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:28:15+00:00 2026-06-18T22:28:15+00:00

I’m using Constraints on my web forms and I’ve noticed that several forms have

  • 0

I’m using Constraints on my web forms and I’ve noticed that several forms have similar validations, for instance I have several types of form with a start date and an end date. In each case, I want to validate that the start date is before the end date. Here’s the case class I’m creating from my form:

   case class OrderSearchForm(orderId: Option[Int], startDate:Option[Long], endDate:Option[Long])

and my validation (Let’s ignore the .get() for now):

  def validateSearchDate = Constraint[OrderSearchForm]{ 
    osf: OrderSearchForm => {
      if (!osf.startDate.isEmpty && !osf.endDate.isEmpty && osf.startDate.get.compareTo(osf.endDate.get) > 0 )
        Invalid("Begin Date is after End Date.")
      else
        Valid
    }
  }

Now, since I have lots of forms with a start date and an end date, I’d like to re-write my validation to work with all of the case classes representing these forms. I’m wondering whether the typeclass pattern can help me with this:

trait TwoDates[T] { 
  def twoDatesTuple(t: T): (Option[Long], Option[Long]) 
}

trait TwoDatesOSF extends TwoDates[OrderSearchForm] { 
  def twoDatesTuple(t: OrderSearchForm) = (t.startDate, t.endDate) 
}

implicit object TwoDatesOSF extends trait TwoDatesOSF

def validateSearchDate = Constraint[TwoDates[_]] { t: TwoDates[_] => ... (as above)}

but applying does not work:

validateSearchDate(OrderSearchForm(None, None, None))

yields:

error: type mismatch; found : OrderSearchForm required:
TwoDates[_]
betweenDates(osf)

1) Can I write generic validations using typeclasses? If so, what am I doing wrong?

2) Can I write generic validations while AVOIDING using super-classes (i.e.

abstract class TwoDates(start: Option[Long], end:Option[Long])

case class OrderSearchForm(orderId: Option[String], startDate:Option[Long], endDate:Option[Long]) extends TwoDates(startDate, endDate) 

which seems awkward once multiple validations are in play)

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T22:28:16+00:00Added an answer on June 18, 2026 at 10:28 pm

    I think you can use structural types:

    private type TwoDates = { def startDate: Option[Date]; def endDate: Option[Date] }
    
    def validateTwoDates = Constraint[TwoDates] { osf: TwoDates =>
    if (!osf.startDate.isEmpty && 
        !osf.endDate.isEmpty && 
        osf.startDate.get.compareTo(osf.endDate.get) > 0) {
            Invalid("Begin Date is after End Date.")
        } else Valid
    }
    
    case class Something(
       startDate: Option[Date], 
       endDate: Option[Date], 
       name: String)
    
    private val form = Form(mapping(
       "startDate" -> optional(date),
       "endDate" -> optional(date),
       "name" -> text)
    (Something.apply)(Something.unapply).verifying(validateTwoDates))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am using JSon response to parse title,date content and thumbnail images and place
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.