diff --git a/app/assets/javascripts/validation.js b/app/assets/javascripts/validation.js new file mode 100644 index 00000000..17574c0d --- /dev/null +++ b/app/assets/javascripts/validation.js @@ -0,0 +1,12 @@ +$(document).on('turbolinks:load', function() { + $('#submit-post').click(function(e) { + var form = $(this).parents('form.form'); + + if (form[0].checkValidity() === false) { + e.preventDefault(); + e.stopPropagation(); + } + + form.addClass('was-validated'); + }); +}); diff --git a/app/models/post/template_field.rb b/app/models/post/template_field.rb index a7233b69..97834b1c 100644 --- a/app/models/post/template_field.rb +++ b/app/models/post/template_field.rb @@ -10,9 +10,13 @@ class Post @contents = contents end + # Obtiene el valor + def value + complex? ? contents.dig('value') : contents + end + def type return @type if @type - return unless simple? case when text_area? @@ -79,11 +83,6 @@ class Post array? end - # Obtiene el valor - def value - complex? ? contents.dig('value') : contents - end - # Obtiene los valores posibles para el campo de la plantilla def values return '' if %w[string text].include? value diff --git a/app/views/posts/_form.haml b/app/views/posts/_form.haml index fcb58944..06d2d7d5 100644 --- a/app/views/posts/_form.haml +++ b/app/views/posts/_form.haml @@ -15,7 +15,7 @@ - else - url = site_post_path(@site, @post, lang: @lang) - method = :patch -= form_tag url, method: method, class: 'form' do += form_tag url, method: method, class: 'form', novalidate: true do .form-group = submit_tag t('posts.save'), class: 'btn btn-success' .form-group @@ -85,9 +85,10 @@ - when 'text' = text_field 'post', template, value: value, - class: 'form-control' + class: 'form-control', + required: template.required? - when 'text_area' - = text_area_tag name, value, class: 'form-control' + = text_area_tag name, value, class: 'form-control', required: template.required? - when 'check_box' = hidden_field 'post', template, value: 'false' = check_box_tag name, 'true', value == 'true', class: 'form-control' @@ -95,5 +96,6 @@ = select_tag name, options_for_select(template.values, @post.get_front_matter(template)), { class: 'form-control select2', multiple: template.multiple? } + .invalid-feedback= t('posts.invalid') .form-group - = submit_tag t('posts.save'), class: 'btn btn-success' + = submit_tag t('posts.save'), class: 'btn btn-success', id: 'submit-post' diff --git a/config/locales/en.yml b/config/locales/en.yml index 232f4fb8..0b259f7e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -202,4 +202,4 @@ en: file: "Couldn't write the file" title: 'Post needs a title' date: 'Post needs a valid date' - + invalid: 'This field is required!' diff --git a/config/locales/es.yml b/config/locales/es.yml index efc877d5..d05e8e8f 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -198,3 +198,4 @@ es: file: 'No se pudo escribir el archivo' title: 'Necesita un título' date: 'Necesita una fecha' + invalid: '¡Este campo es obligatorio!'