diff --git a/app/javascript/controllers/required_checkbox_controller.js b/app/javascript/controllers/required_checkbox_controller.js index 9754b506..a24ce881 100644 --- a/app/javascript/controllers/required_checkbox_controller.js +++ b/app/javascript/controllers/required_checkbox_controller.js @@ -20,5 +20,20 @@ export default class extends Controller { } else { this.requiredTarget.required = !Array.from(this.checkboxTargets).some(x => x.checked); } + + for (const checkbox of this.checkboxTargets) { + if (checkbox === event.target) continue; + + checkbox.required = !event.target.checked; + } + } + + /* + * Si el checkbox es considerado + */ + invalid(event) { + for (const checkbox of this.checkboxTargets) { + checkbox.required = true; + } } } diff --git a/app/views/posts/attributes/_new_array.haml b/app/views/posts/attributes/_new_array.haml index 4293ded1..5778f619 100644 --- a/app/views/posts/attributes/_new_array.haml +++ b/app/views/posts/attributes/_new_array.haml @@ -15,11 +15,12 @@ Si la lista es obligatoria, al menos uno de los ítems tiene que estar activado. Logramos esto con un checkbox oculto que se marca como obligatorio al validar el formulario. - - if metadata.required - %input.form-control{ type: 'checkbox', name: name, data: { target: 'required-checkbox.required' }, required: metadata.value.empty? } - .invalid-feedback Requerido! .d-flex.align-items-center.justify-content-between - = label_tag id, post_label_t(attribute, post: post) + %div + = label_tag id, post_label_t(attribute, post: post), class: 'mb-0' + - if metadata.required + %input.form-control.d-none{ type: 'checkbox', name: name, data: { target: 'required-checkbox.required', action: 'invalid->required-checkbox#invalid' }, required: metadata.value.empty? } + .invalid-feedback.mt-0= t('.required') = render 'bootstrap/btn', content: t('.edit'), action: 'modal#show' -# Mostramos la lista de valores actuales. diff --git a/config/locales/en.yml b/config/locales/en.yml index 94a6968a..6b8562d1 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -764,6 +764,7 @@ en: edit: "Edit" new_array: edit: "Edit" + required: "Please select at least one option." new_has_one: edit: "Edit" new_belongs_to: diff --git a/config/locales/es.yml b/config/locales/es.yml index fcaa6658..d1c61fd2 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -772,6 +772,7 @@ es: edit: "Editar" new_array: edit: "Editar" + required: "Seleccioná al menos una opción." new_has_one: edit: "Editar" new_belongs_to: