Skip to content
This repository has been archived by the owner on Jun 22, 2022. It is now read-only.

Better form validation #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

scottharvey
Copy link

This PR updates the controller and form template so that validation errors are rendered on the screen like this.

Screen Shot 2020-03-21 at 3 49 53 PM

It works by including the https://github.com/jorgemanrubia/turbolinks_render gem, this gem makes it so that HTML returned from a POST request will be rendered by Turbolinks.

I have then updated the controller template to set the errors and I've updated the form template to display the errors inline.

<div class="invalid-feedback">
<%%= <%= model_resource_name %>.errors[:<%= attribute.column_name %>].to_sentence.humanize %>
</div>
<%% end %>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Display the error under the form input

<% else -%>
<%%= form.<%= attribute.field_type %> :<%= attribute.column_name %>, class: 'form-control' %>
<%%= form.<%= attribute.field_type %> :<%= attribute.column_name %>, class: "form-control #{'is-invalid' if <%= model_resource_name %>.errors[:<%= attribute.column_name %>].present?}" %>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the is-invalid class to the input, maybe this logic should be moved into a variable to avoid the repetition.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant