Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add data-attribute to the form tag in simple_form?

I'm using garlic.js to validate my forms. Garlic.js recommends adding a data-attribute on the form tag.

Here's what I need to generate:

<form data-validate="parsley">

I'm having issues to generate this data attribute on the form tag. I tried everything and nothing worked. Anyone has a solution to this?

like image 229
Renato Carvalho Avatar asked May 13 '13 20:05

Renato Carvalho


2 Answers

Try this:

<%= simple_form_for @entity, :html => {:"data-validate" => 'parsley'} do |f| %>
    <!-- inputs -->
<% end %>

Update

From the comment below, to have the form include an HTML5 data attribute with no value try the following:

<%= simple_form_for @entity, :html => {:"other-data-value" => ''} do |f| %> 

By setting the attribute to an empty string the form helper renders just the attribute.

like image 77
Dan Fairaizl Avatar answered Oct 27 '22 01:10

Dan Fairaizl


You can set data attributes directly by passing in a data hash, but all other HTML options must be wrapped in the HTML key. Example:

<%= form_for(@post, data: { validate: "parsley" }, html: { name: "go" }) do |f| %>
   ...
<% end %>

The HTML generated for this would be:

<form action='http://www.example.com' method='post' data-validate='parsley' name='go'>
  ...
 </form>

api.rubyonrails.org

like image 35
mmsilviu Avatar answered Oct 26 '22 23:10

mmsilviu