[scala-2.*] Is it possible to bindFromRequest but skip all validation?

180 views
Skip to first unread message

Artem Kozlov

unread,
Nov 15, 2012, 11:32:15 AM11/15/12
to play-framework
Hi,

I have a form with object mapping:

val form = Form(
mapping(
"a" -> nonEmptyText,
"b" -> nonEmptyText
)(A.apply)(A.unapply)
)

Is it possible to reconstruct A from request, using form mappings, but
skipping all validation?

Thanks in advance, Artem.

Julien Richard-Foy

unread,
Nov 15, 2012, 11:45:46 AM11/15/12
to play-fr...@googlegroups.com
You can’t do that directly. But if you’re using the 2.1 branch there
is a `Form#discadingErrors` method:
https://github.com/playframework/Play20/blob/c012847b1d0b719c9eef1a0753e1c68e1d6c789e/framework/src/play/src/main/scala/play/api/data/Form.scala#L257
Regards

Artem Kozlov

unread,
Nov 15, 2012, 12:57:27 PM11/15/12
to play-framework
Thanks, but it seems it is not possible at all. I need to map values
to case class by hand (again :( ).

I look in to sources and it seems it is not possible at all. In the
Form bind method:

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)))

value is always None if mapping bind was failed.

On 15 ноя, 18:45, Julien Richard-Foy <j...@zenexity.com> wrote:
> You can’t do that directly. But if you’re using the 2.1 branch there
> is a `Form#discadingErrors` method:https://github.com/playframework/Play20/blob/c012847b1d0b719c9eef1a07...
> Regards

Julien Richard-Foy

unread,
Nov 15, 2012, 2:57:57 PM11/15/12
to play-fr...@googlegroups.com
The idea is that the binding will fill your case class only if the
data are valid.

James Roper

unread,
Nov 15, 2012, 6:07:26 PM11/15/12
to play-fr...@googlegroups.com
Think of it this way, if you have a property that is an Int, and you skip validation, what is going to end up in your class if someone enters "this is not an integer"?  It doesn't make sense to skip validation.  The idea of validation is to ensure that what the user entered can be translated to your data model.  If it makes sense for your data model to be able to represent the user inputted data without validating it first, then possibly you should be rethinking whether you have the right data model, and whether you are doing the right validations at the right time.
Reply all
Reply to author
Forward
0 new messages