Custom fields in edit/new admin generator views in Symfony
Let’s say we want to create a field to set the password in plain text from the admin panel, then convert it to sha1 before saving it. Our hypothetical module will be located in backend/modules/admins/, which is a Symfony admin generator module for an AdminUser model.
First we define the getters and setters for the plaintext password in the AdminUser model:
public function setPasswordPlain($plain) { if(! empty($plain)) $this->setPassword(sha1($plain)); } public function getPasswordPlain() { return ""; }
Then, in generator.yml we add this field to form sections, using the display parameter:
form:
display: [login, password_plain, email]If we try the generator now, we’ll get an error message telling us that password_plain widget does not exist, which is true. The new admin generator uses the great Symfony 1.2 forms system, which treats a form as a set of widgets and validators.
If we inspect the lib directory of our generated module we’ll notice the file called adminsGeneratorConfiguration.class.php. This file extends BaseAdminsGeneratorConfiguration which, if we look closely, configures what form is related to this generated module:
public function getFormClass() { return 'AdminUserForm'; }
What we have to do is obvious. We extend AdminUserForm, and we override that method to return the name of our new class. In the same lib directory we create the form:
class myAdminUserForm extends BaseAdminUserForm { public function configure() { parent::configure(); $this->setWidget('password_plain', new sfWidgetFormInput()); $this->setValidator('password_plain', new sfValidatorString(array('max_length' => 255, 'required' => false))); } }
and finally we alter the adminsGeneratorConfiguration accordingly:
class adminsGeneratorConfiguration extends BaseAdminsGeneratorConfiguration { public function getFormClass() { return 'myAdminUserForm'; } }
-
Hi, this is all I need but I have a problem. The _edit action never call to the method setPasswordPlain and off course it don’t store the value in the database when I save. Could you help me please?
-
carl did you ever resolve this? i have the same problem…
-
-
Just a tiny hint: use the generator.yml to avoid messing around in the libraries. You can use a different form by simply adding the following to your form configuration in generator.yml:
class: myAdminUserForm -
Neat web site! hope to visit soon:)
Hi!
You may probably be very interested to know how one can manage to receive high yields on investments.
There is no initial capital needed.
You may begin to get income with a money that usually is spent
for daily food, that’s 20-100 dollars.
I have been participating in one project for several years,
and I’ll be glad to let you know my secrets at my blog.
Please visit my pages and send me private message to get the info.
P.S. I earn 1000-2000 per day now.
http://www.theblogmoney.com