• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need an advice about form validation

#1
[eluser]ang89[/eluser]
Guys, I need an advice about what is best in this case.

I have a database table with a primary key called, let's say "LETTER CODE", which is a non-auto-increment key.
When a new record is posted by the user, what would you do? To check if the new record's LETTER CODE already exists in database (by query) or just directly insert into database and let the database error indicates that the given LETTER CODE already exists?

#2
[eluser]umefarooq[/eluser]
First check the user guide for form validation and you callbacks to check that Letter code is there or not here is one example

http://ellislab.com/codeigniter/user-gui...#callbacks

#3
[eluser]ang89[/eluser]
[quote author="umefarooq" date="1251108583"]First check the user guide for form validation and you callbacks to check that Letter code is there or not here is one example

http://ellislab.com/codeigniter/user-gui...#callbacks[/quote]

Thanks for the reply, umefarooq.
Yes, I've been working with CI for a while and I already quite familiar with the form validation library.

But the thing is about design strategy. Do I have to check the existence of the primary key before insertion? Because to do this, I have to perform extra queries and this will impose extra overheads.

In the other hand, I can just directly insert that letter code into database. And if the database's trans_status() return false, I can say that the letter code already exists in database.

So, which one is commonly done in the real system?

PS: Sorry for asking this kind of question. It's easy to find help about how to use this and that (google it is), but when it comes to design decision, I have nowhere to ask.

#4
[eluser]alboyd[/eluser]
Isn't querying the trans_status or whatever you said causing the same "overhead" than checking up front and not doing the trans_status query?

And wouldn't it make more sense to stay within the validation loop by using a callback function than exiting the loop, receiving an error and trying to find a way back in?

#5
[eluser]ang89[/eluser]
[quote author="alboyd" date="1251111714"]Isn't querying the trans_status or whatever you said causing the same "overhead" than checking up front and not doing the trans_status query?

And wouldn't it make more sense to stay within the validation loop by using a callback function than exiting the loop, receiving an error and trying to find a way back in?[/quote]

Hi alboyd. This trans_status() thing that I talked about is the one defined in CI's DB library. It doesn't perform queries. It only checks whether the recent database transaction performed successfully or not, it is pretty light.

However, forget about this post. Now I realized that my question is pretty stupid. Off course the first one makes more sense. I can't judge that a record is already exists just because the DB insertion query is failed. Like that's the only reason why it would failed.

#6
[eluser]alboyd[/eluser]
Aah cool sorry I didn't know what that was - figured it was a mysql query...

Anyway these queries are done in fractions of a hundredth of a second mostly so I'd still do it with a callback function myself...


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.