Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable validation in update form - CakePHP

I have a form that i use to register and update data. It works fine, but when i update the data, the model validation returns false, because the value inserted already exists.

I have the following code:

/* Validation Rule*/
public $validate = array(
    'unique' => array(
        'rule' => array('unique', 'field in the table'),
        'message' => 'The field cannot be empty.'
    )
    ...
)
/* Function that check if the value already exists */
public function unique($value, $field) {
    $check = $this->find('count', array(
        'recursive' => -1,
        'conditions' => array(
            $this->Alias.$field => $value
        )
    ));

    return $check == 0;
}

So, how can i disable the unique rule, but keeping the other rules?

like image 686
cbaracat Avatar asked Mar 20 '26 09:03

cbaracat


1 Answers

You can add on in your validation rules and accept this validation only on create:

public $validate = array(
    'unique' => array(
        'rule' => array('unique', 'field in the table'),
        'message' => 'The field cannot be empty.',
        'on' => 'create'
    );
);

Reference:

  • Data Validation
like image 133
Paulo Rodrigues Avatar answered Mar 23 '26 14:03

Paulo Rodrigues