This stuff is mostly taken from
http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html

Here's the model:

class User extends UserModel {
    public $name = 'User';
    public $validate = array(
        'username' => array(
                'rule1' => array(
                        'rule' => 'alphaNumeric',
                        'required' => true,
                        'allowEmpty' => false,
                        'message' => 'Username must be specified and
be alphanumeric'),
                'rule2' => array(
                        'rule' => array('minLength', 4),
                        'message' => 'Username must have at least 4
characters')));

Here's the controller:
class UsersController extends AppController {
    public function add() {
        if ($this->request->isPost()) {
            $this->User->create();
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been
saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be
saved. Please, try again.'));
                $this->set('errors', $this->User->validationErrors);
                $this->set('data', $this->request->data);
            }
        }
    }


Here's the view Users/add.ctp
<div class="users form">
<?php echo $this->element('form_errors', array('errors' => $errors)); ?
>
<?php echo $this->Form->create('Admin');?>
    <fieldset>
        <legend><?php echo __('Add User'); ?></legend>
    <?php
        echo $this->Form->input('username');
        echo $this->Form->input('password');
        echo $this->Form->input('role', array(
            'options' => array('admin' => 'Admin', 'author' =>
'Author')
        ));
    ?>
    </fieldset>
<?php echo $this->Form->end(__('Submit'));?>
</div>

and here's View/Elements/form_errors.ctp:
<?php

if (!empty($errors)) { ?>
<?php var_dump($errors); ?>
<?php echo '<br>'; var_dump($data); ?>
<div class="errors">
    <h3>There are <?php echo count($errors); ?> error(s) in your
submission:</h3>

    <ul>
        <?php foreach ($errors as $field => $error) { ?>
<?php if (is_array($error)): ?>
        <li><?php echo $error[0]; ?></li>
<?php else: ?>
        <li><?php echo $error; ?></li>
<?php endif ?>
        <?php } ?>
    </ul>
</div>
<?php } ?>


Then I visit users/add in my browser. No matter what I enter, after
clicking submit I get:
The user could not be saved. Please, try again.
array(1) { ["username"]=> array(1) { [0]=> string(46) "Username must
be specified and be alphanumeric" } }
array(1) { ["Admin"]=> array(3) { ["username"]=> string(0)
"" ["password"]=> string(0) "" ["role"]=> string(5) "admin" } }
There are 1 error(s) in your submission:

    Username must be specified and be alphanumeric


Did I do anything wrong? Please help.

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to