Skip to content

Commit

Permalink
Merge pull request #37 from leepeuker/revalidate-watch-date-input-on-…
Browse files Browse the repository at this point in the history
…focus-end

Revalidate watch date input when input loses focus
  • Loading branch information
leepeuker authored Jul 4, 2022
2 parents 9a08e10 + 168f62a commit aee582a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
38 changes: 23 additions & 15 deletions public/js/logMovie.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
const elems = document.querySelectorAll('.datepicker_input')
for (const elem of elems) {
const datepicker = new Datepicker(elem, {
format: 'dd.mm.yyyy',
title: 'Watch date',
})
}
const datepicker = new Datepicker(document.getElementById('watchDate'), {
format: 'dd.mm.yyyy',
title: 'Watch date',
})

const watchModal = document.getElementById('watchDateModal')
watchModal.addEventListener('show.bs.modal', async function (e) {
Expand Down Expand Up @@ -97,12 +94,7 @@ const addAlertMessage = (message, type) => {
alertPlaceholder.append(wrapper)
}

function logMovie () {
let rating = getRatingFromStars()
let tmdbId = getTmdbId()
let watchDate = getWatchDate()
let movieTitle = document.getElementById('watchDateModalTitle').innerHTML

function validateWatchDate (watchDate) {
document.getElementById('watchDateValidationRequiredErrorMessage').classList.remove('d-block')
document.getElementById('watchDateValidationFormatErrorMessage').classList.remove('d-block')

Expand All @@ -111,20 +103,36 @@ function logMovie () {
document.getElementById('watchDate').style.borderColor = '#dc3545'
document.getElementById('ratingStars').style.marginTop = '0'
document.getElementById('watchDateValidationRequiredErrorMessage').classList.add('d-block')
return

return false
}

if (isValidDate(watchDate) === false) {
document.getElementById('watchDate').style.borderStyle = 'solid'
document.getElementById('watchDate').style.borderColor = '#dc3545'
document.getElementById('ratingStars').style.marginTop = '0'
document.getElementById('watchDateValidationFormatErrorMessage').classList.add('d-block')
return

return false
}

document.getElementById('watchDate').style.borderStyle = ''
document.getElementById('watchDate').style.borderColor = ''
document.getElementById('ratingStars').style.marginTop = '0.5rem'

return true
}

function logMovie () {
let rating = getRatingFromStars()
let tmdbId = getTmdbId()
let watchDate = getWatchDate()
let movieTitle = document.getElementById('watchDateModalTitle').innerHTML

if (validateWatchDate(watchDate) === false) {
return
}

fetch('/log-movie', {
method: 'post', headers: {
'Content-type': 'application/json',
Expand Down
2 changes: 1 addition & 1 deletion templates/page/logMovie.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<input type="hidden" value="" name="tmdbId" id="tmdbId" required>
<div class="input-group" id="watchDateGroup">
<i class="bi bi-calendar-date input-group-text"></i>
<input type="text" class="datepicker_input form-control" placeholder="Watch date" aria-label="Watch date" name="watchDate" id="watchDate" required>
<input type="text" class="datepicker_input form-control" placeholder="Watch date" aria-label="Watch date" name="watchDate" id="watchDate" required onfocusout="validateWatchDate(this.value)">
<div class="invalid-feedback" id="watchDateValidationRequiredErrorMessage" style="padding-bottom: 0;margin-bottom: 0">
Watch date is required!
</div>
Expand Down

0 comments on commit aee582a

Please sign in to comment.