In this episode, we focus on ensuring that our book creation process is robust by properly validating the status field. At first, we only required a status to be present, but realized a user could send any value, and our app would accept it—even if it isn't something we want to support.
To solve this, we walk through writing a test that deliberately sends an invalid status (like eating
) and check that it triggers a validation error. Initially, our validation fails because we’re not limiting the values to just our allowed options. We fix this by updating the validation rule to only accept specific statuses—want to read
, reading
, and read
.
But hardcoding these values everywhere isn't ideal, especially as our app and its features grow. So, we take things up a notch by creating a custom pivot class (called BookUser
) and storing our valid statuses there as a static property. This means we can reference our allowed statuses from a single source, making it way easier to maintain and update them later.
There's a bit of fiddling with namespaces and making sure everything is hooked up right, but with some test-driven development and quick fixes, our validation is now reliable and future-proof. The tests all pass in the end, so we're ready to move on to actually hooking up the form and letting users create books with valid statuses. On to the next episode!