# Handling Nested forms

**URL:** https://discourse.dry-rb.org/t/handling-nested-forms/146
**Category:** dry-validation
**Created:** [November 1, 2016, 3:34pm UTC](https://discourse.dry-rb.org/t/handling-nested-forms/146 "2016-11-01T15:34:58Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![willian](https://avatars.discourse-cdn.com/v4/letter/w/ecd19e/32.png) [@willian](https://discourse.dry-rb.org/u/willian)
#### Post date: [November 1, 2016, 3:34pm UTC](https://discourse.dry-rb.org/t/handling-nested-forms/146/1 "2016-11-01T15:34:58Z")

</div>

This is my form:

```auto
Dry::Validation.Form do
  required(:id).filled(:int?)

  required(:data).schema do
    required(:id).filled(:int?)
  end
end

```

With `{ id: 1, data: { id: 2 }}` it works perfectly, but using the `{ id: '1', data: { id: '2' }}` it will coerce the `id` in the root but not the one in the `data` fragment. This makes sense since I’m using a nested schema, not a form. I tried (unsuccessfully):

```auto
Dry::Validation.Form do
  required(:id).filled(:int?)

  required(:data).form do
    required(:id).filled(:int?)
  end
end

```

My question is: how can I do a nested form (nested schema coercion + validation)?

---

<div class="post-metadata">

### Author: ![solnic](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/solnic/32/779_2.png) [@solnic](https://discourse.dry-rb.org/u/solnic)
#### Post date: [November 9, 2016, 9:22pm UTC](https://discourse.dry-rb.org/t/handling-nested-forms/146/2 "2016-11-09T21:22:22Z")

</div>

This looks like a bug, actually, but I’m not sure if it’s gonna be fixed as we’re moving away from implicit coercions as they will be replaced with explicit type specs.

---

<div class="post-metadata">

### Author: ![mainameiz](https://avatars.discourse-cdn.com/v4/letter/m/8e8cbc/32.png) [@mainameiz](https://discourse.dry-rb.org/u/mainameiz)
#### Post date: [July 3, 2018, 6:16pm UTC](https://discourse.dry-rb.org/t/handling-nested-forms/146/3 "2018-07-03T18:16:09Z")

</div>

I have the same problem ☹ Any news here?

```
require 'dry-types'
require 'dry-validation'

puts "dry-types version: #{Dry::Types::VERSION}"
puts "dry-validation version: #{Dry::Validation::VERSION}"

module Types
  include Dry::Types.module
end

BookSchema = Dry::Validation.JSON do
  required(:publication_date).maybe(:time?)
end

MessageSchema = Dry::Validation.JSON do
  required(:book).maybe

  required(:type).filled(:str?).when(eql?: 'book') do
    required(:book).schema(BookSchema)
  end
end

# type conversion works
puts BookSchema.call(publication_date: Time.now.to_s).messages(full: true).inspect
puts BookSchema.call(publication_date: Time.now).messages(full: true).inspect

# type conversion does not work
puts MessageSchema.call(type: 'book', book: { publication_date: Time.now.to_s }).messages(full: true).inspect
puts MessageSchema.call(type: 'book', book: { publication_date: Time.now }).messages(full: true).inspect

```

Outputs:

```
dry-types version: 0.13.2
dry-validation version: 0.12.0
{}
{}
{:book=>{:publication_date=>["publication_date must be a time"]}}
{}
```
