diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index fe1ce9b26..3320c2032 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -43,6 +43,7 @@ //= require bulkrax/application //= require hyrax +//= require hyrax/conditionally_required_fields //= require iiif_print //= require jquery.flot.pie diff --git a/app/assets/javascripts/hyrax/conditionally_required_fields.js b/app/assets/javascripts/hyrax/conditionally_required_fields.js new file mode 100644 index 000000000..01e86718d --- /dev/null +++ b/app/assets/javascripts/hyrax/conditionally_required_fields.js @@ -0,0 +1,43 @@ +/** + * This file is used to conditionally require fields on the edit/new work forms. + * At the moment, rights statement is required by default, and rights notes is not. + * One or the other must be filled out in order to create/update a work + */ + +// check the value of the rights statement and rights notes fields on page load +$(document).on('turbolinks:load', function () { + $('#generic_work_rights_statement').each(function () { + if (this.value || $('#generic_work_rights_notes')[0].value) return; + + $('#generic_work_rights_statement').attr('required', true); + $('#generic_work_rights_notes').attr('required', true); + }); +}); + +// check the value of the rights notes field after its focus is removed +$(document).on('turbolinks:load', function () { + return $('body').on('blur', '#generic_work_rights_notes', function () { + if (this.value === undefined) return; + $('#generic_work_rights_notes').attr('required', true); + + if (this.value === '') { + $('#generic_work_rights_statement').attr('required', true); + } else { + $('#generic_work_rights_statement').attr('required', false); + } + }); +}); + +// check the value of the rights statement field after its focus is removed +$(document).on('turbolinks:load', function () { + return $('body').on('blur', '#generic_work_rights_statement', function () { + if (this.value === undefined || !this.value) return; + $('#generic_work_rights_statement').attr('required', true); + + if (this.value === '') { + $('#generic_work_rights_notes').attr('required', true); + } else { + $('#generic_work_rights_notes').attr('required', false); + } + }); +}); diff --git a/app/views/records/edit_fields/_rights_statement.html.erb b/app/views/records/edit_fields/_rights_statement.html.erb new file mode 100644 index 000000000..b9bebb4b2 --- /dev/null +++ b/app/views/records/edit_fields/_rights_statement.html.erb @@ -0,0 +1,8 @@ +<%# OVERRIDE: Hyrax 3.5.0 so this is not a required field by default %> +<% rights_statements = Hyrax.config.rights_statement_service_class.new %> +<%= f.input :rights_statement, + collection: rights_statements.select_active_options, + include_blank: true, + item_helper: rights_statements.method(:include_current_value), + required: false, + input_html: { class: 'form-control' } %>