Controller:
<?php
class UsersController extends AppController
{
public $uses = array('Users');
public function register()
{
if(!empty($this->data))
{
//save data
if($this->Users->save($this->data))
{
//$this->redirect("/users");
}
}
}
}
?>
View
<?php echo $form->create("Users", array('url' => array('controller' =>
'users', 'action' => 'register'))); ?>
<?php echo $form->hidden("action", array('value' => "register")); ?>
<table width='100%' cellspacing='2' cellpadding='2'>
<tr>
<td><strong>Username</strong></td>
<td><?php echo $form->input("username", array('label' => '')); ?></
td>
</tr>
<tr>
<td><strong>Password</strong></td>
<td><?php echo $form->input("Users.pass.1", array('type' =>
'password', 'label' => '')); ?></td>
</tr>
<tr>
<td><strong>Password Again</strong></td>
<td><?php echo $form->input("Users.pass.2", array('type' =>
'password', 'label' => '')); ?></td>
</tr>
<tr>
<td><strong>Email Address</strong></td>
<td><?php echo $form->input("Users.address.1", array('label' =>
'')); ?></td>
</tr>
<tr>
<td><strong>Repeat Address</strong></td>
<td><?php echo $form->input("Users.address.2", array('label' =>
'')); ?></td>
</tr>
<tr>
<td colspan='2'><?php echo $form->submit("Register"); ?></td>
</tr>
</table>
<?php echo $form->end(); ?>
On Jul 1, 5:25 am, Lamonte <sche...@gmail.com> wrote:
> When I submit the form:
>
> Array
> (
> [Users] => Array
> (
> [username] => test
> [email] => t...@gmail.com
> [password] => 098f6bcd4621d373cade4e832627b4f6
> )
>
> )
>
> Debug info outputs what it should output:
>
> users model
>
> <?php
> class Users extends AppModel
> {
> public $name = "Users";
> public $validate = array(
> 'address' => array(
> 'emailFieldsMatch' => array(
> 'rule' => 'emailFieldsMatch',
> 'message' => 'Email Fields do not match.',
> ),
> 'emailValid' => array(
> 'rule' => 'emailValid',
> 'message' => 'Invalid Email Address',
> ),
> ),
> 'username' => array(
> 'rule' => 'notEmpty',
> 'message' => 'Field Required',
> ),
> 'pass' => array(
> 'rule' => 'passFieldsMatch',
> 'message' => 'Password Fields do not match.',
> ),
> );
>
> public function beforeSave()
> {
> if(!empty($this->data) && isset($this->data['Users']['action']) &&
> ($this->data['Users']['action'] == 'register'))
> {
> //actual email address field.
> $this->data['Users']['email'] = $this->data['Users']['address'][1];
> $this->data['Users']['password'] = md5($this->data['Users']['pass']
> [1]);
>
> //we don't need these variables anymore delete it before cake
> attempts to save it
> unset($this->data['Users']['address']);
> unset($this->data['Users']['action']);
> unset($this->data['Users']['pass']);
>
> pr($this->data);
> }
> }
>
> /** -------------------------------------
> Custom Validation Rules
> ------------------------------------- **/
>
> public function passFieldsMatch($data)
> {
> return $data['pass'][1] == $data['pass'][2];
> }
>
> public function emailFieldsMatch($data)
> {
> return $data['address'][1] == $data['address'][2];
> }
>
> public function emailValid($data)
> {
> $regex = "/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*
> [0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/";
> return preg_match($regex, $data['address'][1]);
> }}
>
> ?>
--~--~---------~--~----~------------~-------~--~----~
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:
Post a Comment