GeekedOut
GeekedOut

Reputation: 17185

HAML - how to create form validation and display error messages?

I am able to render a form in HAML but I am not sure how to validate it.

Here is what I have so far:

  #disclosure-form-container
    = form_for([:mobile,@disclosure], :html => {:id => "disclosure-form", :remote => true}) do |f|
      %p
        =f.label :display_as, (t :select_disclosure_type)
        =f.select :display_as, options_from_collection_for_select(Disclosure.basics, :display_as, :name, f.object.display_as)
      %p
        =f.label :notes, (t :notes)
        =f.text_area :notes, :class => "#{validation_rules(:free_disclosure_notes)}", :rows => 5 , :id => "disclosure_notes_entry"
        = buttonset({:submit_label => (t "buttons.influencers.disclosures.create"),:submit_css_class => "call-to-action",:cancel_url => "#",:direction => :left_to_right})

This renders a form with two fields and a button to click submit. But what if people just enter submit without doing much? Where do I put the validation code and how do I validate this form?

Thanks!

Upvotes: 2

Views: 4951

Answers (2)

Andrew
Andrew

Reputation: 43153

In your Disclosure model, you need:

class Disclosure < ActiveRecord::Base
  validates_presence_of :display_as, :notes
  ...
end

Then you can include error messages at the top of your form with something like:

- if @disclosure.errors.any?
  %ul.errors
    - @disclosure.errors.full_messages.each do |msg|
      %li= msg

Or if you use Simple Form you get automatic validation messages inline, and your form would just look like this:

= simple_form_for @disclosure do |f|
  = f.input :display_as
  = f.input :notes
  = f.submit

And you're done.

Note, as Jdoe pointed out, HAML has nothing to do with detecting validations, it only displays what the server tells it. The server is what determines whether there are validation errors.

If you want to try and come up with the equivalent of something like this client side you could give your form an id and do something like this (in CoffeeScript):

jQuery ->
  $('form#disclosures').submit (event) ->
    unless $('input#display_as').val().length > 0 && $('input#notes').val().length > 0
      event.preventDefault()
      $(this).append('<div class="error">You must select 'display' and enter notes.</div>')

Upvotes: 2

jdoe
jdoe

Reputation: 15779

A user enters data that should be saved in your model and then stored in your DB. So it's naturally to implement validation on model-level. Rails allows you to create validation for models easy. Here you can read about it. In a few words: adding few lines to your model prevent it to be saved with inconsistent data.

But you can in addition to model-level validation use client-side validation: i.e., data will be checked before sending to server. This way user don't have to submit form to find out that he forgot to fill some required field. But of course this can't be any guaranty as it's easy to bypass. Here more about client-side validation.

So, as you see, Haml can't help you with validations. It's a great tool, but not for this purpose.

Upvotes: 2

Related Questions