From eba5e0918a68a30c8b92541c60dd9015e8f504ec Mon Sep 17 00:00:00 2001 From: f Date: Fri, 15 Jun 2018 19:13:18 -0300 Subject: [PATCH] los campos pueden ser obligatorios --- app/assets/javascripts/validation.js | 12 ++++++++++++ app/models/post/template_field.rb | 11 +++++------ app/views/posts/_form.haml | 10 ++++++---- config/locales/en.yml | 2 +- config/locales/es.yml | 1 + 5 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 app/assets/javascripts/validation.js diff --git a/app/assets/javascripts/validation.js b/app/assets/javascripts/validation.js new file mode 100644 index 0000000..17574c0 --- /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 a7233b6..97834b1 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 fcb5894..06d2d7d 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 232f4fb..0b259f7 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 efc877d..d05e8e8 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!'