Luka Kerr
Luka Kerr

Reputation: 4239

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 => "[email protected]" } %>

  <%= 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

Upvotes: 0

Views: 3641

Answers (2)

Dyllan
Dyllan

Reputation: 21

For those looking for a simple browser validation that prevents the user from submitting if the required field is not filled in. By default simple_form turns off browser validation.

You can turn it on in the configuration file, if you do not have the configuration file, you can generate it with the following command:

rails generate simple_form:install

config > initializers > simple_form

# Tell browsers whether to use the native HTML5 validations (novalidate form option).
# These validations are enabled in SimpleForm's internal config but disabled by default
# in this configuration, which is recommended due to some quirks from different browsers.
# To stop SimpleForm from generating the novalidate option, enabling the HTML5 validations,
# change this configuration to true.

  config.browser_validations = true

Upvotes: 1

gonzalo2000
gonzalo2000

Reputation: 648

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.

Upvotes: 1

Related Questions