-1

for a file_field field I would like to make a required: true but without the view returning a (*) and also specifying the return message if a file is empty.

example: <%= f.file_field :file, required: true, label: '&nbsp;'.html_safe %>

someone to guide me please.

Thanks in advance I'm new to ruby

  • 2
    You want a field to be required, but don't display that it's required, and don't show any meaningful error message if left blank? This doesn't sound like a very helpful UI. – Tom Lord Jul 01 '20 at 16:04
  • I not only want (*) otherwise I want to see an error message, but I want to customize the error message @TomLord – Ibrahima Koné Jul 01 '20 at 16:35
  • Oh right... Your question is worded a bit ambiguously, sorry. To specify a custom validation error message, see the rails documentation [here](https://guides.rubyonrails.org/active_record_validations.html#message). – Tom Lord Jul 01 '20 at 21:23
  • As for the asterisk -- you *could* just cheat and not mark the field as required, but that would be bad practice. A better solution is to disable whatever HTML/CSS is causing it to appear in the first place. I don't know exactly how it's implemented, because you've only shown one line of code, and none of the HTML nor CSS. For example, maybe you're using `simple_form`? Does [this](https://stackoverflow.com/q/7650689/1954610) answer your question? I'd need more information to say for sure. – Tom Lord Jul 01 '20 at 21:29

1 Answers1

0
                <%= f.file_field :file,  hide_label: true, label: '.',
                       required: (not @my_file.persisted? ) %>

with this code the file_field is checked without (*) thanks @TomLord