1

I have a model that consists of 3 fields, ID, Language, Filename. How can i prevent to save a new record if there is an existing record with same Filename and Language. For example

1. ID: 0001    Language: EN     Filename:Test.pdf

If I try to add another record with Language EN and Filename Test.pdf to show an error. I want to do it in the model. Bear in mind that i am new to CakePHP.

Inigo Flores
  • 4,461
  • 1
  • 15
  • 36
user1292656
  • 2,502
  • 20
  • 48
  • 68

1 Answers1

1

Add a rule to the $validate array in your model:

public $validate = array(
   'Filename' => array(
        'rule' => array('isUnique', array('Language', 'Filename'), false),
        'message' => 'The File already exists in the specified Language ',
        'required' => 'create'
    )
);

From the CakePHP 2.x documentation.

Inigo Flores
  • 4,461
  • 1
  • 15
  • 36
  • This rule works correctly on adding but for some reason every time i am trying to edit an existing record it always fails on saving. If i remove the above rule then everything is fine. – user1292656 Nov 18 '15 at 15:55
  • I've updated the answer so that the rule is only enforced when adding new entries. – Inigo Flores Nov 18 '15 at 18:53