Symfony – no error message on empty values in not required fields
It's been a long time since I wrote my last post, but I kept myself busy with work. I'm just working with another web 2.0 project (yes, there will be another web 2.0 project
, which was written using symfony framework. I need to admit that my previous experience with different frameworks was pretty good, but symfony is different and I'm really excited about it. In a way it is similar to CakePHP using MVC patter but it has features, which I couldn't find in Cake. I mean symfony has its pros and cons but I find it really easy to build database models and modules using CLI and ORM. Propel is probably not the best abstraction layer, but I was reading on Doctrine site (which I personally find better than Propel) that it will be included in the new 1.2 release. I don't want to write a lot about symfony here, but rather solve a validation problem I encountered during development.
Symfony has a really powerful configuration system, in which you can also define your validation rules. My problem was as follows, I have 3 different access control levels and for one of them an input field is required and for the rest not. Let's describe it with an example of a company.
You have managers, executives and simple workers. To validate a manager we generated codes which we later distributed between them. Executives and simple workers didn't have to use a validation code. Therefore a simple input field of type text was provided only if a person registering on our site chose the position manager. Therefore I couldn't make this field as required because it was only required for a certain group of people registering on our site.
The validation rule was simple. I compared the code provided during registration with the one saved in the database and returned true or false according to the case. The problem I encountered was in the case that if a person chose manager and did not provide any input (an empty field), the error message was not displayed at all. I mean you could probably solve this problem using JavaScript but server side validation is always important. Researching the core code of symfony I found a place where this case is not considered an error. I could possibly change the core, but I didn't want to do that only for one case. I mean, if it would be a problem with many fields I would probably consider it necessary, but I've found a solution for that problem without changing the core code. Here is how I accomplished it.
I assume that you are familiar with symfony framework, therefore I wont explain the whole process from the beginning. In my /apps/myproject/lib folder I created a class extending sfValidation class, where I placed the database check. Unfortunately, defining additional string validation rules ($string == null etc.) in that class did not solve my problem that an error did not appear when the field was empty.
I did some more reading and I've found a really interesting solution. In your action.class.php file besides executeXXX() methods you can define additional validateXXX() methods, which are executed before executeXXX() methods. I placed a simple string validation rule in this method (with min value of 1) and it works like a charm right now. Below you can see my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public function validateIndex() { //manager code validation if($this->getRequestParameter('position') == 'manager') { $managerCode = $this->getRequestParameter('manager_code'); $val = new sfStringValidator(); $val->initialize($this->getContext(), array( 'min' => 1, )); if (!$val->execute($managerCode)) { $this->getRequest()->setError('managerCode', $this->getContext()->getI18N()->__('You have to fill out the code', null, 'user')); return false; } return true; } } |
As I said before this approach might get pretty annoying if you are dealing with multiple fields but for me it was just this one. I hope this solution will help you during your development. As usually I'm waiting for your feedback. Cheers!
Pages
Categories
Blogroll
- <?PHPDeveloper.org
- <raphael.on.php/>
- Phly, boy, phly
- Recess!
- techPortal
- Zend Casts
- Zend Developer Zone
Archive
- May 2010
- September 2009
- May 2009
- February 2009
- November 2008
- August 2008
- July 2008
- June 2008
- May 2008