Gabber
Gabber

Reputation: 5452

Db fields validation client side

I would like to automate client side data validation for my application that uses SpringMVC and Hibernate, specially in case of referential integrity constraint violation. A simple version of the structure of my application follows:

DB

I have an oracle db with some tables, let's take for example

a table DOCUMENT with an integer primary key DOC_KEY and a TITLE field a table PAGE with a foreign key FK_DOCUMENT_ID, a key PAG_KEY and a text "TEXT" field.

ORM/application layer

I have two (hibernate) entities(Document and Page) mapping those tables. To create a Page you need to create a Document before, it is not possible to create a Page without specifying the related DOC_KEY in the foreign key field.

Web layer

I created a form that allows the user to create a new document with some pages. He can insert a title for the document and one or more pages. Trying to create the Page object without specifying the Document title results in a DB error because no document is specified for the pages.

Example use case

The user tries to create a page setting its contents without specifying a document title. The client side validator marks the title field in red asking the user to fill it.

Question

Is it possible to automatically generate client side validation rules to avoid a server side call in case the Entity/Object/row I'm trying to insert doesn't satisfy the FK integrity rule check?

OR Are there any tools that allow a client side validation tool to "understand" foreign keys consistency?

I tried with jquery but wasn't able to find a way to automatically check foreign keys consistency.

EDIT: *Notes on accepted answer* Of course there is no way to know the state of the db client side without querying it or caching it client side, but that knowledge is necessary to check if a key exists and an integrity constraint is satisfied. My accepted answer matches the focus of the question. I should have thought about it before

Upvotes: 3

Views: 470

Answers (3)

Dileep
Dileep

Reputation: 5440

Is it possible to automatically generate client side validation rules to avoid a server side call in case the Entity/Object/row I'm trying to insert doesn't satisfy the FK integrity rule check?

The Answer is a no..!!

The database can be set with the different foreign key relations mapping and its integrity can be maintained by specifying the Foreign key relations in the mapping class. But the hibernate does not do anything in the front end.!!

You can do validations using the hibernate annotations.But that can only be done in the back end. Hibernate does not deal with the front end.!!

I understand the requirement that you have, but hibernate is not a front end tool. I suggest, any Operations related to DB communication must stay in the back end, else it makes an entry point to security freaks. Better keep it behind the service layer.

Upvotes: 1

xwoker
xwoker

Reputation: 3171

If you declare you database rules on the hibernate entities (what I would recommend), you might want to check the JSR303JS project. I haven't used it myself and it doesn't seem to be very active, but it should give you an idea how you could implement to yourself.

Upvotes: 1

abcd
abcd

Reputation: 3210

You may have to check if the document is created earlier and this you have to save it in some global object of js and then check for page. If both has the data then go for insertion. You can achieve this using jquery.

Upvotes: 0

Related Questions