I need to add required fields for some forms on my website in order to better verify users
My intended solve: In looking online, I’ve tried a simple html fix within views>devise>registrations>new.html.erb
and have started by editing the following code:
<div class="flex-col md:flex md:flex-row">
<div class="form-group mb-3 pr-0 md:pr-2">
<%= f.label :first_name %>
<%= f.text_field :first_name, autofocus: true, autocomplete: "given-name", class: "form-control"%>
</div>
I've attempted to implement the following:
:required => :true
, required: true
, validates :first_name, presence => true
and other similar variations
I also tried changing in the simple_form.rb
file:
config.browser_validations = false
to = true
in order to enable validations on form fields.
Where I became stuck: None of these changes have broken the localhost, but also haven't changed anything. Each new account creation/sign up is able to be executed without entering a First Name. I can't tell if it's simpleform, html or otherwise.
Here's a few links I've tried to follow:
How to make a field required in Rails?
https://guides.rubyonrails.org/v3.2.13/active_record_validations_callbacks.html
Rails: Attribute required for collection in simple_form https://github.com/heartcombo/simple_form (README)
HTML5 'required' validation in Ruby on Rails forms
EDIT: User.rb
as requested:
class User < ApplicationRecord
# acts_as_token_authenticatable
rolify
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable,
:trackable, :validatable, :rememberable, :confirmable, :async
# :omniauthable, omniauth_providers: %i[facebook]
mount_uploader :logo, AvatarUploader
attr_accessor :skip_password_validation, :registered_as
has_many :wishlists
has_many :projects, dependent: :destroy
# callbacks
after_create :assign_role
after_save :sync_to_active_campaign_contact
#
def already_wishlist?(project)
Wishlist.where(user_id: id, project_id: project.id).exists?
end
#
# def self.from_omniauth(auth)
# where(provider: auth.provider, uid: auth.uid).first
# # where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
# # user.email = auth.info.email
# # user.skip_password_validation = true
# # # user.password = Devise.friendly_token[0,20]
# # # user.name = auth.info.name # assuming the user model has a name
# # # user.image = auth.info.image # assuming the user model has an image
# # # If you are using confirmable and the provider(s) you use validate emails,
# # # uncomment the line below to skip the confirmation emails.
# # # user.skip_confirmation!
# # end
# end
# after_create :send_admin_mail
# def send_admin_mail
# ###Send email stuff here
# end
#
def sync_to_active_campaign_contact
return if Rails.env.development?
if (confirmed_at.nil? ? false : confirmed_at <= Time.zone.now)
begin
fields = {
'email' => email
}
ac_tags = ActiveCampaign.show_tags
contact_tags = []
if has_role?(:buyer)
ac_fields = ActiveCampaign.show_fields
field_id = ac_fields[:fields].find { |field| field[:title].casecmp?('buyer type') }[:id]
fields.merge!({ fieldValues: [{
field: field_id,
value: buyer_type
}] }, first_name: first_name, last_name: last_name)
contact_tags << { contact: "id", tag: ac_tags[:tags].find { |tag| tag[:tag].casecmp?('buyer') }[:id] }
contact_tags << { contact: "id", tag: ac_tags[:tags].find { |tag| tag[:tag].casecmp?(buyer_type) }[:id] }
end
# 1 user can have both roles, using else would crash the logic
if has_role?(:developer)
fields.merge!('orgname' => business_name)
contact_tags << { contact: "id", tag: ac_tags[:tags].find { |tag| tag[:tag].casecmp?('developer') }[:id] }
end
ac_contact = ActiveCampaign.sync_contact(fields)
ac_contact_id = ac_contact.dig(:contact, :id)
contact_tags.each do |ct|
contact_tag = ct.merge({ contact: ac_contact_id, tag: ct[:tag] })
ActiveCampaign.create_contact_tag(contact_tag)
end
rescue StandardError
logger.debug 'unable to sync with active campaign'
end
end
end
protected
#
def password_required?
return false if skip_password_validation
super
end
#
def assign_role
add_role registered_as
end
end
EDIT 2: I'm also needing to include a required selection from an f.select
option, while disabling the default 'Select One' option:
<div class="form-group mb-3">
<%= f.label :buyer_type, "Which best describes you: " %>
<%= f.select :buyer_type, ['Please Select','Option 1', 'Option 2', 'Option 3'], {}, { class: "form-control" } %>
</div>