# Phoenix Form default values

**URL:** <https://forum.elixirforum.com/t/phoenix-form-default-values/22813>\
**Category:** Questions\
**Tags:** forms, phoenix, database\
**Created:** [May 31, 2019, 9:41am UTC](https://forum.elixirforum.com/t/phoenix-form-default-values/22813 "2019-05-31T09:41:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeykey](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jeykey/32/17786_2.png) [@Jeykey](https://forum.elixirforum.com/u/Jeykey)\
**Post date:** [May 31, 2019, 9:41am UTC](https://forum.elixirforum.com/t/phoenix-form-default-values/22813/1 "2019-05-31T09:41:54Z")

</div>

Hey everyone,  
I’m new to Elixir and Phoenix and I’m currently working on a very simple Phoenix application.  
While working on the Form for Users to sing up, a problem came up.  
At the moment users can set their own score, but should not be possible.  
How can I set a default score for each new user that then gets written to the database?

---

<div class="post-metadata">

**Author:** ![peerreynders](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peerreynders/32/5826_2.png) [@peerreynders](https://forum.elixirforum.com/u/peerreynders)\
**Post date:** [May 31, 2019, 10:37am UTC](https://forum.elixirforum.com/t/phoenix-form-default-values/22813/2 "2019-05-31T10:37:14Z")

</div>

User sign up would typically have two phases in a [User Controller](https://media.pragprog.com/titles/phoenix14/code/authentication/listings/rumbl/lib/rumbl_web/controllers/user_controller.change3.ex):

- `new` prepares the information to be shown on the sign up page.
- `create` takes the information entered on the sign up page and prepares it for insertion into the database.

So it would be `Accounts.register_user(user_params)` (under `create`) responsibility to set any “default” values. The score wouldn’t be in the `user_params` as the user can’t set it. But `register_user` would “know” what default score to set.

---

<div class="post-metadata">

**Author:** ![cnck1387](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/c/2bfe46/32.png) [@cnck1387](https://forum.elixirforum.com/u/cnck1387)\
**Post date:** [May 31, 2019, 12:34pm UTC](https://forum.elixirforum.com/t/phoenix-form-default-values/22813/3 "2019-05-31T12:34:45Z")

</div>

I would set this at the database + schema level, so if the field is not present upon creation, it will use that value.

For example:

- In your schema: `field :score, :integer, default: 100, null: false`
- In your DB migration: `add(:score, :integer, default: 100, null: false)`

If you don’t want users to be able to adjust it during sign up or in other forms, then in your changeset you wouldn’t put `score` in your list of casted attributes.

---

<div class="post-metadata">

**Author:** ![Jeykey](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jeykey/32/17786_2.png) [@Jeykey](https://forum.elixirforum.com/u/Jeykey)\
**Post date:** [June 3, 2019, 2:56pm UTC](https://forum.elixirforum.com/t/phoenix-form-default-values/22813/4 "2019-06-03T14:56:50Z")

</div>

Thank you, I might try that later. For now I just removed the field and from the form and in the controller I simply merge the params with a map for the default values, which works great for me.
