Tag Archives: forms
As I release my latest MooTools plugin, PlaceholderInput, that adds support for input placeholders, I decided to explain my solution, and what others are available.
An example of my implementation is seen on the Devthought sidebar, right next to this post. I’ll analyze next the different solutions that have been offered for this problem, and the upsides and downsides each has.
if ( comments_open() ) { ?>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';
}
}