验证错误未显示

时间:2009-11-05 13:00:21

标签: validation cakephp

我正在尝试在用户注册我的应用程序时对其进行验证。什么都没有设置为validationErrors,任何人都可以帮我解决这个问题很奇怪吗?

这是我的MembersController

<?php
    class MembersController extends AppController {
var $name = 'Members';

var $components = array('RequestHandler','Uploader.Uploader');

function beforeFilter() {
    parent::beforeFilter();
    $this->layout = 'area';
    $this->Auth->allow('register');
    $this->Auth->loginRedirect = array('controller' => 'members', 'action' => 'dashboard');
    $this->Uploader->uploadDir = 'files/avatars/';
    $this->Uploader->maxFileSize = '2M';
}


function login() {}

function logout() {
     $this->redirect($this->Auth->logout());
}

function register() {
    if ($this->data) {
        if ($this->data['Member']['psword'] == $this->Auth->password($this->data['Member']['psword_confirm'])) {
            $this->Member->create();
            if ($this->Member->save($this->data)) {
                $this->Auth->login($this->data);
                $this->redirect(array('action' => 'dashboard'));
            } else {
                $this->Session->setFlash(__('Account could not be created', true));
                $this->redirect(array('action' => 'login'));
                pr($this->Member->invalidFields());
            }
        }
    }
}

} ?&GT;

会员模型

<?php

类成员扩展AppModel {

var $name = 'Member';
var $actsAs = array('Searchable'); 
var $validate = array(
    'first_name' => array(
        'rule' => 'alphaNumeric',
        'required' => true,
        'allowEmpty' => false,
        'message' => 'Please enter your first name'
    ),
    'last_name' => array(
        'rule' => 'alphaNumeric',
        'required' => true,
        'allowEmpty' => false,
        'message' => "Please enter your last name"
    ),
    'email_address' => array(
        'loginRule-1' => array(
            'rule' => 'email',  
            'message' => 'please enter a valid email address',
            'last' => true
         ),
        'loginRule-2' => array(
            'rule' => 'isUnique',  
            'message' => 'It looks like that email has been used before'
        )
    ),
    'psword' => array(
        'rule' => array('minLength',8),
        'required' => true,
        'allowEmpty' => false,
        'message' => 'Please enter a password with a minimum lenght of 8 characters.'
    )
);
var $hasOne = array('Avatar');

var $hasMany = array(
    'Favourite' => array(
        'className' => 'Favourite',
        'foreignKey' => 'member_id',
        'dependent' => false
    ),
    'Friend' => array(
        'className' => 'Friend',
        'foreignKey' => 'member_id',
        'dependent' => false
    ),
    'Guestbook' => array(
        'className' => 'Guestbook',
        'foreignKey' => 'member_id',
        'dependent' => false
    ),
    'Accommodation'
);

var $hasAndBelongsToMany = array('Interest' => array(
        'fields' => array('id','interest')
    )
);


function beforeSave($options = array()) {
    parent::beforeSave();
    if (isset($this->data[$this->alias]['interests']) && !empty($this->data[$this->alias]['interests'])) {
        $tagIds = $this->Interest->saveMemberInterests($this->data[$this->alias]['interests']);
        unset($this->data[$this->alias]['interests']);
        $this->data[$this->Interest->alias][$this->Interest->alias] = $tagIds;
    }
    $this->data['Member']['first_name'] = Inflector::humanize($this->data['Member']['first_name']);
    $this->data['Member']['last_name'] = Inflector::humanize($this->data['Member']['last_name']);
    return true;
}   

} ?&GT;

login.ctp

    <div id="login-form" class="round">
    <h2>Sign In</h2>
    <?php echo $form->create('Member', array('action' => 'login')); ?>
        <?php echo $form->input('email_address',array('class' => 'login-text',
                                                    'label' => array('class' => 'login-label')
                                                ));?>
        <?php echo $form->input('psword'    ,array('class' => 'login-text',
                                            'label' => array('class' => 'login-label','text' => 'Password')
                                        ))?>
    <?php echo $form->end('Sign In');?> 
</div>  
<div id="signup-form" class="round">
    <h2>Don't have an account yet?</h2>
    <?php echo $form->create('Member', array('action' => 'register')); ?>
        <?php echo $form->input('first_name',array('class' => 'login-text',
                                                    'label' => array('class' => 'login-label')
                                                ));?>
        <?php echo $form->input('last_name',array('class' => 'login-text',
                                                    'label' => array('class' => 'login-label')
                                                ));?>
        <?php echo $form->input('email_address',array('class' => 'login-text',
                                                    'label' => array('class' => 'login-label')
                                                ));?>
        <?php echo $form->input('psword'    ,array('class' => 'login-text',
                                            'label' => array('class' => 'login-label','text' => 'Password')
                                        ))?>
        <?php echo $form->input('psword_confirm'    ,array('class' => 'login-text',
                                                        'label' => array('class' => 'login-label','text' => 'Confirm'),
                                                        'div' => array('style' => ''),
                                                        'type' => 'password'  
                                        ))?>
    <?php echo $form->end('Sign In');?>
</div>

1 个答案:

答案 0 :(得分:2)

我相信你的问题在这里:

$this->redirect(array('action' => 'login'));
pr($this->Member->invalidFields());

验证错误旨在显示在表单上的相应字段下方。但是,您不是继续并尝试显示表单,而是将用户重定向到其他页面。

如果删除上面的两行,则在验证失败时,它应在表单的字段下显示验证错误。