@MrTufty:
The code I posted above should work with both operations (updating and
inserting). That's why the code checks if the id is set in the model
(hence it's working with an existing row then, and checks all other
rows for duplicates of the to-update value).
On Nov 30, 2:49 pm, MrTufty <[EMA
Good solutions Richard, nice one.
You may want to look into specifying the 'on' part of the validation
rule, which allows you to set whether the validation is done on
creating a new record, or updating an existing one. I'm not sure how,
if at all, that would tie into the code you've provided here
If anyone else is looking for some drop in code to handle unique
validation, here's what I came up with.
This is a generic function you can drop into your app_model:
/**
* checks is the field value is unqiue in the table
* note: we are overriding the default cakephp isU
Thanks for the additional feedback.
I'd already discovered the 'edit' gotcha and created a workaround
similar to Involution's.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group,
Alternatively (and better because reusable in other Models), you could
insert the function _isUnique (documented below) in your own
app_model.php and use the following code in your Model.
Second argument in the 'rule' array is the field that has to be
checked for uniqueness..
var $validate = arr
Take care when using with edit. Indeed you can edit your data, not
modifying the field but the field will exist in the DB (the current
record)...
On Nov 29, 7:54 pm, José Pablo Orozco Marín <[EMAIL PROTECTED]>
wrote:
> Maybe this help:http://tempdocs.cakephp.org/#TOC121845
>
> class User extends
Maybe this help:
http://tempdocs.cakephp.org/#TOC121845
array(
'rule' => array('checkUnique'),
'message' => 'Login name already taken.'
)
);
function checkUnique($data)
{
$valid = false;
if(isset($fieldName) && $this->hasField($fieldN
Ahh, thank you very much Mr Tufty. Another piece of the Cake puzzle
slides into place :-)
On Nov 29, 4:28 pm, MrTufty <[EMAIL PROTECTED]> wrote:
> And is entirely wrong... well, no, it'll work, but it doesn't take
> advantage of CakePHP 1.2 validation. The IBM tutorials are good, but
> outdated n
And is entirely wrong... well, no, it'll work, but it doesn't take
advantage of CakePHP 1.2 validation. The IBM tutorials are good, but
outdated now.
Richard, as you suspect, you're not supposed to be referencing
isUnique in a rule. But, you don't need to use it in the controller.
Here's how you'
On 29 Nov., 13:40, RichardAtHome <[EMAIL PROTECTED]> wrote:
> As a side note, I suspect isUnique might not meant to be referenced in
> a rule and is meant to be referenced in the controller. If this is
> true can someone point me towards some examples of how / where it
> should be used?
I'm quit
10 matches
Mail list logo