Luka Kerr Luka Kerr - 5 months ago 35
HTML Question

Simple_form not validating input

I have a rails app where a user submits a form to create an item and then gets redirected to their item they have just created.

Problem I'm having is that if all the fields are left empty, or even one of the required fields is left empty, then the data still gets passed and a new item is created.

I've used the

required: true
property thinking that this will make sure that data is entered, and then no data is entered an error will occur. Although this isn't working.

In the
config/initializaers/simple_form.rb
file, the
config.browser_validations = true
is set to true.

Anybody know why the input is still passing through?

Simple form:

<%= simple_form_for @item do |f| %>

<%= f.collection_select :category_id, Category.all, :id, :name, {promt: "Choose a category" }, input_html: { class: " dropdown-toggle" } %>

<%= f.input :name, label: "Your Name", required: true, error: 'Your name is required', input_html: { class: "form-control", maxlength: 30} %>

<%= f.input :title, label: "Item Title", required: true, error: 'Item title is required', input_html: { class: "form-control", maxlength: 50 } %>

<%= f.input :used?, as: :check_boxes, required: true, label: "Is Your Item Used?" %>

<%= f.input :price, label: "Item Price", required: true, error: 'Price is required', input_html: { class: "form-control", :placeholder => "$" } %>

<%= f.input :description, label: "Item Description", input_html: { class: "form-control" } %>

<%= f.input :email, label: "Email", required: true, error: 'Email is required', input_html: { class: "form-control", :placeholder => "user@email.com" } %>

<%= f.input :phone, label: "Phone Number", input_html: { class: "form-control", :placeholder => "+61 --- --- ---", :value => "+61 " } %>

<%= f.input :suburb, label: "Suburb", required: true, error: 'Suburb is required', input_html: { class: "form-control" } %>

<%= f.input :image, label: "Upload An Image (Must be less than 2mb)" %>

<%= f.button :submit %>

<% end %>


Item Model:

class Item < ActiveRecord::Base
belongs_to :category
belongs_to :user
end


Items Controller:

class ItemsController < ApplicationController
before_action :find_item, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user!, except: [:index, :show]

def show
end

def new
@item = current_user.items.build
end

def create
@item = current_user.items.build(items_params)

if @item.save
redirect_to @item
else
render "New"
end
end

def edit
end

private

def items_params
params.require(:item).permit(:name, :title, :price, :description, :used?, :email, :phone, :suburb, :category_id, :image, :search)
end

def find_item
@item = Item.find(params[:id])
end
end

Answer

From the simple_form documentation:

By default all inputs are required. When the form object has presence validations attached to its fields, Simple Form tells required and optional fields apart. For performance reasons, this detection is skipped on validations that make use of conditional options, such as :if and :unless.

You can add validations for presence in your Item model, e.g. validates :name, presence: true You will not need to include the messages manually this way for each input.

Comments