This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » legacy support » symfony 1.1 » Validation / sfValidatorPropelUnique
Validation / sfValidatorPropelUnique [message #57959] Tue, 05 August 2008 20:01 Go to next message
trontank  is currently offline trontank
Messages: 179
Registered: July 2008
Location: Germany
Senior Member
After reading the docs i played around with forms in Symfony 1.1 and came to the conclusion that 1.1 custom form validation is quite complicated and not easy.

I am using the sfGuardPlugin to manage users, i have everything up and running and even the sfGuardUserProfile stuff works, at least in the backend. What I need now is a module that handles the registration of new users.

Problem #1: The username has to be unique. I then created a user module in frontend and a very simple form:

<?php
class UserForm extends BaseFormPropel
{
  
  public function 
configure()
  {
    
$this->validatorSchema['username']->setOption('required'true);
  }
  
  public function 
setup()
  {
    
$this->setWidgets(array(
      
'username'                      => new sfWidgetFormInput(),
      
'password'                      => new sfWidgetFormInput(),
    ));

    
$this->setValidators(array(
      
'username'                      => new sfValidatorString(array('max_length' => 128)),
      
'password'                      => new sfValidatorString(array('max_length' => 128)),
    ));

    
$this->validatorSchema->setPostValidator(
      new 
sfValidatorPropelUnique(array('model' => 'sfGuardUser''column' => array('username')))
    );

    
$this->widgetSchema->setNameFormat('sf_guard_user[%s]');

    
$this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    
parent::setup();
  }

  public function 
getModelName()
  {
    return 
'sfGuardUser';
  }
}
?>


The modules register action looks like this:

<?php
public function executeRegister($request)
{
    
$this->form = new UserForm();
    if (
$this->getRequest()->isMethod('post')) { 
    
$this->form->bind($this->getRequestParameter('user')); 
    }
}
?>


After entering an existing username and submitting the form it looks like the sfValidatorPropelUnique check never happens. I do not get any validation error nor does a query that checks that appear in the debugger. Any idea what I am doing wrong here?
Re: Validation / sfValidatorPropelUnique [message #57962 is a reply to message #57959 ] Tue, 05 August 2008 20:36 Go to previous messageGo to next message
trontank  is currently offline trontank
Messages: 179
Registered: July 2008
Location: Germany
Senior Member
Ok, I'm stupid. Obvious solution:

<?php
// method executeRegister
$this->form->bind($this->getRequestParameter('sf_guard_user'));
?>


Or in the form class:

<?php
$this
->widgetSchema->setNameFormat('user[%s]');
?>


Now i need to find out how to use sfValidatorPropelUnique for the new user's email address (model sfGuardUserProfile). Tomorrow...
Re: Validation / sfValidatorPropelUnique [message #57967 is a reply to message #57962 ] Tue, 05 August 2008 21:15 Go to previous messageGo to next message
trontank  is currently offline trontank
Messages: 179
Registered: July 2008
Location: Germany
Senior Member
trontank wrote on Tue, 05 August 2008 20:36

Now i need to find out how to use sfValidatorPropelUnique for the new user's email address (model sfGuardUserProfile). Tomorrow...


Sigh! I figured out how to do this, too. Me thinks I am not the only one who needs a module that handles user registration following SF 1.1 standards. As soon as I got a working solution I'll post some details here.
Re: Validation / sfValidatorPropelUnique [message #59942 is a reply to message #57959 ] Tue, 02 September 2008 05:57 Go to previous messageGo to next message
honkenhg  is currently offline honkenhg
Messages: 12
Registered: November 2007
Junior Member
Hi there, I stumbled over your post earlier on in hope to find the solution, so following your thought I just post what did work for me. This code checks the email and username for uniqueness and compares two password fields with each other.

$this->validatorSchema->setPostValidator(
 	new sfValidatorAnd(array(
 		new sfValidatorPropelUnique(array('model' => 'sfGuardUser', 'column' => array('username'))),
 	        new sfValidatorSchemaCompare('password1', sfValidatorSchemaCompare::EQUAL, 'password2'),
 		new sfValidatorPropelUnique(array('model' => 'SfGuardUserProfile', 'column' => array('email')))
 )));


Hope it helps.
Re: Validation / sfValidatorPropelUnique [message #67953 is a reply to message #59942 ] Wed, 17 December 2008 10:09 Go to previous messageGo to next message
kiewic  is currently offline kiewic
Messages: 6
Registered: December 2008
Junior Member
Surprised Great!
Re: Validation / sfValidatorPropelUnique [message #102583 is a reply to message #67953 ] Fri, 16 July 2010 11:56 Go to previous message
dipesh  is currently offline dipesh
Messages: 2
Registered: June 2010
Location: Nepal
Junior Member
It helped a lot thanks. Razz
Previous Topic:Save values from custom form to Database
Next Topic:sfWebBrowser and sfCurlAdapter redirection support
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software