CakePHP 2.5验证是独特的

时间:2014-12-18 14:50:25

标签: cakephp

我一直绞尽脑汁想要解决这个问题。基本上我有一个叫做客户的桌子。我可以创建没有问题的新客户,但是当涉及到编辑它们时,独特总是说它们已经存在,显然我知道但我希望它与所有其他记录相比是独一无二的。

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);
    }
}

查看: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>

我在其他帖子中找到的解决方案似乎永远不会令人沮丧。

感谢您的帮助 史蒂夫

1 个答案:

答案 0 :(得分:0)

好吧,我有点白痴!

我的数据库没有对ID进行自动增量,因此问题就是现在一切正常。