This is my form:-
<form action="#" th:object="${note}" th:action="$('#note-id').val()!=null ? @{/update} : @{/note}" method="POST">
<input type="hidden" name="noteId" id="note-id" th:field="*{noteId}" >
<div class="form-group">
<label for="note-title" class="col-form-label">Title</label>
<input type="text" name="noteHead" th:field="*{noteHead}" class="form-control" id="note-title" maxlength="20" required>
</div>
<div class="form-group">
<label for="note-description" class="col-form-label">Description</label>
<textarea class="form-control" name="noteDescription" th:field="*{noteDescription}" id="note-description" rows="5" maxlength="1000" required></textarea>
</div>
<button id="noteSubmit" type="submit" class="d-none" ></button>
</form>
This form is inside a popUp window(modal class). I want use the same Window for add/update. Add is working fine.
For Update i am passing in the data to the modal, which is getting displayed aswell.
<button type="button" class="btn btn-success"
th:data-noteId="${note.noteId}"
th:data-noteTitle="${note.noteHead}"
th:data-noteDescription="${note.noteDescription}"
onclick="showNoteModal(
this.getAttribute('data-noteId'),
this.getAttribute('data-noteTitle'),
this.getAttribute('data-noteDescription'));">
Edit</button>
What i want to do is to create a conditional statement in th:action based on wheather "note-id" has value, in which case its an update otherwise it should be a New Entry.
Please Help
You want to update th:action of your form in such a way that if note-id
has value an edit request is sent to server otherwise add request?
If that's the case then you can do it this way
<form th:action="${user.note_id}==null? @{/save} : @{/edit}">
With param
<form th:action="${user.note_id}==null? @{/save} : @{/edit/(noteId=${user.note_id})}">
Check thymeleaf standard URL syntax documentation for more details.