Hi there,
Im new to cakePHP and only been playing around with it for a few days.
I have made 2 simple tables just for me to have a feel of using
cakePHP.
table 1: (stores car makes)
makes (id, make, created, modified)
table 2: (stores car models, make_id foreign key)
models (id, make_id, model, created, modified)
The makes/add() action works fine, but im having troubles with the
models/add() action.
When I submit an empty form to add a new model, it just spits out
errors in MySQL and it bypasses the validation code. Notice the
dropdown of the car makes in models/add.
Here's the code:
[Models]:
<?php
class Make extends AppModel {
var $name = 'Make';
var $hasMany = array('Model' =>
array('className' =>
'Model',
'conditions'
=> '',
'order' =>
'Model.model ASC',
'limit' => '',
'foreignKey'
=> 'make_id',
'dependent'
=> 'true',
'exclusive'
=> 'false',
'finderQuery'
=> '',
'fields' =>
'',
'offset' =>
'',
'counterQuery' => ''
)
);
var $validate = array('make' => '/[a-zA-Z]+/i');
} // end class Make
?>
<?php
class Model extends AppModel {
var $name = 'Model';
var $belongsTo = array('Make' =>
array('className' =>
'Make',
'conditions'
=> '',
'order' => '',
'foreignKey'
=> 'make_id'
)
);
var $validate = array('make_id' => VALID_NOT_NULL,
'model' =>
'/[a-zA-Z0-9\s\-]+/i'
);
} // end class Model
?>
[Controllers]:
<?php
class MakesController extends AppController {
var $name = 'Makes';
function index() {
$this->pageTitle = 'Car Makes';
$this->layout = 'temp';
$this->set('makes', $this->Make->findAll());
} // end index()
function add() {
$this->pageTitle = 'Add Car Makes';
$this->layout = 'temp';
if (!empty($this->data)) {
if ($this->Make->save($this->data)) {
$this->flash('Car make ' .
$this->data['Make']['make'] . ' has
been saved.', '/makes/');
} else {
$this->set('errorMsg', 'Please correct the
errors below:');
$this->render();
}
}
} // end add()
function getMakeList() {
return $this->Make->generateList(null, 'make ASC', null,
'{n}.Make.id', '{n}.Make.make');
} // end getMakeList()
} // end class MakesController
?>
<?php
class ModelsController extends AppController {
var $name = 'Models';
function index() {
$this->pageTitle = 'Car Models';
$this->layout = 'temp';
$this->set('models', $this->Model->findAll());
} // end index()
function add() {
$this->pageTitle = 'Add Car Models';
$this->layout = 'temp';
// Load car makes dropdown
$this->set('makes',
$this->requestAction('/makes/getMakeList/'));
if (!empty($this->data)) {
if ($this->Model->save($this->data)) {
$this->flash('Car model ' .
$this->data['Model']['model'] . ' has
been added', '/models/');
} else {
$this->set('errorMsg', 'Please correct the
errors below:');
$this->render();
}
}
} // end add()
} // end class ModelsController
?>
[Views]:
<?php if (!empty($errorMsg)) { echo $errorMsg; } ?>
<h1>Add Car Models</h1>
<form method="post" action="<?php echo $html->url('/models/add/'); ?
>">
<p>
Add Model:
<?php echo $html->input('Model/model'); ?>
<?php echo $html->tagErrorMsg('Model/model', 'Model is
required.'); ?
>
</p>
<p>
To this Make:
<?php echo $html->selectTag('Model/make_id', $makes, null,
array(),
null, true, false); ?>
<?php echo $html->tagErrorMsg('Model/make_id', 'Make is
required.'); ?>
</p>
<?php echo $html->submit('Save'); ?>
</form>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---