Archive for the ‘Nette’ Category

Repeated forms on the same page

Tuesday, November 22nd, 2011

Often you want the user to have a form not just for one entity but for all of them on the same page. A form for each product in the same category, for example. I’m not talking about multiple different forms but multiple instances of the same form.

This topic has been discussed in the Nette forum so many times that it’s really a shame that it’s still missing in the documentation. Frustrated after an hour spent reading outdated forum posts, I offer here a complete solution based on one of the posts.


Refreshing current user’s identity

Wednesday, November 9th, 2011

Nette stores current user’s identity in session. There are a few situations when you want to reload the identity or change it completely. For example, the user updates his profile and you want the change reflected in the page header. Or a super administrator is allowed to re-authenticate as a different user without having to know the other user’s password.

NUser has a convenient method called setIdentity() but unfortunately, it’s declared as private so you can’t use it. The solution is either to loop through the current identity and update its fields one by one, or do it nice and clean with a custom authenticator. Let me show you.


Testing for unique value during form validation

Monday, November 7th, 2011

Let’s talk about what happens when a user signs up for a new account. He fills in his nickname, e-mail address and password and clicks Register. When validating the e-mail address you need to check for at least three things:

  1. the user provided one
  2. the syntax is all right – the e-mail is formally valid and can in fact exist (which doesn’t mean it really exists)
  3. the address hasn’t been used yet for some other account

So far not very difficult.

The tricky part comes when you allow the user to edit his profile. He can change any of the three values. When he submits the edit form the situation is one step more difficult than during registration. The e-mail address has in fact already been used – for the very user that is submitting the form. That is not the tricky part. The fun is how to get the user id when validating his e-mail address.


Tweaking your forms

Friday, September 9th, 2011

Nette forms documentation lacks information about enhancing your forms while still using the default renderer. I offer here a list of solutions for the most common needs, using getControlPrototype(), getLabelPrototype(), getSeparatorPrototype() and setOption().

IS NOT NULL trouble in Nette/Database

Monday, September 5th, 2011

Sooner or later, you’ll find yourself in a need of filtering out NULL values along with some other criterion. You’ve tried ->where(array('something' => $value, 'other_thing IS NOT NULL')) and got an error. I’m sure you’ve spent considerable time looking for some kind of negation that would work with NULL and I bet you’ve even tried to stick NSqlLiteral somewhere in there. I’m here to rescue you.