First, excuse me for my poor english.
I'm using Cake 1.2.6311. I'm having a weird problem validating some
fields from my model.
The situation is like that:
- One model "users" with validation rules.
- "Users" controller with 2 basic actions: 'edit' & 'changepassword'.
- The problem: in the action "changepassword" where I'm only showing
the field "password" (and hidden ID field) when the form is submited
I'm obtaining a connection reset.
The weird thing is:
- If I add this two lines, in which I set the value for the "email"
into the ['data'] array the forms submits OK and the validation is
triggered.
------------------------------------------------------------------
$pp = $this->User->read( null, $id );
$this->data['User']['email'] = $pp['User']['email'];
------------------------------------------------------------------
- If the only showed field is "email" the form is submited OK and the
validation triggered as expected without manually loading the
"password" field to the "data" array. So I guess this is not a problem
related with some requirement of having all "validated" fields in the
form.
My question is:
Why if I only show the "email" field everything works fine. But with
the field "password" I've to use the workaround from above?
I'm very new with Cake so is VERY POSSIBLE I'm doing something veeeery
wrong.
Someone can help me with this issue? Thanks for your attention.
PS: here is the code of my files:
User Model:
-------------------------------------------------------------------------------------
<?php
class User extends AppModel{
var $name = 'User';
var $belongsTo = array( 'Company', 'Group' );
var $validate = array(
'email' => array(
'required' => array(
'rule' => 'email',
'required' => true,
'message' => 'Ingrese una dirección de e-mail
válida'
)
),
'password' => array(
'required' => array(
'rule' => VALID_NOT_EMPTY,
'message' => 'Debe indicar su contraseña'
)
),
'company_id' => array(
'required' => array(
'rule' => VALID_NOT_EMPTY,
'message' => 'Seleccione una empresa'
)
),
'group_id' => array(
'required' => array(
'rule' => VALID_NOT_EMPTY,
'message' => 'Seleccione el grupo al que
pertenece el usuario'
)
),
// campo adicional para confirmar la contraseña, no existe en la
BBDD
'password_conf' => array(
'required' => array(
'rule' => VALID_NOT_EMPTY,
'message' => 'Debe confirmar su contraseña'
)
)
);
}
?>
Users controller
-------------------------------------------------------------------------------------
<?php
class UsersController extends AppController{
var $name = 'Users';
var $paginate = array( 'limit' => 15, 'page' => 1 );
function control_view() {
..........
}
function control_edit( $id = null ) {
// protección por tipo de admin
// inicialización
$this->layout = 'default';
// datos necesarios
$companies = $this->User->Company->find( 'list' );
$this->set('companies', $companies );
$groups = $this->User->Group->find( 'list' );
$this->set('groups', $groups );
// si no se ha enviado el formulario carga el ID del registro a
editar
if ( empty( $this->data ) ) :
$this->User->id = $id;
$this->data = $this->User->read();
else :
// si se ha enviado y valida guarda los datos
if ( $this->User->create($this->data) && $this->User-
>validates() ) :
// encripta la contraseña si se está insertando
if ( empty( $id ) ) :
$this->data['User']['password'] = md5(
$this->data['User']
['password'] );
endif;
if( $this->User->save( $this->data['User'] ) ) :
$this->redirect('view');
endif;
endif;
endif;
}
function control_delete( $id = null ) {
......................
}
function control_changepassword( $id = null ) {
// protección por tipo de admin
if ( $this->Session->read('Administrator.superadmin') != 1 ) :
$this-
>redirect('/control/'); endif;
// inicialización
$this->layout = 'default';
// inicialización variables
$this->set('passwords_match_error', 0 );
// si no se ha enviado el formulario carga el ID del registro a
editar
if ( empty( $this->data ) ) :
$this->User->id = $id;
$this->data = $this->User->read();
// dejo en blanco la contraseña para permitir cambiarla
$this->data['User']['password'] = '';
else :
// no se porqué, pero debo cargarle aunque sea un valor
del
registro al array data para que funcione la validación
$pp = $this->User->read( null, $id );
$this->data['User']['email'] = $pp['User']['email'];
// si se ha enviado y valida guarda los datos
if ( $this->User->create( $this->data['User'] ) &&
$this->User-
>validates() ) :
// si estoy editando
if ( !empty( $id ) ) :
// si los campos coinciden guardo la
nueva contraseña y
redirecciono
if ( $this->data['User']['password'] ==
$this->data['User']
['password_conf'] ) :
// encripta la contrasña
$this->data['User']['password']
= md5( $this->data['User']
['password'] );
// guarda la contraseña
if( $this->User->save(
$this->data['User'] ) ) :
$this->redirect('view');
endif;
else:
// guarda el error
$this->set('passwords_match_error', 1);
$this->set('passwords_match_error_msg', __('Las contraseñas
introducidas no coinciden. Por favor inténtelo nuevamente.', true) );
endif;
else:
$this->redirect('view');
endif;
endif;
endif;
}
}
?>
Users changepassword view
-------------------------------------------------------------------------------------
<div id="sidebar" class="floatLeft">
<h3><?php echo __('Ayuda'); ?></h3>
<p><?php echo __('En esta sección podrá crear y administrar los
usuarios de la aplicación.'); ?></p>
<p> </p>
<h3><?php echo __('Asistencia técnica'); ?></h3>
<p><?php echo __('Si encuentra algún problema con la aplicación,
comuníquese con'); ?> <a href="mailto:<?php echo
__('#webmaster_mail'); ?>"><?php echo __('#webmaster_mail'); ?></a></
p>
</div>
<div id="mainContent">
<h2><?php echo __('Usuarios'); ?></h2>
<div class="list">
<div class="listHeader clearfix">
<h3><?php echo __('Cambiar contraseña'); ?> : <span
class="recordName"><?php echo $this->data['User']['email']; ?></span></
h3>
</div>
<?php echo $form->create('User', array('action' =>
'changepassword')) ?>
<div class="editForm">
<?php if ( $passwords_match_error == 1 ) : ?>
<div class="editFormErrorMsg">
<?php echo $passwords_match_error_msg;
?>
</div>
<?php endif; ?>
<div class="formRow clearfix">
<?php echo
$form->label('User.password', __('Contraseña nueva',
true) . ' <em>*</em>', array('class' => 'floatLeft w200px alignRight
marginRight20')); ?>
<div class="floatLeft">
<?php echo
$form->input('User.password', array('type' =>
'password', 'div' => false, 'label' => false, 'class' => 'textField
w150px')); ?>
</div>
</div>
<div class="formRow clearfix">
<?php echo
$form->label('User.password_conf', __('Confirmar
contraseña nueva', true) . ' <em>*</em>', array('class' => 'floatLeft
w200px alignRight marginRight20')); ?>
<div class="floatLeft">
<?php echo
$form->input('User.password_conf', array('type' =>
'password', 'div' => false, 'label' => false, 'class' => 'textField
w150px')); ?>
</div>
</div>
</div>
<div class="editFormButtons">
<p><?php echo $form->submit(__('Guardar',
true), array('div' =>
false, 'class' => 'button') ); ?> <?php echo __('o'); ?> <?
php echo $html->link(__("Cancelar",true), 'view' ); ?></p>
</div>
<?php echo $form->input('User.id', array('type' =>
'hidden' ) ); ?>
<?php echo $form->end(); ?>
</div>
</div>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---