Thursday, June 3, 2010

Re: Registration page- password filed making conlict

another trick is to use 2 "tmp" fields:
pwd and pwd_repeat

and only hash + save the new password to the real field "password"
if sth has been submitted in both fields

this can happen in the beforeValidate() or beforeSave() method of the
model


On 3 Jun., 16:57, John Andersen <j.andersen...@gmail.com> wrote:
> When you leave the password field empty in the form, then upon
> submitting the form, the Auth component will hash the password field,
> thus it will not be empty when it reaches the model.
>
> You just need to check that the second password is not empty and that
> the hashed version of the second password is equal to the first
> password, that should ensure that a blank first password is not given.
>
> Enjoy,
>    John
>
> On Jun 3, 11:03 am, Ambika Kulkarni <ambikakulkarn...@gmail.com>
> wrote:
>
> > Hi All,
>
> > I have a user model as below
>
> > class User extends AppModel {
> >     var $name = 'User';
> >     var $primaryKey = 'user_id';
> >    //validation for the registration fields
> >     var $validate = array(
> >                     'name' => array(
> >                              'notempty' => array(
> >                                            'rule' => array(
> >                                                      'alphaNumeric'),
> >                                                      'required' =>
> > true,
> >                                                      'allowEmpty' =>
> > false,
> >                                                     'message' =>
> > 'Please enter a value for the First Name field')),
>
> >                     'telephone' => array(
> >                                    'rule' => array(
> >                                              'alphaNumeric'),
> >                                              'required' => true,
> >                                              'allowEmpty' => false,
> >                                              'message' => 'Please
> > enter a value for the  Contact Number field'),
> >                     'email' => array(
> >                             'rule1' => array(
> >                                            'rule' => 'notEmpty',
> >                                            'required' => true,
> >                                            'allowEmpty' => false,
> >                                            'message' => 'Please enter
> > a value for the Email field',
> >                                             'last' => true),
> >                             'rule2' => array(
> >                                             'rule' => 'email',
> >                                             'message' => 'Please enter
> > a proper Email Id'
> >                                             )),
> >                     'username' => array(
> >                                    'notempty' => array(
> >                                                   'rule' =>
> > 'notEmpty',
> >                                                    'required' => true,
> >                                                    'allowEmpty' =>
> > false,
> >                                                    'message' => 'User
> > name cannot be empty',
> >                                                     'last' => true
> >                                                ),
> >                                    'unique' => array(
> >                                       'rule' => array('checkUnique',
> > 'username'),
> >                                       'message' => 'User name taken.
> > Use another'
> >                                    )
> >                                 ),
> >                      'password' => array(
> >                                     'notempty' => array(
> >                                                    'rule' =>
> > 'alphaNumeric',
> >                                                    'required' => true,
> >                                                    'allowEmpty' =>
> > false,
> >                                                    'message' =>
> > 'Password cannot be empty.',
> >                                                    'last' => true
>
> >                                                ),
> >                                     'passwordSimilar' => array(
> >                                                            'rule' =>
> > 'checkPasswords',
> >                                                            'message'
> > => 'Different password re entered.'
> >                                                          )
> >                                     ));
> >    function checkUnique ($data, $fieldName) {
> >         $valid = false;
> >         if ( isset($fieldName) && $this->hasField($fieldName)) {
> >             $valid = $this->isUnique(array($fieldName => $data));
> >         }
> >         return $valid;
> >     }
> >    function checkPasswords($data) {
> >            if($data['password'] == $this->data['User']
> > ['password2hashed'])
> >               return true;
> >            return false;
>
> > }
> > }
>
> > and in the register.ctp file i have number of fileds but i giving the
> > code snippet only for password filed
>
> >  <?php  if($form->isFieldError('User.password')) {
> >                 e($form->error ('User.password',  null,
> > array( 'class' => 'failure')));  }
> > ?>
> > <?php echo $form->create('User', array( 'action' => 'register'));
> >   ?>
> >   <table width="100%" cellpadding="0" cellspacing="3" border="0"
> > class="form_text">
> >        <tr>
> >                 <td align="right"><font color="red">*&nbsp;</font>
> > Password&nbsp;</td>
> >                 <td><?php e($form->password('password',  array('value'
> > => '', 'style' => 'width: 30%'))); ?></td>
> >        </tr>
> >         <tr>
> >                 <td align="right"><font color="red">*&nbsp;</font>
> > Confirm Password&nbsp;</td>
> >                 <td><?php e($form->password('password2',
> > array( 'style' => 'width: 30%')));  ?> </td>
> >        </tr>
> > </table>
> > <?php e($form->end()); ?>
>
> > In the controller file
> > class UsersController extends AppController {
> >     var $uses = array ('User','Group', 'GroupUser');
> >     var $components = array('Auth');
> >     var $helpers = array('Html', 'Form');
> >     function register() {
> >        if (!empty($this->data)) {
> >             //$this->data['User']['password2hashed'] = $this->Auth->password($this->data['User']['password2']);
>
> >             //$this->set('password', '');
> >             // if (isset($this->data['User']['password2'])) {
> >                 //$this->data['User']['password2hashed'] = $this->User->mysqlpass($this->data['User']['password']);
>
> >                //$this->data['User']['password2hashed'] = $this->Auth->password($this->data['User']['password2']);
>
> >                // }
> >             $this->User->create();
> >             if ($this->User->save($this->data)) {
> >                     $this->Session->setFlash('Congratulations! You
> > have signed up!');
> >                     $this->Auth->loginRedirect = array('controller' =>
> > 'campaigns', 'action' => 'step_one');
> >             }
> >             else {
> >                $this->Session->setFlash(__('There was an error signing
> > up. Please, try again.', true));
> >               //$this->data = null;
> >             }  }  }}
>
> >  Problem is : the validation for password field is not happening. If
> > pwd field is blank then it is registering the user. with a blank
> > password field.
>
> > Is their any mistake in the model file.
>
> > Please help me i stucked badly.
>
> > thanks and regards
> > Ambika

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: