set username equal to email_address in sfDoctrineGuardPlugin

442 Views Asked by At

in the table sf_guard_user, the email_address and username are requiered, but i want to use the email as username

so, i'm thinking to remove one of this field from the form, but as it are required, i always get the error, and i'm trying to set the username = email_address or vice versa, i've tried in the action, form, and model, but no success....

thought that writing a function in the sfGuardUser model with the name getUsername() and returning the email was enough, but no........

is this possible?? how can i do it??

thanks

2

There are 2 best solutions below

1
On BEST ANSWER

Of cause it is possible. You don't even have to mess with schema. Just store email in both username and email_address.

class sfGuardUserForm extends PluginsfGuardUserForm
{
  public function doSave($con = null)
  {
    //update object with form values
    $this->updateObject();
    $this->getObject()->setUsername($this->getObject()->getEmailAddress());

    return parent::doSave($con);
  }
}
0
On

Overriding username field in the Object::save() function is much better way - so that it handles all forms.

In you forms, you override the validator for username widget. You can also hide the widget if you want.

class sfGuardUserAdminForm extends BasesfGuardUserAdminForm
{

  public function setup()
  {
    parent::setup();
    $this->setWidget('username', new sfWidgetFormInputHidden());
    $this->setValidator('username', new sfValidatorPass());
  }

}

and

class sfGuardUser extends PluginsfGuardUser
{
  public function save(Doctrine_Connection $conn = null){
    $this->setUsername($this->getEmailAddress());
    parent::save($conn);
  }
}