-1

I am trying to make a ruby tag field a required field client side only and I am trying to do so via:

<%= f.text_field :name, :placeholder => 'Event Title', :id => "form-field-first", :class => "form-field", :class => "req" %>

but it still submits no prob if the field is empty. Is it more complicated than this? do I have to go in my .rb file and make a special required class? Thanks for any help!

ZachyBear
  • 297
  • 3
  • 15
  • Are you trying to style the field as a required field or actually make the field required? For the former, see @mdestantis' answer. For the latter, see my answer. – Mark Silverberg Sep 09 '14 at 21:45
  • @ZachyBear, there's no `form_for` "req" option; can you be more clear? – mdesantis Sep 09 '14 at 21:46
  • 1
    possible duplicate of [HTML5 'required' validation in Ruby on Rails forms](http://stackoverflow.com/questions/13541983/html5-required-validation-in-ruby-on-rails-forms) – Brad Werth Sep 09 '14 at 23:15
  • Nope, never asked that question, this is no duplicate. – ZachyBear Sep 09 '14 at 23:17
  • Why am I getting down voted for trying something wrong?=( Isn't that what stack overflow is all about? I saw the class "req" on a resource somewhere and I tried to use it. – ZachyBear Sep 10 '14 at 20:53

3 Answers3

2

As noted at http://guides.rubyonrails.org/active_record_validations.html#presence,

You should use validates :field, presence: true in your model.rb file to do this.

Mark Silverberg
  • 1,249
  • 2
  • 8
  • 21
1

You should write the tag class attribute as a space-separated string:

<%= f.text_field :name, 
                 :placeholder => 'Event Title', 
                 :id => "form-field-first", 
                 :class => "form-field req" %>
mdesantis
  • 8,257
  • 4
  • 31
  • 63
0

All good ideas but I found it on a similar question, all you need in any ruby tag is:

:required => true

so I can,

<%= f.text_field :name, :placeholder => 'Event Title', :id => "form-field-first", :class => "form-field", :required => true %>

and bingo! super simple and could not find the option on the docsXD

ZachyBear
  • 297
  • 3
  • 15
  • this is all and well if you want to validate **client side only**. This is not rails specific and is instead just setting the HTML attribute of the input element. A user can modify the DOM and bypass this easily. – Mark Silverberg Sep 10 '14 at 00:00