This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

Validation / sfValidatorPropelUnique

Discussion specific to symfony 1.1.x stable

Validation / sfValidatorPropelUnique

by trontank » Tue Aug 05, 2008 8:01 pm

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:

<span name="php"><code><span style="color: #000000">
<span style="color: #0000BB"><?php </span><span style="color: #007700">class </span><span style="color: #0000BB">UserForm </span><span style="color: #007700">extends </span><span style="color: #0000BB">BaseFormPropel </span><span style="color: #007700">{      public function </span><span style="color: #0000BB">configure</span><span style="color: #007700">()   {     </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">validatorSchema</span><span style="color: #007700">[</span><span style="color: #DD0000">'username'</span><span style="color: #007700">]-></span><span style="color: #0000BB">setOption</span><span style="color: #007700">(</span><span style="color: #DD0000">'required'</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">);   }      public function </span><span style="color: #0000BB">setup</span><span style="color: #007700">()   {     </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">setWidgets</span><span style="color: #007700">(array(       </span><span style="color: #DD0000">'username'                      </span><span style="color: #007700">=> new </span><span style="color: #0000BB">sfWidgetFormInput</span><span style="color: #007700">(),       </span><span style="color: #DD0000">'password'                      </span><span style="color: #007700">=> new </span><span style="color: #0000BB">sfWidgetFormInput</span><span style="color: #007700">(),     ));     </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">setValidators</span><span style="color: #007700">(array(       </span><span style="color: #DD0000">'username'                      </span><span style="color: #007700">=> new </span><span style="color: #0000BB">sfValidatorString</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'max_length' </span><span style="color: #007700">=> </span><span style="color: #0000BB">128</span><span style="color: #007700">)),       </span><span style="color: #DD0000">'password'                      </span><span style="color: #007700">=> new </span><span style="color: #0000BB">sfValidatorString</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'max_length' </span><span style="color: #007700">=> </span><span style="color: #0000BB">128</span><span style="color: #007700">)),     ));     </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">validatorSchema</span><span style="color: #007700">-></span><span style="color: #0000BB">setPostValidator</span><span style="color: #007700">(       new </span><span style="color: #0000BB">sfValidatorPropelUnique</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'model' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'sfGuardUser'</span><span style="color: #007700">, </span><span style="color: #DD0000">'column' </span><span style="color: #007700">=> array(</span><span style="color: #DD0000">'username'</span><span style="color: #007700">)))     );     </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">widgetSchema</span><span style="color: #007700">-></span><span style="color: #0000BB">setNameFormat</span><span style="color: #007700">(</span><span style="color: #DD0000">'sf_guard_user[%s]'</span><span style="color: #007700">);     </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">errorSchema </span><span style="color: #007700">= new </span><span style="color: #0000BB">sfValidatorErrorSchema</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">validatorSchema</span><span style="color: #007700">);     </span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">setup</span><span style="color: #007700">();   }   public function </span><span style="color: #0000BB">getModelName</span><span style="color: #007700">()   {     return </span><span style="color: #DD0000">'sfGuardUser'</span><span style="color: #007700">;   } } </span><span style="color: #0000BB">?></span>
</span>
</code></span>

The modules register action looks like this:

<span name="php"><code><span style="color: #000000">
<span style="color: #0000BB"><?php </span><span style="color: #007700">public function </span><span style="color: #0000BB">executeRegister</span><span style="color: #007700">(</span><span style="color: #0000BB">$request</span><span style="color: #007700">) {     </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">form </span><span style="color: #007700">= new </span><span style="color: #0000BB">UserForm</span><span style="color: #007700">();     if (</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">getRequest</span><span style="color: #007700">()-></span><span style="color: #0000BB">isMethod</span><span style="color: #007700">(</span><span style="color: #DD0000">'post'</span><span style="color: #007700">)) {      </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">form</span><span style="color: #007700">-></span><span style="color: #0000BB">bind</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">getRequestParameter</span><span style="color: #007700">(</span><span style="color: #DD0000">'user'</span><span style="color: #007700">));      } } </span><span style="color: #0000BB">?></span>
</span>
</code></span>

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?
trontank
Senior Member
 
Posts: 178
Joined: Thu Jul 31, 2008 2:18 pm
Location: Germany

Re: Validation / sfValidatorPropelUnique

by trontank » Tue Aug 05, 2008 8:36 pm

Ok, I'm stupid. Obvious solution:

<span name="php"><code><span style="color: #000000">
<span style="color: #0000BB"><?php </span><span style="color: #FF8000">// method executeRegister </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">form</span><span style="color: #007700">-></span><span style="color: #0000BB">bind</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">getRequestParameter</span><span style="color: #007700">(</span><span style="color: #DD0000">'sf_guard_user'</span><span style="color: #007700">)); </span><span style="color: #0000BB">?></span>
</span>
</code></span>

Or in the form class:

<span name="php"><code><span style="color: #000000">
<span style="color: #0000BB"><?php $this</span><span style="color: #007700">-></span><span style="color: #0000BB">widgetSchema</span><span style="color: #007700">-></span><span style="color: #0000BB">setNameFormat</span><span style="color: #007700">(</span><span style="color: #DD0000">'user[%s]'</span><span style="color: #007700">); </span><span style="color: #0000BB">?></span>
</span>
</code></span>

Now i need to find out how to use sfValidatorPropelUnique for the new user's email address (model sfGuardUserProfile). Tomorrow...
trontank
Senior Member
 
Posts: 178
Joined: Thu Jul 31, 2008 2:18 pm
Location: Germany

Re: Validation / sfValidatorPropelUnique

by trontank » Tue Aug 05, 2008 9:15 pm

trontank wrote: 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.
trontank
Senior Member
 
Posts: 178
Joined: Thu Jul 31, 2008 2:18 pm
Location: Germany

Re: Validation / sfValidatorPropelUnique

by honkenhg » Tue Sep 02, 2008 5:57 am

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.

Code: Select all
$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.
honkenhg
Junior Member
 
Posts: 12
Joined: Mon Nov 05, 2007 2:09 pm

Re: Validation / sfValidatorPropelUnique

by kiewic » Wed Dec 17, 2008 11:09 am

:o~ :-o~ :eek: Great!
kiewic
Junior Member
 
Posts: 11
Joined: Wed Dec 17, 2008 11:08 am

Re: Validation / sfValidatorPropelUnique

by dipesh » Fri Jul 16, 2010 11:56 am

It helped a lot thanks. :P
dipesh
Junior Member
 
Posts: 2
Joined: Tue Jun 08, 2010 12:05 pm
Location: Nepal