/
Form.scala
918 lines (800 loc) · 27.1 KB
/
Form.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
package play.api.data
import scala.language.existentials
import format._
import validation._
/**
* Helper to manage HTML form description, submission and validation.
*
* For example, a form handling a `User` case class submission:
* {{{
* import play.api.data._
* import play.api.data.format.Formats._
*
* val userForm = Form(
* mapping(
* "name" -> of[String],
* "age" -> of[Int],
* "email" -> of[String]
* )(User.apply)(User.unapply)
* )
* }}}
*
* @tparam T the type managed by this form
* @param mapping the form mapping, which describes all form fields
* @param data the current form data, used to display the form
* @param errors the collection of errors associated with this form
* @param value a concrete value of type `T` if the form submission was successful
*/
case class Form[T](mapping: Mapping[T], data: Map[String, String], errors: Seq[FormError], value: Option[T]) {
/**
* Constraints associated with this form, indexed by field name.
*/
val constraints: Map[String, Seq[(String, Seq[Any])]] = mapping.mappings.map { m =>
m.key -> m.constraints.collect { case Constraint(Some(name), args) => name -> args }
}.filterNot(_._2.isEmpty).toMap
/**
* Formats associated to this form, indexed by field name. *
*/
val formats: Map[String, (String, Seq[Any])] = mapping.mappings.map { m =>
m.key -> m.format
}.collect {
case (k, Some(f)) => k -> f
}.toMap
/**
* Binds data to this form, i.e. handles form submission.
*
* @param data the data to submit
* @return a copy of this form, filled with the new data
*/
def bind(data: Map[String, String]): Form[T] = mapping.bind(data).fold(
errors => this.copy(data = data, errors = errors, value = None),
value => this.copy(data = data, errors = Nil, value = Some(value)))
/**
* Binds data to this form, i.e. handles form submission.
*
* @param data Json data to submit
* @return a copy of this form, filled with the new data
*/
def bind(data: play.api.libs.json.JsValue): Form[T] = bind(FormUtils.fromJson(js = data))
/**
* Binds request data to this form, i.e. handles form submission.
*
* @return a copy of this form filled with the new data
*/
def bindFromRequest()(implicit request: play.api.mvc.Request[_]): Form[T] = {
bindFromRequest {
(request.body match {
case body: play.api.mvc.AnyContent if body.asFormUrlEncoded.isDefined => body.asFormUrlEncoded.get
case body: play.api.mvc.AnyContent if body.asMultipartFormData.isDefined => body.asMultipartFormData.get.asFormUrlEncoded
case body: play.api.mvc.AnyContent if body.asJson.isDefined => FormUtils.fromJson(js = body.asJson.get).mapValues(Seq(_))
case body: Map[_, _] => body.asInstanceOf[Map[String, Seq[String]]]
case body: play.api.mvc.MultipartFormData[_] => body.asFormUrlEncoded
case body: play.api.libs.json.JsValue => FormUtils.fromJson(js = body).mapValues(Seq(_))
case _ => Map.empty[String, Seq[String]]
}) ++ request.queryString
}
}
def bindFromRequest(data: Map[String, Seq[String]]): Form[T] = {
bind {
data.foldLeft(Map.empty[String,String]) {
case (s, (key, values)) if key.endsWith("[]")=> s ++ values.zipWithIndex.map { case (v,i) => (key.dropRight(2) + "[" + i + "]") -> v }
case (s, (key, values)) => s + (key -> values.headOption.getOrElse(""))
}
}
}
/**
* Fills this form with a existing value, used for edit forms.
*
* @param value an existing value of type `T`, used to fill this form
* @return a copy of this form filled with the new data
*/
def fill(value: T): Form[T] = {
val result = mapping.unbind(value)
this.copy(data = result._1, value = Some(value))
}
/**
* Fills this form with a existing value, and performs a validation.
*
* @param value an existing value of type `T`, used to fill this form
* @return a copy of this form filled with the new data
*/
def fillAndValidate(value: T): Form[T] = {
val result = mapping.unbind(value)
this.copy(data = result._1, errors = result._2, value = Some(value))
}
/**
* Handles form results. Either the form has errors, or the submission was a success and a
* concrete value is available.
*
* For example:
* {{{
* anyForm.bindFromRequest().fold(
* f => redisplayForm(f),
* t => handleValidFormSubmission(t)
* )
* }}}
*
* @tparam R common result type
* @param hasErrors a function to handle forms with errors
* @param success a function to handle form submission success
* @return a result `R`.
*/
def fold[R](hasErrors: Form[T] => R, success: T => R): R = value.map(success(_)).getOrElse(hasErrors(this))
/**
* Retrieves a field.
*
* For example:
* {{{
* val usernameField = userForm("username")
* }}}
*
* @param key the field name
* @return the field, returned even if the field does not exist
*/
def apply(key: String): Field = Field(
this,
key,
constraints.get(key).getOrElse(Nil),
formats.get(key),
errors.collect { case e if e.key == key => e },
data.get(key))
/**
* Retrieves the first global error, if it exists, i.e. an error without any key.
*
* @return an error
*/
def globalError: Option[FormError] = globalErrors.headOption
/**
* Retrieves all global errors, i.e. errors without a key.
*
* @return all global errors
*/
def globalErrors: Seq[FormError] = errors.filter(_.key.isEmpty)
/**
* Applies a function for a field.
*
* For example:
* {{{
* userForm.forField("username") { field =>
* <input type="text" name={field.name} value={field.value.getOrElse("")} />
* }
* }}}
*
* @tparam R result type
* @param key field name
* @param handler field handler (transform the field to `R`)
*/
def forField[R](key: String)(handler: Field => R): R = handler(this(key))
/**
* Returns `true` if there is an error related to this form.
*/
def hasErrors: Boolean = !errors.isEmpty
/**
* Retrieve the first error for this key.
*
* @param key field name.
*/
def error(key: String): Option[FormError] = errors.find(_.key == key)
/**
* Retrieve all errors for this key.
*
* @param key field name.
*/
def errors(key: String): Seq[FormError] = errors.filter(_.key == key)
/**
* Returns `true` if there is a global error related to this form.
*/
def hasGlobalErrors: Boolean = !globalErrors.isEmpty
/**
* Returns the concrete value, if the submission was a success.
*
* Note that this method fails with an Exception if this form as errors.
*/
def get: T = value.get
/**
* Returns the form errors serialized as Json.
*/
def errorsAsJson(implicit lang: play.api.i18n.Lang): play.api.libs.json.JsValue = {
import play.api.libs.json._
Json.toJson(
errors.groupBy(_.key).mapValues { errors =>
errors.map(e => play.api.i18n.Messages(e.message, e.args: _*))
}
)
}
/**
* Adds an error to this form
* @param error Error to add
* @return a copy of this form with the added error
*/
def withError(error: FormError): Form[T] = this.copy(errors = errors :+ error, value = None)
/**
* Convenient overloaded method adding an error to this form
* @param key Key of the field having the error
* @param message Error message
* @param args Error message arguments
* @return a copy of this form with the added error
*/
def withError(key: String, message: String, args: Any*): Form[T] = withError(FormError(key, message, args))
/**
* Adds a global error to this form
* @param message Error message
* @param args Error message arguments
* @return a copy of this form with the added global error
*/
def withGlobalError(message: String, args: Any*): Form[T] = withError(FormError("", message, args))
/**
* Discards this form’s errors
* @return a copy of this form without errors
*/
def discardingErrors: Form[T] = this.copy(errors = Seq.empty)
}
/**
* A form field.
*
* @param name the field name
* @param constraints the constraints associated with the field
* @param format the format expected for this field
* @param errors the errors associated to this field
* @param value the field value, if any
*/
case class Field(private val form: Form[_], name: String, constraints: Seq[(String, Seq[Any])], format: Option[(String, Seq[Any])], errors: Seq[FormError], value: Option[String]) {
/**
* The field ID - the same as the field name but with '.' replaced by '_'.
*/
lazy val id: String = name.replace('.', '_').replace('[', '_').replace(']', '_')
/**
* Returns the first error associated with this field, if it exists.
*
* @return an error
*/
lazy val error: Option[FormError] = errors.headOption
/**
* Check if this field has errors.
*/
lazy val hasErrors: Boolean = !errors.isEmpty
/**
* Retrieve a field from the same form, using a key relative to this field key.
*
* @param key Relative key.
*/
def apply(key: String): Field = {
form(Option(name).filterNot(_.isEmpty).map(_ + (if (key(0) == '[') "" else ".")).getOrElse("") + key)
}
/**
* Retrieve available indexes defined for this field (if this field is repeated).
*/
lazy val indexes: Seq[Int] = {
RepeatedMapping.indexes(name, form.data)
}
}
/**
* Provides a set of operations for creating `Form` values.
*/
object Form {
/**
* Creates a new form from a mapping.
*
* For example:
* {{{
* import play.api.data._
* import format.Formats._
*
* val userForm = Form(
* tuple(
* "name" -> of[String],
* "age" -> of[Int],
* "email" -> of[String]
* )
* )
* }}}
*
* @param mapping the form mapping
* @return a form definition
*/
def apply[T](mapping: Mapping[T]): Form[T] = Form(mapping, Map.empty, Nil, None)
/**
* Creates a new form from a mapping, with a root key.
*
* For example:
* {{{
* val userForm = Form(
* "user" -> tuple(
* "name" -> of[String],
* "age" -> of[Int],
* "email" -> of[String]
* )
* )
* }}}
*
* @param mapping the root key, form mapping association
* @return a form definition
*/
def apply[T](mapping: (String, Mapping[T])): Form[T] = Form(mapping._2.withPrefix(mapping._1), Map.empty, Nil, None)
}
private[data] object FormUtils {
import play.api.libs.json._
def fromJson(prefix: String = "", js: JsValue): Map[String, String] = js match {
case JsObject(fields) => {
fields.map { case (key, value) => fromJson(Option(prefix).filterNot(_.isEmpty).map(_ + ".").getOrElse("") + key, value) }.foldLeft(Map.empty[String, String])(_ ++ _)
}
case JsArray(values) => {
values.zipWithIndex.map { case (value, i) => fromJson(prefix + "[" + i + "]", value) }.foldLeft(Map.empty[String, String])(_ ++ _)
}
case JsNull => Map.empty
case JsUndefined(_) => Map.empty
case JsBoolean(value) => Map(prefix -> value.toString)
case JsNumber(value) => Map(prefix -> value.toString)
case JsString(value) => Map(prefix -> value.toString)
}
}
/**
* A form error.
*
* @param key The error key (should be associated with a field using the same key).
* @param message The form message (often a simple message key needing to be translated).
* @param args Arguments used to format the message.
*/
case class FormError(key: String, message: String, args: Seq[Any] = Nil) {
/**
* Copy this error with a new Message.
*
* @param message The new message.
*/
def withMessage(message: String): FormError = FormError(key, message)
}
/**
* A mapping is a two-way binder to handle a form field.
*/
trait Mapping[T] {
self =>
/**
* The field key.
*/
val key: String
/**
* Sub-mappings (these can be seen as sub-keys).
*/
val mappings: Seq[Mapping[_]]
/**
* The Format expected for this field, if it exists.
*/
val format: Option[(String, Seq[Any])] = None
/**
* The constraints associated with this field.
*/
val constraints: Seq[Constraint[T]]
/**
* Binds this field, i.e. construct a concrete value from submitted data.
*
* @param data the submitted data
* @return either a concrete value of type `T` or a set of errors, if the binding failed
*/
def bind(data: Map[String, String]): Either[Seq[FormError], T]
/**
* Unbinds this field, i.e. transforms a concrete value to plain data.
*
* @param value the value to unbind
* @return either the plain data or a set of errors, if the unbinding failed
*/
def unbind(value: T): (Map[String, String], Seq[FormError])
/**
* Constructs a new Mapping based on this one, adding a prefix to the key.
*
* @param prefix the prefix to add to the key
* @return the same mapping, with only the key changed
*/
def withPrefix(prefix: String): Mapping[T]
/**
* Constructs a new Mapping based on this one, by adding new constraints.
*
* For example:
* {{{
* import play.api.data._
* import validation.Constraints._
*
* Form("phonenumber" -> text.verifying(required) )
* }}}
*
* @param constraints the constraints to add
* @return the new mapping
*/
def verifying(constraints: Constraint[T]*): Mapping[T]
/**
* Constructs a new Mapping based on this one, by adding a new ad-hoc constraint.
*
* For example:
* {{{
* import play.api.data._
* import validation.Constraints._
*
* Form("phonenumber" -> text.verifying {_.grouped(2).size == 5})
* }}}
*
* @param constraint a function describing the constraint that returns `false` on failure
* @return the new mapping
*/
def verifying(constraint: (T => Boolean)): Mapping[T] = verifying("error.unknown", constraint)
/**
* Constructs a new Mapping based on this one, by adding a new ad-hoc constraint.
*
* For example:
* {{{
* import play.api.data._
* import validation.Constraints._
*
* Form("phonenumber" -> text.verifying("Bad phone number", {_.grouped(2).size == 5}))
* }}}
*
* @param error The error message used if the constraint fails
* @param constraint a function describing the constraint that returns `false` on failure
* @return the new mapping
*/
def verifying(error: => String, constraint: (T => Boolean)): Mapping[T] = {
verifying(Constraint { t: T =>
if (constraint(t)) Valid else Invalid(Seq(ValidationError(error)))
})
}
/**
* Transform this Mapping[T] to a Mapping[B].
*
* @tparam B The type of the new mapping.
* @param f1 Transform value of T to a value of B
* @param f2 Transform value of B to a value of T
*/
def transform[B](f1: T => B, f2: B => T): Mapping[B] = WrappedMapping(this, f1, f2)
// Internal utilities
protected def addPrefix(prefix: String) = {
Option(prefix).filterNot(_.isEmpty).map(p => p + Option(key).filterNot(_.isEmpty).map("." + _).getOrElse(""))
}
protected def applyConstraints(t: T): Either[Seq[FormError], T] = {
Right(t).right.flatMap { v =>
Option(collectErrors(v)).filterNot(_.isEmpty).toLeft(v)
}
}
protected def collectErrors(t: T): Seq[FormError] = {
constraints.map(_(t)).collect {
case Invalid(errors) => errors.toSeq
}.flatten.map(ve => FormError(key, ve.message, ve.args))
}
}
/**
* A mapping wrapping another existing mapping with transformation functions.
*
* @param wrapped Existing wrapped mapping
* @param f1 Transformation function from A to B
* @param f2 Transformation function from B to A
* @param additionalConstraints Additional constraints of type B
*/
case class WrappedMapping[A, B](wrapped: Mapping[A], f1: A => B, f2: B => A, val additionalConstraints: Seq[Constraint[B]] = Nil) extends Mapping[B] {
/**
* The field key.
*/
val key = wrapped.key
/**
* Sub-mappings (these can be seen as sub-keys).
*/
val mappings = wrapped.mappings
/**
* The Format expected for this field, if it exists.
*/
override val format = wrapped.format
/**
* The constraints associated with this field.
*/
val constraints: Seq[Constraint[B]] = wrapped.constraints.map { constraintOfT =>
Constraint[B](constraintOfT.name, constraintOfT.args) { b =>
constraintOfT(f2(b))
}
} ++ additionalConstraints
/**
* Binds this field, i.e. construct a concrete value from submitted data.
*
* @param data the submitted data
* @return either a concrete value of type `B` or a set of errors, if the binding failed
*/
def bind(data: Map[String, String]): Either[Seq[FormError], B] = {
wrapped.bind(data).right.map(t => f1(t)).right.flatMap(applyConstraints)
}
/**
* Unbinds this field, i.e. transforms a concrete value to plain data.
*
* @param value the value to unbind
* @return either the plain data or a set of errors, if the unbinding failed
*/
def unbind(value: B): (Map[String, String], Seq[FormError]) = {
(wrapped.unbind(f2(value))._1, collectErrors(value))
}
/**
* Constructs a new Mapping based on this one, adding a prefix to the key.
*
* @param prefix the prefix to add to the key
* @return the same mapping, with only the key changed
*/
def withPrefix(prefix: String): Mapping[B] = {
copy(wrapped = wrapped.withPrefix(prefix))
}
/**
* Constructs a new Mapping based on this one, by adding new constraints.
*
* For example:
* {{{
* import play.api.data._
* import validation.Constraints._
*
* Form("phonenumber" -> text.verifying(required) )
* }}}
*
* @param constraints the constraints to add
* @return the new mapping
*/
def verifying(constraints: Constraint[B]*): Mapping[B] = copy(additionalConstraints = additionalConstraints ++ constraints)
}
/**
* Provides a set of operations related to `RepeatedMapping` values.
*/
object RepeatedMapping {
/**
* Computes the available indexes for the given key in this set of data.
*/
def indexes(key: String, data: Map[String, String]): Seq[Int] = {
val KeyPattern = ("^" + java.util.regex.Pattern.quote(key) + """\[(\d+)\].*$""").r
data.toSeq.collect { case (KeyPattern(index), _) => index.toInt }.sorted.distinct
}
}
/**
* A mapping for repeated elements.
*
* @param wrapped The wrapped mapping
*/
case class RepeatedMapping[T](wrapped: Mapping[T], val key: String = "", val constraints: Seq[Constraint[List[T]]] = Nil) extends Mapping[List[T]] {
/**
* The Format expected for this field, if it exists.
*/
override val format: Option[(String, Seq[Any])] = wrapped.format
/**
* Constructs a new Mapping based on this one, by adding new constraints.
*
* For example:
* {{{
* import play.api.data._
* import validation.Constraints._
*
* Form("phonenumber" -> text.verifying(required) )
* }}}
*
* @param constraints the constraints to add
* @return the new mapping
*/
def verifying(addConstraints: Constraint[List[T]]*): Mapping[List[T]] = {
this.copy(constraints = constraints ++ addConstraints.toSeq)
}
/**
* Binds this field, i.e. construct a concrete value from submitted data.
*
* @param data the submitted data
* @return either a concrete value of type `List[T]` or a set of errors, if the binding failed
*/
def bind(data: Map[String, String]): Either[Seq[FormError], List[T]] = {
val allErrorsOrItems: Seq[Either[Seq[FormError], T]] = RepeatedMapping.indexes(key, data).map(i => wrapped.withPrefix(key + "[" + i + "]").bind(data))
if (allErrorsOrItems.forall(_.isRight)) {
Right(allErrorsOrItems.map(_.right.get).toList).right.flatMap(applyConstraints)
} else {
Left(allErrorsOrItems.collect { case Left(errors) => errors }.flatten)
}
}
/**
* Unbinds this field, i.e. transforms a concrete value to plain data.
*
* @param value the value to unbind
* @return either the plain data or a set of errors, if the unbinding failed
*/
def unbind(value: List[T]): (Map[String, String], Seq[FormError]) = {
val (datas, errors) = value.zipWithIndex.map { case (t, i) => wrapped.withPrefix(key + "[" + i + "]").unbind(t) }.unzip
(datas.foldLeft(Map.empty[String, String])(_ ++ _), errors.flatten ++ collectErrors(value))
}
/**
* Constructs a new Mapping based on this one, adding a prefix to the key.
*
* @param prefix the prefix to add to the key
* @return the same mapping, with only the key changed
*/
def withPrefix(prefix: String): Mapping[List[T]] = {
addPrefix(prefix).map(newKey => this.copy(key = newKey)).getOrElse(this)
}
/**
* Sub-mappings (these can be seen as sub-keys).
*/
val mappings: Seq[Mapping[_]] = wrapped.mappings
}
/**
* A mapping for optional elements
*
* @param wrapped the wrapped mapping
*/
case class OptionalMapping[T](wrapped: Mapping[T], val constraints: Seq[Constraint[Option[T]]] = Nil) extends Mapping[Option[T]] {
override val format: Option[(String, Seq[Any])] = wrapped.format
/**
* The field key.
*/
val key = wrapped.key
/**
* Constructs a new Mapping based on this one, by adding new constraints.
*
* For example:
* {{{
* import play.api.data._
* import validation.Constraints._
*
* Form("phonenumber" -> text.verifying(required) )
* }}}
*
* @param constraints the constraints to add
* @return the new mapping
*/
def verifying(addConstraints: Constraint[Option[T]]*): Mapping[Option[T]] = {
this.copy(constraints = constraints ++ addConstraints.toSeq)
}
/**
* Binds this field, i.e. constructs a concrete value from submitted data.
*
* @param data the submitted data
* @return either a concrete value of type `T` or a set of error if the binding failed
*/
def bind(data: Map[String, String]): Either[Seq[FormError], Option[T]] = {
data.keys.filter(p => p == key || p.startsWith(key + ".") || p.startsWith(key + "[")).map(k => data.get(k).filterNot(_.isEmpty)).collect { case Some(v) => v }.headOption.map { _ =>
wrapped.bind(data).right.map(Some(_))
}.getOrElse {
Right(None)
}.right.flatMap(applyConstraints)
}
/**
* Unbinds this field, i.e. transforms a concrete value to plain data.
*
* @param value The value to unbind.
* @return Either the plain data or a set of error if the unbinding failed.
*/
def unbind(value: Option[T]): (Map[String, String], Seq[FormError]) = {
val errors = collectErrors(value)
value.map(wrapped.unbind(_)).map(r => r._1 -> (r._2 ++ errors)).getOrElse(Map.empty -> errors)
}
/**
* Constructs a new Mapping based on this one, adding a prefix to the key.
*
* @param prefix the prefix to add to the key
* @return the same mapping, with only the key changed
*/
def withPrefix(prefix: String): Mapping[Option[T]] = {
copy(wrapped = wrapped.withPrefix(prefix))
}
/** Sub-mappings (these can be seen as sub-keys). */
val mappings: Seq[Mapping[_]] = wrapped.mappings
}
/**
* A mapping for a single field.
*
* @param key the field key
* @param constraints the constraints associated with this field.
*/
case class FieldMapping[T](val key: String = "", val constraints: Seq[Constraint[T]] = Nil)(implicit val binder: Formatter[T]) extends Mapping[T] {
/**
* The Format expected for this field, if it exists.
*/
override val format: Option[(String, Seq[Any])] = binder.format
/**
* Constructs a new Mapping based on this one, by adding new constraints.
*
* For example:
* {{{
* import play.api.data._
* import validation.Constraints._
*
* Form("phonenumber" -> text.verifying(required) )
* }}}
*
* @param constraints the constraints to add
* @return the new mapping
*/
def verifying(addConstraints: Constraint[T]*): Mapping[T] = {
this.copy(constraints = constraints ++ addConstraints.toSeq)
}
/**
* Changes the binder used to handle this field.
*
* @param binder the new binder to use
* @return the same mapping with a new binder
*/
def as(binder: Formatter[T]): Mapping[T] = {
this.copy()(binder)
}
/**
* Binds this field, i.e. constructs a concrete value from submitted data.
*
* @param data the submitted data
* @return either a concrete value of type `T` or a set of errors, if binding failed
*/
def bind(data: Map[String, String]): Either[Seq[FormError], T] = {
binder.bind(key, data).right.flatMap { applyConstraints(_) }
}
/**
* Unbinds this field, i.e. transforms a concrete value to plain data.
*
* @param value the value to unbind
* @return either the plain data or a set of errors, if unbinding failed
*/
def unbind(value: T): (Map[String, String], Seq[FormError]) = {
binder.unbind(key, value) -> collectErrors(value)
}
/**
* Constructs a new Mapping based on this one, adding a prefix to the key.
*
* @param prefix the prefix to add to the key
* @return the same mapping, with only the key changed
*/
def withPrefix(prefix: String): Mapping[T] = {
addPrefix(prefix).map(newKey => this.copy(key = newKey)).getOrElse(this)
}
/** Sub-mappings (these can be seen as sub-keys). */
val mappings: Seq[Mapping[_]] = Seq(this)
}
/**
* Common helper methods for all object mappings - mappings including several fields.
*/
trait ObjectMapping {
/**
* Merges the result of two bindings.
*
* @see bind()
*/
def merge2(a: Either[Seq[FormError], Seq[Any]], b: Either[Seq[FormError], Seq[Any]]): Either[Seq[FormError], Seq[Any]] = (a, b) match {
case (Left(errorsA), Left(errorsB)) => Left(errorsA ++ errorsB)
case (Left(errorsA), Right(_)) => Left(errorsA)
case (Right(_), Left(errorsB)) => Left(errorsB)
case (Right(a), Right(b)) => Right(a ++ b)
}
/**
* Merges the result of multiple bindings.
*
* @see bind()
*/
def merge(results: Either[Seq[FormError], Any]*): Either[Seq[FormError], Seq[Any]] = {
val all: Seq[Either[Seq[FormError], Seq[Any]]] = results.map(_.right.map(Seq(_)))
all.fold(Right(Nil)) { (s, i) => merge2(s, i) }
}
}
/**
* Represents an object binding (ie. a binding for several fields).
*
* This is used for objects with one field. Other versions exist, e.g. `ObjectMapping2`, `ObjectMapping3`, etc.
*
* @tparam T the complex object type
* @tparam A the first field type
* @param apply a constructor function that creates a instance of `T` using field `A`
* @param fa a mapping for field `A`
* @param constraints constraints associated with this mapping
*/
case class ObjectMapping1[R, A1](apply: Function1[A1, R], unapply: Function1[R, Option[(A1)]], f1: (String, Mapping[A1]), val key: String = "", val constraints: Seq[Constraint[R]] = Nil) extends Mapping[R] with ObjectMapping {
val field1 = f1._2.withPrefix(f1._1).withPrefix(key)
def bind(data: Map[String, String]) = {
merge(field1.bind(data)) match {
case Left(errors) => Left(errors)
case Right(values) => {
applyConstraints(apply(
values(0).asInstanceOf[A1]))
}
}
}
def unbind(value: R) = {
unapply(value).map { fields =>
val (v1) = fields
val a1 = field1.unbind(v1)
(a1._1) ->
(a1._2)
}.getOrElse(Map.empty -> Seq(FormError(key, "unbind.failed")))
}
def withPrefix(prefix: String) = addPrefix(prefix).map(newKey => this.copy(key = newKey)).getOrElse(this)
def verifying(addConstraints: Constraint[R]*) = {
this.copy(constraints = constraints ++ addConstraints.toSeq)
}
val mappings = Seq(this) ++ field1.mappings
}