简体   繁体   中英

How do I set minimum length of my Simple_Form textarea? Ruby on Rails

I am curious about how we should change the validation setting of Simple_form so it allows me to set a minlength for my textarea. How shall we manage to do that? Thank you!

No, simple form doesn't provide any validation for textarea . You have to use javascript, i guess jquery-validation will perfectly fits here.

It is worth mentioning that you can take advantage of HTML5 validation attribute ie maxlength for text area.

And, in case if you don't know to generate required textarea wrapper... this is how

<%= f.input :name , as: :text , :input_html => {:minlength => 10 , maxlength: 25}   %>

If you are using ruby on rails. You can easily create a form in your template view as follows

...
<%= form_tag(your_tag, method: "post") do |f| %>
<%= label_tag('your_textarea_tag', "Test:") %>
    <%= text_field_tag('your_textarea_tag', nil, required: true, minlength: 2 , maxlength: 100, size: 100) %>
<% end %>
...

required is to make sure it is not empty. minlength and maxlength controls the length of the content. label_tag and label_tag is derived from actionview which is included in rails.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM