Reputation: 99
I'm setting some model properties before validating and I have this block of code
public function beforeValidate() {
$this->inventory->read(null, 1);
$this->inventory->set('item_amount', '0.00');
if ($this->inventory.'item_type' == 'powder'){
$this->inventory->set('unit_measurement','g')
}
else if ($this->inventory.'item_type' == 'liquid'){
$this->inventory->set('unit_measurement','ml')
}
}
basically I'm setting the property of an inventory model based on a a prameter passed from the form *while the item item type parameter is in the form..... the unit measurement isn't.... but it's still part of my model and its according database table... any bewtter way to go about this?
here's the view if you guys need it
<div class="inventories form">
<?php echo $this->Form->create('Inventory'); ?>
<fieldset>
<legend><?php echo __('Admin Add Inventory item'); ?></legend>
<?php
echo $this->Form->input('Item_Name');
echo $this->Form->input('Item_type', array(
'options' => array('powder', 'liquid'),
'empty' => '(choose one)'
));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('List Inventories'), array('action' => 'index')); ?></li>
</ul>
</div>
Upvotes: 0
Views: 74
Reputation: 481
You need to change your before validate function like as
public function beforeValidate() {
if ($this->data['Inventory']['Item_type'] == 'powder'){
$this->data['Inventory']['unit_measurement'] = 'g';
}
else {
$this->data['Inventory']['unit_measurement'] = 'ml';
}
}
Upvotes: 3