Wednesday, November 18, 2009

Re: Cake PHP Form validation

Hi jburns,

Thanks a lot for the reply. I've tried adding your code but it's still not working.

This is what happened:
- if name, email or username is empty, it will give an error message, something like "Alphabets and numbers only" or "This field cannot be left blank". However, there is no error message for password1 / password2.
- then I try to input everything. And when I click on save, the page doesn't load anything. It's still on the same page, and my 'users' table is not updated.
- if I remove the validation for password1 & password2 in the model's $validate, it saves the data to db and redirect to other page (working fine).

I'm not sure what's wrong with this.

On Wed, Nov 18, 2009 at 10:10 PM, jburns <jeremyburns@me.com> wrote:
I forgot something...you need to clear out the password1 and password2
fields before saving. Use this:

function beforeSave() {
               if (isset($this->data['User']['password1'])):
                       $this->data['User']['password'] =  Security::hash($this->data
['User']['password1'], null, true);
                       unset($this->data['User']['password1']);
               endif;

               if (isset($this->data['User']['password2'])):
                       unset($this->data['User']['password2']);
               endif;

               return true;
       }

On Nov 18, 2:06 pm, jburns <jeremybu...@me.com> wrote:
> First, a general point. Rather than having separate entries for label
> and the input, try:
> echo $form->input('xxx', array('label'=>'Xxxx'));
> (where xxx is the name of your field and Xxxx is your label).
>
> This works for me when confirming that passwords match:
>
> 'password1'=>array(
>                         'password_1'=>array(
>                                 'rule'=>'notEmpty',
>                                 'message'=>'Please enter a password.',
>                                 'required'=>true,
>                                 'last'=>true),
>                         'password_2'=>array(
>                                 'rule'=>array('between', 8, 20),
>                                 'message'=>'Your password must be between 8 and 20 characters
> long.'),
>                 ),
>                 'password2'=>array(
>                         'match'=>array(
>                                 'rule'=>'validatePasswdConfirm',
>                                 'required'=>true,
>                                 'allowEmpty'=>false,
>                                 'message'=>'Your passwords do not match')
>                 )
>
> You'll notice that the rule for validating password2 is
> 'validatePasswdConfirm'. Place this function in your model:
>
>         function validatePasswdConfirm($data) {
>                 if ($this->data['User']['passwd'] !== $data['passwd_confirm']):
>                         return false;
>                 endif;
>
>                 return true;
>         }
>
> For the role field, it looks as if your syntax is a bit muddled. Try
> something along these lines:
>
> echo $form->input('role', array('type'=>'select', 'empty'=>true,
> 'options'=>$roles, 'label'=>'Role'));
>
> $options is the array that contains the allowed values and can be set
> in the controller - particularly useful if you are extracting them
> from a table.
>
> Hope this helps.
>
> On Nov 18, 10:44 am, Code Buzz <codeb...@gmail.com> wrote:
>
>
>
> > Hi all,
>
> > I'm a newbie at cakePHP, and still at the learning phase.
>
> > I was trying to make a user management system with cakephp, where we
> > can add/edit/delete user. But I am stuck in the validation part. I
> > hope someone can help me on this.
>
> > I have this 'users' table:
> > =========================
> > CREATE TABLE `users` (
> >   `id` int(11) NOT NULL AUTO_INCREMENT,
> >   `username` varchar(255) NOT NULL,
> >   `password` varchar(32) NOT NULL,
> >   `email` varchar(80) NOT NULL,
> >   `name` varchar(255) NOT NULL DEFAULT '',
> >   `designation` varchar(255) DEFAULT NULL,
> >   `role` varchar(30) NOT NULL,
> >   `print_perm` int(1) NOT NULL,
> >   `disabled` int(1) NOT NULL DEFAULT '0',
> >   PRIMARY KEY (`id`)
> > ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
>
> > and I have this form to add the user:
> > =========================
> >   echo $form->create('User');
> >   echo $form->input('username');
> >   echo $form->input('email');
> >   echo $form->input('name');
> >   echo $form->input('designation');
> >   $options=array('admin'=>'Administrator','user'=>'User');
> >   echo $form->label('Role');
> >   echo $form->select('role',$options);
> >   echo $form->label('Password');
> >   echo $form->password('password1');
> >   echo $form->label('Confirm Password');
> >   echo $form->password('password2');
> >   echo "<br>";
> >   echo $form->checkbox('print_perm');
> >   echo "<span class='text-bold'> Can Print?</span><br><br>";
> >   echo $form->input('id', array('type'=>'hidden'));
> >   echo $form->button('Save', array('type' => 'submit'));
> >   echo $form->button('Reset', array('type' => 'reset'));
> >   echo $form->end();
>
> > I put this validation on my model:
> > =========================
> >     var $validate = array(
> >         'username' => array(
> >             'alphaNumeric' => array(
> >                 'rule' => 'alphaNumeric',
> >                 'message' => 'Alphabets and numbers only'
> >                 ),
> >         ),
> >         'email' => 'email',
> >         'name' => VALID_NOT_EMPTY,
> >         'role' => VALID_NOT_EMPTY,
> >         'password' => VALID_NOT_EMPTY,
> >     );
>
> > it is not working for the 'role' and 'password' field. I have
> > 'password1' and 'password2' in the view, while my db field is
> > 'password'. I'm not sure what to do to make this work.
>
> > Any help is appreciated. Thank you.

--

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=.



--

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=.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate