Wednesday, December 28, 2011

Form validation error doesn't show! CPHP 1.3

Form validation error doesn't show! CPHP 1.3

I have a user model and in my user model i have

User Model
--------------------------------
<?php
class User extends AppModel {
var $name = 'User';
var $displayField = 'username';


var $validate = array(
'username' => array(
'usernameRule' => array(
'rule' => 'alphaNumeric',
'message' => 'Alphabetic and Number characters only'

),
'usernameRule2' => array(
'rule' => array('minLength', 6),
'message' => 'Minimum length of 6 characters'
),
),
'password' => array(
'passwordRule' => array(
'rule' => 'alphaNumeric',
'message' => 'Alphabetic and Number characters only'

),
'passwordRule2' => array(
'rule' => array('minLength', 6),
'message' => 'Minimum length of 6 characters'
),
)
);
}
?>
in my user controller i have included from helper but doesn't show
error for username field or password field when I enter less then 6
chars..

thanks. by the way I am not getting error when I am updating a user
data but when i user user add form it works.

User Controller
---------------------------

<?php
class UsersController extends AppController {

var $name = 'Users';
var $components = array('Email');
var $helpers = array('Form');

function login() {
}

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

function _refreshAuth($field = '', $value = '') {
if (!empty($field) && !empty($value)) {
$this->Session->write($this->Auth->sessionKey .'.'. $field,
$value);
} else {
if (isset($this->User)) {
$this->Auth->login($this->User->read(false, $this->Auth-
>user('id')));
} else {
$this->Auth->login(ClassRegistry::init('User')->findById($this-
>Auth->user('id')));
}
}
}

function forgot(){
if (!empty($this->data)){
$user = $this->User->find('first',array('conditions' =>
array('email' =>$this->data['User']['email'])));
if(!empty($user)){
$newPassword = $this->_createRandomPassword(8);
$this->Email->from = 'Example <support@example.com>';
$this->Email->to = $this->data['User']['email'];
$this->Email->subject = 'Example Password!';
$this->Email->send('Dear '.$user['User']['username'].', your new
password is '.$newPassword.'. Please login and update your
password.');

$this->data['User']['password'] = $this->Auth-
>password($newPassword);
$this->User->id = $user['User']['id'];

$data = array(
'User' => array(
'id' => $user['User']['id'],
'password' => $this->Auth-
>password($newPassword)
)
);
$this->User->save( $data, true, array('password') );

$this->Session->setFlash('Thanks, your new password is been sent to
your email!');
} else {
$this->Session->setFlash("Sorry, this email doesn't exsits!");
}
}
}

function profile(){
$this->set('user',$this->Auth->user());

if (!empty($this->data)) {
$this->User->id = $this->Auth->user('id');

if(empty($this->data['User']['password'])){
$this->data['User']['password'] = $this->Auth-
>user('password');
}
$this->User->save($this->data, false,
array('username','email','password'));
$this->_refreshAuth();
}

$this->Auth->login($this->Auth->user());
Cache::clear();

}


function register() {
if($this->Auth->user()){
$this->cakeError('notAllowedWhenLoggedin');
}

if (!empty($this->data)) {
$this->User->create();
if ($this->User->save($this->data)) {
$this->Session->setFlash(__('The user has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please,
try again.', true));
}
}
}

public function beforeFilter() {
$this->Auth->allow('login','forgot','register');
parent::beforeFilter();
}

function index() {
$this->User->recursive = 0;
$this->set('users', $this->paginate());
}

function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid user', true));
$this->redirect(array('action' => 'index'));
}
$this->set('user', $this->User->read(null, $id));
}

function add() {
if (!empty($this->data)) {
$this->User->create();
if ($this->User->save($this->data)) {
$this->Session->setFlash(__('The user has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please,
try again.', true));
}
}
}

function edit($id = null) {
if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid user', true));
$this->redirect(array('action' => 'index'));
}
if (!empty($this->data)) {
if ($this->User->save($this->data)) {
$this->Session->setFlash(__('The user has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please,
try again.', true));
}
}
if (empty($this->data)) {
$this->data = $this->User->read(null, $id);
}
}

function delete($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid id for user', true));
$this->redirect(array('action'=>'index'));
}
if ($this->User->delete($id)) {
$this->Session->setFlash(__('User deleted', true));
$this->redirect(array('action'=>'index'));
}
$this->Session->setFlash(__('User was not deleted', true));
$this->redirect(array('action' => 'index'));
}

function admin_index() {
$this->User->recursive = 0;
$this->set('users', $this->paginate());
}

function admin_view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid user', true));
$this->redirect(array('action' => 'index'));
}
$this->set('user', $this->User->read(null, $id));
}

function admin_add() {
if (!empty($this->data)) {
$this->User->create();
if ($this->User->save($this->data)) {
$this->Session->setFlash(__('The user has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please,
try again.', true));
}
}
}

function admin_edit($id = null) {
if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid user', true));
$this->redirect(array('action' => 'index'));
}
if (!empty($this->data)) {
if ($this->User->save($this->data)) {
$this->Session->setFlash(__('The user has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please,
try again.', true));
}
}
if (empty($this->data)) {
$this->data = $this->User->read(null, $id);
}
}

function admin_delete($id = null) {
if (!$id){
$this->Session->setFlash(__('Invalid id for user', true));
$this->redirect(array('action'=>'index'));
}
if ($this->User->delete($id)) {
$this->Session->setFlash(__('User deleted', true));
$this->redirect(array('action'=>'index'));
}
$this->Session->setFlash(__('User was not deleted', true));
$this->redirect(array('action' => 'index'));
}

function _createRandomPassword($numberOfChars = 8) {

$chars = "abcdefghijkmnopqrstuvwxyz023456789";
srand((double)microtime()*1000000);
$i = 0;
$pass = '' ;

while ($i <= $numberOfChars) {
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}

return $pass;

}

}
?>


Add.ctp it shows in this one
------------------------------------------------------------------

<div class="users form">
<?php echo $this->Form->create('User');?>
<fieldset>
<legend><?php __('Add User'); ?></legend>
<?php
echo $this->Form->input('username');
echo $this->Form->input('email');
echo $this->Form->input('password');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
</div>
<div class="actions">
<h3><?php __('Actions'); ?></h3>
<ul>

<li><?php echo $this->Html->link(__('List Users', true),
array('action' => 'index'));?></li>
<li><?php echo $this->Html->link(__('List Events', true),
array('controller' => 'events', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New Event', true),
array('controller' => 'events', 'action' => 'add')); ?> </li>
</ul>
</div>

profile.ctp -it doesn't show the error in this one
---------------------------------------------------------------------------------------------------------


<div><h2>Profile</h2></div>
<?php echo $session->flash();
echo $this->Form->create('User');
echo $this->Form->input('username', array('label' =>
'Username','value'=>$user['username']));
echo $this->Form->input('email', array('value'=>$user['email']));
echo $this->Form->input('email_confirm', array('value'=>
$user['email']));
echo $this->Form->input('password', array('value'=>''));
echo $this->Form->input('password_confirm',
array('type'=>'password','value'=>''));
echo $this->Form->end('Update');
?>

--
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
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: