Skip to content

07.0.0 Use Server Side Validation

Patrick Baselier edited this page Oct 12, 2015 · 2 revisions

What you will learn

  • Add Server Side Validation

Before you start

See the installation instructions. Use tag 7.0.0.

Add Server Side Validation

  • Open backend/app/models/product.rb and make title a required attribute:
class Product < ActiveRecord::Base
  validates :title, presence: true
end
  • Open the app, edit a product, leave the title empty and save it.
  • Inspect the error shown in the Console tab in the browser. There's an error telling that the backend did not save the record. Let's use this error and show it to the user.
  • Open frontend/app/templates/products/show.hbs and update the markup for the title field in the form:
<div class='form-group {{if model.errors.title 'has-error'}}'>
  {{input value=model.title class='form-control' placeholder='Enter a title'}}
  {{#each model.errors.title as |error|}}
    <div class='control-label'>{{error.message}}</div>
  {{/each}}
</div>