Spring Boot validation with a svelte frontend (part 3)
In the previous installment of this series we created a react application that read the
content from our server and allowed editing Club entities via an HTML form.
Here, we will create the same application using a react based frontend that allows us to edit
Here, we will create the same application using Svelte.
If you have a dark background still, just empty the app.css file.
Integrating svelte-query
Just like in the previous react tutorial we will use svelte-query which is a 1:1 port of react-query.
First we need to add a provider in App.svelte
To be able to call our apis we define the data model in
src/lib/model.ts
and the api methods in src/lib/api.ts
Unsurprisingly, both of these files are an exact copy of the react based version.
One thing missing is the proxy configuration in the frontend that
will allow us to connect to the spring backend. Add the following lines to client-svelte/vite.config.ts
Loading the club data
As we have all the basics out of the way we can finally load
our club list in src/pages/club/ClubList.svelte
While we won’t win any beauty contest with that, it is functional, so let’s move on.
Clicking on a club line will cause the browser to navigate to http://localhost:5173/clubs/395152559210943000
which is currently still an empty page, let’s fix that by changing src/pages/club/SingleClub.svelte to
This is basically more of the same. We simply load a single club rather than the whole list of clubs.
To edit a club we need to update the src/pages/club/EditClub.svelte page to look like this:
This is just a carbon copy of the SingleClub.svelte file but we are using the not yet defined
ClubForm.
Create the file src/lib/components/ClubForm.svelte with the following content:
If you have already read part 2 of the tutorial
then you should be already quite familiar with what is happening here.
If you are new to the series here is a quick explanation.
We define an input data of type Club here which is passed in by ClubEdit.svelte.
Now we define a felte form that uses the zod validator for ensuring our data is valid.
This is the mutation that will upload all changes to the server. If we encounter an error
we parse the error response to figure out which field had which problem and use
setErrors to populate the client validation errors.
In addition we show a toast message that saving has failed.
The doUpload method is super trivial. This is being called after a successful validation
by felte and only triggers the mutation.
The UI part is very simple. All we need to do is define the input and label
fields and if we have an error, then we display an error annotation.
If we now enter a invalid data, then we should immediately see the validation errors in the form:
Testing server side validation errors
Now if you remember the zod validation we created way back when,
you might notice that there is a discrepancy between the minimum
length in typescript
and the corresponding java code
To be able to produce a server side validation error all we need
to do is to update a club and assign it a clubName that is
only 2 characters long.
The server side validation errors are rendered in-line just like
client side validation errors would.
This concludes part 3 of the tutorial, check out the next article in the series about sorting and paging.
Hi, I'm Rainer. I'm a software engineer based in Salzburg.
You can follow me on
Twitter, see
some of my work on
GitHub, or read
more about me on
my website.