Validation for Select options using active model validations


#1

Hi all -

I’m very new to ruby, and am currently trying to build a form with validation without relying on javascript.
It’s been a steep learning curve, and I’m sure what I’ve done is very flawed, but I was hoping for some help
to continue my journey.

I currently have a very basic form with a ‘title’ select box, and two text fields for first and last name. I found a ruby doc on ‘active model validations’ which I’m trying to use to validate the fields. So far I’m using it and the regex to validate the text fields against being empty, and only allowing letters and spaces and that’s working.
But I’m unsure how to use it with a select box which has a bunch of possible values. I need to validate the select box to not be empty (be default an empty value shows). I’ve tried ‘validates_inclusion_of’ and creating an array of possible values, but I’m a bit stuck.

I’m going to paste what I have below and would REALLY appreciate some feedback. Many thanks!!

**MODEL:**

class Person

include ActiveModel::Validations

attr_reader :firstname, :lastname

def initialize(title=nil, firstname=nil, lastname=nil)
  @title = title
  @firstname = firstname
  @lastname = lastname
end

def errors
  if ( @errors == nil)
    @errors = ActiveModel::Errors.new(self)
  end
  return @errors
end

validates_inclusion_of :title, :in => [Mr, Mrs, Miss, Ms], :allow_nil => false
validates :firstname, presence: true, length: {in: 2..120, allow_blank: true}, format: { with: /\A[-A-Z\s]+\z/i, allow_blank: true}
validates :lastname, presence: true, length: {in: 2..120, allow_blank: true}, format: { with: /\A[-A-Z\s]+\z/i, allow_blank: true}
end


**CONTROLLER:**
    
class PersonController < ApplicationController
  def get
      @person = Person.new()
      render action: "person"
  end

  def done
      render action: "success"
  end

  def post
      puts params

      @person = Person.new(params["title"], params["firstname"], params["lastname"])

      if @person.valid?
          render action: "success"
      else
          render action: "person"
      end
  end
end

**VIEW:**

<h1>Form</h1>

<% if @person.invalid? %>
    <h3>Form is Invalid</h3>
<% end %>

<%= form_tag("/person", method: "post", controller: "person") do %>

  <div class="form-group<%= ' has-error' if @person.errors.include?(:title) %>">
    <label for="name" class="control-label">Name</label>
    <select id="title" name="title">
      <option value="none" selected></option>
      <option value="mr">Mr</option>
      <option value="mrs">Mrs</option>
      <option value="miss">Miss</option>
      <option value="ms">Ms</option>
      <option value="other">Other</option>
    </select>
     <span class="help-block"><%= @person.errors.full_messages_for(:title).first %></span>
  </div>

  <div class="form-group<%= ' has-error' if @person.errors.include?(:firstname) %>">
    <label for="firstname" class="control-label">First Name</label>
    <input type="text" class="form-control" name="firstname" id="firstname" value="<%= @person.firstname %>">
    <span class="help-block"><%= @person.errors.full_messages_for(:firstname).first %></span>
  </div>

  <div class="form-group<%= ' has-error' if @person.errors.include?(:lastname) %>">
    <label for="lastname" class="control-label">Last Name</label>
    <input type="text" class="form-control" name="lastname" id="lastname" value="<%= @person.lastname %>">
    <span class="help-block"><%= @person.errors.full_messages_for(:lastname).first %></span>

  </div>

  <button type="submit" class="btn btn-primary">Submit</button>

<% end %>