My form is not validating on submission. I'm a newbie, so please be
gentle. Here's my model:
<?php
class User extends AppModel {
var $name = 'User';
var $belongsTo = array('School' =>
array('className' => 'School',
'conditions' => '',
'order' => '',
'foreignKey' => ''
),'Country' =>
array('className' => 'Country',
'conditions' => '',
'order' => '',
'foreignKey' => ''
),'Timezone' =>
array('className' => 'Timezone',
'conditions' => '',
'order' => '',
'foreignKey' => ''
),'Chattype' =>
array('className' => 'Chattype',
'conditions' => '',
'order' => '',
'foreignKey' => ''
),'Chatimage' =>
array('className' => 'Chatimage',
'conditions' => '',
'order' => '',
'foreignKey' => ''
),'Usertype' =>
array('className' => 'Usertype',
'conditions' => '',
'order' => '',
'foreignKey' => ''
),'Status' =>
array('className' => 'Status',
'conditions' => '',
'order' => '',
'foreignKey' => ''
)
);
var $validate = array(
'firstname' => VALID_NOT_EMPTY,
'lastname' => VALID_NOT_EMPTY,
'email' => VALID_EMAIL,
'password' => VALID_NOT_EMPTY,
'confirm' => VALID_NOT_EMPTY
);
function beforeValidate()
{
if( isset($this->data[$this->name]['password']) &&
($this->data[$this->name]['password'] != $this->data[$this-
>name]['confirm']))
{
$this->data[name]['password'] = '';
$this->data[name]['confirm'] = '';
$this->invalidate('confirm');
}
if ( isset($this->data[$this->name]['school_id']) && $this-
>data[$this->name]['school_id'] == '0') {
$this->invalidate('school_id');
}
if ( isset($this->data[$this->name]['country_id']) && $this-
>data[$this->name]['country_id'] == '0') {
$this->invalidate('country_id');
}
if ( isset($this->data[$this->name]['timezone_id']) && $this-
>data[$this->name]['timezone_id'] == '0') {
$this->invalidate('timezone_id');
}
return true;
}
}
?>
And here's my controller:
<?php
class UsersController extends AppController {
var $layout = 'form';
var $helpers = array('Html', 'Form', 'Javascript', 'Session');
var $uses =
array('User','Usertitle','School','Country','Timezone','Chattype','Chatimage','Usertype','Status');
function newTeacher() {
if (empty($this->data)) {
//
// Grab view options arrays from db
//
$options = array();
$options['usertitle'] = $this->Usertitle-
>generateList(null, 'Usertitle.name ASC', null,
'{n}.Usertitle.id', '{n}.Usertitle.name');
$options['school'] = $this->School->generateList(null,
'School.name ASC', null,
'{n}.School.id', '{n}.School.name');
$options['country'] = $this->Country-
>generateList(null, 'Country.name ASC', null,
'{n}.Country.id', '{n}.Country.name');
$options['timezone'] = $this->Timezone-
>generateList(null, 'Timezone.name ASC', null,
'{n}.Timezone.id', '{n}.Timezone.name');
$options['chattype'] = $this->Chattype-
>generateList(null, 'Chattype.name ASC', null,
'{n}.Chattype.id', '{n}.Chattype.name');
$chatimage = $this->Chatimage->findAll();
// Set select option defaults
array_unshift($options['usertitle'], '---');
array_unshift($options['school'], '-- Please Select --');
array_push($options['school'], '-- New --');
array_unshift($options['country'], '-- Please Select --');
array_unshift($options['timezone'], '-- Please Select
--');
array_unshift($options['chattype'], 'Type?');
// Build chat image arrays
$count = count($chatimage);
for ($ii = 0; $ii < $count; $ii++) {
$id = $chatimage[$ii]['Chatimage']['image'];
$title = $chatimage[$ii]['Chatimage']['name'];
$options['chatimage'][$ii] = array('id' => $id,
'title' => $title);
}
// Set initial user defaults
$user = array('User');
$user['User'] = $this->User->getColumnTypes();
foreach ($user['User'] as $field => $type) {
switch ($type) {
case 'integer': $type = 0; break;
case 'string':
case 'text': $type = ''; break;
}
$user['User'][$field] = $type;
}
$user['User']['hours'] = '';
unset($user['User']['id']);
unset($user['User']['created']);
unset($user['User']['modified']);
unset($user['User']['verified']);
// Save to session
$this->Session->write('options', $options);
$this->Session->write('user', $user);
// Send info to view
$this->set('user', $user);
$this->set('options', $options);
$this->render();
exit();
}
if ( !empty($this->data['User']) ) {
if ( $this->User->create($this->data) && $this->User-
>validates() ) {
$this->flash("Your information has been validated!",
'/');
$options = $this->Session->delete('options');
$options = $this->Session->delete('user');
exit();
}
else {
$this->validateErrors($this->User);
$options = $this->Session->read('options');
$user = $this->Session->read('user');
$user = array_merge($user, $this->data);
$this->set('user', $user);
$this->set('options', $options);
$this->render();
exit();
}
}
}
}
?>
If I submit my form without filling anything out, I get the flash
message.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---