CakePHP 2-验证密码字段 [英] CakePHP 2 - Validating password fields

查看:71
本文介绍了CakePHP 2-验证密码字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Cakephp 2验证存在一些问题。

I have some problem with Cakephp 2 validation.

我正在尝试验证编辑表单上的多个字段。其中一些是密码并确认密码字段。

I am trying to validate several fields on an Edit form. Some of them are a password and confirm password fields.

我只想同时提供两个都进行验证。如果它们为空,我不会更改密码,但是如果用户将其覆盖,我想验证它们是否具有minLength或密码是否匹配。

I want to validate both only if they are supplied. If they are empty I dont change the password, but if the user writes over them I would like to validate if they have a minLength or the passwords matchs.

代码:

'pwd' => array(
            'length' => array(
                'rule'      => array('between', 8, 40),
                'message'   => 'Your password must be between 8 and 40 characters.',
                'allowEmpty' => true
            ),
        ),
'pwd_repeat' => array(
            'length' => array(
                'rule'      => array('between', 8, 40),
                'message'   => 'Your password must be between 8 and 40 characters.',
                'allowEmpty' => true
            ),
            'compare'    => array(
                'rule'      => array('validate_passwords'),
                'message'   => 'The passwords you entered do not match.',
                'allowEmpty' => true
            ),

我不知道是否必须在控制器中的edit()函数上定义一些规则,还是应该足够,但是我的代码却不够

I dont know if I have to define some rules on edit() function in the controller or it should be enough, but my code is not working.

谢谢!

编辑:(控制器代码)

public function edit($id = null) {
        if (!$this->User->exists($id)) {
        throw new NotFoundException(__('Usuario incorrecto'));
    }
    if ($this->request->is('post') || $this->request->is('put')) {

                if ($this->User->save($this->request->data)) {
                    $this->Session->setFlash(__('El usuario ha sido actualizado.'));
                    return $this->redirect(array('action' => 'index'));
                } else {
                    $this->Session->setFlash(__('El usuario no ha podido actualizarse. Por favor, inténtelo de nuevo.'));      
                }
                unset($this->request->data['User']['pwd']);
                unset($this->request->data['User']['pwd_repeat']);
    } else {
        $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
        $this->request->data = $this->User->find('first', $options);
    }
    $roles = $this->User->Role->find('list');
    $this->set(compact('roles'));
}

(查看代码)

<div id="contenedor" class="users form">
<?php echo $this->Form->create('User', array('name' => 'form')); ?>
<fieldset>
    <legend><?php echo __('Editar Usuario'); ?></legend>
<?php
    echo $this->Form->input('id');
    echo $this->Form->input('username', array('label' => __('Usuario')));
    echo $this->Form->input('pwd', array('label' => __('Contraseña'), 'type' => 'password', 'name' => 'pass', 'onKeyUp' => 'habilita()', 'value' => ''));
            echo $this->Form->input('pwd_repeat', array('label' => __('Repite Contraseña'), 'type' => 'password', 'name' => 'rpass', 'disabled' => 'disabled'));
    echo $this->Form->input('firstname', array('label' => __('Nombre')));
    echo $this->Form->input('lastname', array('label' => __('Apellidos')));
    echo $this->Form->input('telephone', array('label' => __('Teléfono')));
    echo $this->Form->input('email', array('label' => __('Email')));
    echo $this->Form->input('role_id', array('label' => __('Rol')));
?>
</fieldset>
<?php echo $this->Form->end(__('Aceptar')); ?>

推荐答案

制定这样的验证规则

'pwd' => array(
    'length' => array(
        'rule'      => array('between', 8, 40),
        'message'   => 'Your password must be between 8 and 40 characters.',
    ),
),
'pwd_repeat' => array(
    'length' => array(
        'rule'      => array('between', 8, 40),
        'message'   => 'Your password must be between 8 and 40 characters.',
    ),
    'compare'    => array(
        'rule'      => array('validate_passwords'),
        'message' => 'The passwords you entered do not match.',
    )
)

您的validate_passwords函数应该是这样的。

And your validate_passwords function should be like this.

public function validate_passwords() {
    return $this->data[$this->alias]['pwd'] === $this->data[$this->alias]['pwd_repeat']
}

这篇关于CakePHP 2-验证密码字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆