I have been racking my brain for hours trying to figure this out. Basically i have a table called customers. I can create new customers with no issues but when it comes to editing them isUnique always says they already exist, obviously i know that but i want it to be unique compared to all other records.
model: Customer.php
public $validate = array(
'name' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
'required' => true,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
'uniqueName' => array(
'rule' => 'isUnique',
'message' => 'Customer already exists',
'on' => 'create'
)
),
'reference' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
'required' => true,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
'uniqueReference' => array(
'rule' => 'isUnique',
'message' => 'Customer reference already exists',
'on' => 'create'
)
),
);
Controller: CustomerController.php
public function edit($id = null) {
if (!$this->Customer->exists($id)) {
throw new NotFoundException(__('Invalid customer'));
}
if ($this->request->is(array('post', 'put'))) {
$this->Customer->id = $id;
if ($this->Customer->save($this->request->data)) {
$this->Session->setFlash(__('The customer has been updated.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The customer could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('Customer.' . $this->Customer->primaryKey => $id));
$this->request->data = $this->Customer->find('first', $options);
}
}
View: edit.ctp
<div class="users form index col-md-9">
<?php echo $this->Form->create('Customer',array('class'=>'form-horizontal','role' => 'form')); ?>
<fieldset>
<legend><?php echo __('Edit Customer'); ?></legend>
<?php
echo $this->Form->input('id', array('type' => 'hidden'));
echo $this->Form->input('reference');
echo $this->Form->input('name');
echo $this->Form->input('description');
?>
</fieldset>
<?php echo $this->Form->submit(__('Update'),array('class'=>'btn btn-medium btn-success')); ?>
<?php echo $this->Form->end(); ?>
</div>
<div class="actions index col-md-3">
<h3><?php echo __('Actions'); ?></h3>
<ul class="nav nav-pills nav-stacked">
<li><?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $this->Form->value('Customer.id')), null, __('Are you sure you want to delete # %s?', $this->Form->value('Customer.id'))); ?></li>
<li><?php echo $this->Html->link(__('List Customers'), array('action' => 'index')); ?></li>
<li><?php echo $this->Html->link(__('New Customer'), array('controller' => 'customers', 'action' => 'add')); ?> </li>
</ul>
</div>
the solutions i have found in other posts never seem to work which is so frustrating.
Thanks for any help Steve