sgruhier / foundation_rails_helper

Rails Helper for Zurb Fondation framework
MIT License
153 stars 84 forks source link

Add abide validation helpers #100

Open dsandstrom opened 9 years ago

dsandstrom commented 9 years ago

Reference: http://foundation.zurb.com/docs/components/abide.html

If a field has a required attribute, the form should get a data-abide attribute and a <small class="error">required</small> should be added below the field. The error message should be able to be edited with a field option.

jlaw90 commented 8 years ago

I agree that abide validation would be great, I don't think the framework should automatically add the data-abide attribute to the form based on specific attributes on controls however.

If form_for had an option to enable abide validation which subsequently added the error tags to controls then that would work great in all scenarios. The main problem here would be how to get a generalised error message depending on the validation that failed.

I propose something like the following should be flexible enough for most cases:

= form_for(@user, abide: true) do |f|
  = f.text_field :username, required: true, pattern: '[a-zA-Z]+', error: 'Please enter your username'
  = f.password_field :password, required: true, error: 'Password is required'

Which would generate the following html:

<form class="new_user" action="/users/new" data-abide accept-charset="UTF-8" method="post">
  <div class="user-username-field">
    <label for="user_username">Username <small>required</small>
      <input type="text" name="user[username]" id="user_email" required pattern="[a-zA-Z]+">
    </label>
    <small class="error">Please enter your username</error>
  </div>
  <div class="user-password-field">
    <label>Password <small>required</small>
      <input type="password" name="user[password]" id="user_password" required>
    </label>
    <small class="error">Password is required</small>
  </div>
</form>

It would be even better if the code generating the fields could automatically add pattern and required attributes based on rails validations but that would probably be a bit more difficult!