You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
defmoduleParentdouseEcto.SchemaimportEcto.ChangesetaliasParent.Childschema"parent"dohas_one(:child,Child)enddefchangeset(parent,attrs\\%{})doparent|>cast(attrs,[])|>cast_assoc(:child)enddefmoduleChilddouseEcto.SchemaimportEcto.Changesetschema"child"dofield(:boolean_field,:boolean)field(:another_field,:string)enddefchangeset(child,attrs)docast(child,attrs,[:boolean_field,:another_field])# This is important!# we change the boolean_field back to false (in real life this is something dynamic)|>put_change(:boolean_field,false)endendend
defmoduleTestdouseExUnit.CaseimportEcto.Changesettest"FormField should reflect changes"doparent=%Parent{child: %Parent.Child{id: 1,boolean_field: false}}changeset=Parent.changeset(parent,%{"child"=>%{"id"=>"1","boolean_field"=>"true","another_field"=>"bananas"}})# This assert proves the above setup results in the desired outcome.# Remember that boolean_field is changed back to `false` no matter# what the params were. This works as expected.changed_parent=apply_changes(changeset)assert%{boolean_field: false,another_field: "bananas"}=changed_parent.child# Emulate creation of a FormField (this should replicate the usage of# the <.form> and <.inputs_for> components more or less).form_parent=Phoenix.HTML.FormData.to_form(changeset,[])[form_child]=Phoenix.HTML.Form.inputs_for(form_parent,:child)# works as expected for the other fieldassertform_child[:another_field].value=="bananas"# BOOM! This is not what is expected. We would expect `false` here,# just like in the resulting struct after doing `apply_changes/1` on the changeset.assertform_child[:boolean_field].value==falseendendExUnit.run()