los campos pueden ser obligatorios
This commit is contained in:
parent
3e16a6eaeb
commit
eba5e0918a
5 changed files with 25 additions and 11 deletions
12
app/assets/javascripts/validation.js
Normal file
12
app/assets/javascripts/validation.js
Normal file
|
@ -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');
|
||||||
|
});
|
||||||
|
});
|
|
@ -10,9 +10,13 @@ class Post
|
||||||
@contents = contents
|
@contents = contents
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Obtiene el valor
|
||||||
|
def value
|
||||||
|
complex? ? contents.dig('value') : contents
|
||||||
|
end
|
||||||
|
|
||||||
def type
|
def type
|
||||||
return @type if @type
|
return @type if @type
|
||||||
return unless simple?
|
|
||||||
|
|
||||||
case
|
case
|
||||||
when text_area?
|
when text_area?
|
||||||
|
@ -79,11 +83,6 @@ class Post
|
||||||
array?
|
array?
|
||||||
end
|
end
|
||||||
|
|
||||||
# Obtiene el valor
|
|
||||||
def value
|
|
||||||
complex? ? contents.dig('value') : contents
|
|
||||||
end
|
|
||||||
|
|
||||||
# Obtiene los valores posibles para el campo de la plantilla
|
# Obtiene los valores posibles para el campo de la plantilla
|
||||||
def values
|
def values
|
||||||
return '' if %w[string text].include? value
|
return '' if %w[string text].include? value
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
- else
|
- else
|
||||||
- url = site_post_path(@site, @post, lang: @lang)
|
- url = site_post_path(@site, @post, lang: @lang)
|
||||||
- method = :patch
|
- method = :patch
|
||||||
= form_tag url, method: method, class: 'form' do
|
= form_tag url, method: method, class: 'form', novalidate: true do
|
||||||
.form-group
|
.form-group
|
||||||
= submit_tag t('posts.save'), class: 'btn btn-success'
|
= submit_tag t('posts.save'), class: 'btn btn-success'
|
||||||
.form-group
|
.form-group
|
||||||
|
@ -85,9 +85,10 @@
|
||||||
- when 'text'
|
- when 'text'
|
||||||
= text_field 'post', template,
|
= text_field 'post', template,
|
||||||
value: value,
|
value: value,
|
||||||
class: 'form-control'
|
class: 'form-control',
|
||||||
|
required: template.required?
|
||||||
- when 'text_area'
|
- 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'
|
- when 'check_box'
|
||||||
= hidden_field 'post', template, value: 'false'
|
= hidden_field 'post', template, value: 'false'
|
||||||
= check_box_tag name, 'true', value == 'true', class: 'form-control'
|
= 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)),
|
= select_tag name, options_for_select(template.values, @post.get_front_matter(template)),
|
||||||
{ class: 'form-control select2',
|
{ class: 'form-control select2',
|
||||||
multiple: template.multiple? }
|
multiple: template.multiple? }
|
||||||
|
.invalid-feedback= t('posts.invalid')
|
||||||
.form-group
|
.form-group
|
||||||
= submit_tag t('posts.save'), class: 'btn btn-success'
|
= submit_tag t('posts.save'), class: 'btn btn-success', id: 'submit-post'
|
||||||
|
|
|
@ -202,4 +202,4 @@ en:
|
||||||
file: "Couldn't write the file"
|
file: "Couldn't write the file"
|
||||||
title: 'Post needs a title'
|
title: 'Post needs a title'
|
||||||
date: 'Post needs a valid date'
|
date: 'Post needs a valid date'
|
||||||
|
invalid: 'This field is required!'
|
||||||
|
|
|
@ -198,3 +198,4 @@ es:
|
||||||
file: 'No se pudo escribir el archivo'
|
file: 'No se pudo escribir el archivo'
|
||||||
title: 'Necesita un título'
|
title: 'Necesita un título'
|
||||||
date: 'Necesita una fecha'
|
date: 'Necesita una fecha'
|
||||||
|
invalid: '¡Este campo es obligatorio!'
|
||||||
|
|
Loading…
Reference in a new issue