Okay, looks like userDefined isn't implemented yet.
So I tried my luck with beforeValidate() callback, and got it to work
- sort of.
function beforeValidate()
{
$data = $this->data;
if(!empty($this->id))
{
$assetType = ife($this->isFolder($this->id), 0, '> 0');
$result = $this->hasAny(
array(
"{$this->name}.name" =>
$data[$this->name]['name'],
"{$this->name}.{$this->primaryKey}" =>
"!= {$data[$this->name]
[$this->primaryKey]}",
"{$this->name}.parent_id" =>
$data[$this->name]['parent_id'],
"{$this->name}.asset_type_id" =>
$assetType
)
);
}
else
{
$assetType = ife($this->isFolder($this->id), 0, '> 0');
$result = $this->hasAny(
array(
"{$this->name}.name" =>
$data[$this->name]['name'],
"{$this->name}.parent_id" =>
$data[$this->name]['parent_id'],
"{$this->name}.asset_type_id" =>
$assetType
)
);
}
if($result)
{
$this->invalidate('name');
return false;
}
return true;
}
This code prevents Model::save() when appropiate, but I can't get it
to show an error message in the view?
Have tried : <?php e($form->input('name', array('error' =>
'Invalidated')));?>
No luck so far.
Thoughts and comments are very welcome!!
On Mar 20, 8:42 am, "biesbjerg" <[EMAIL PROTECTED]> wrote:
> Hi guys,
>
> I'm trying to check if a record already exists (based on a filename),
> if it does it should fail validation.
>
> I'm using CakePHP 1.2 and had a look at 'userDefined' which looks like
> it should call a custom function.
> This function I'm talking
> about:http://api.cakephp.org/1.2/validation_8php-source.html#l00633
>
> Here's what I've tried:
>
> function uniqueFilename()
> {
> return false;
>
> }
>
> var $validate = array(
> 'name' => array(
> 'rule' => array(
> 'userDefined' => array('Asset',
> 'uniqueFilename', array('param1',
> 'param2'))
> ),
> 'message' => 'Filename already exists!'
> )
> );
>
> Above code gives me a couple of errors:
>
> Notice (8): Undefined offset: 0 [CORE/cake/libs/model/model.php, line
> 1683]
>
> Context | Code
>
> $this = Asset object
> $data = array("parent_id" => "574", "name" => "Lloyd Banks ft. 50 Cent
> - Hands Up (Dirty).mp3", "id" => "576")
> $Validation = Validation object
> $validator = array("allowEmpty" => true, "message" => "Angiv
> venligst
> en titel for siden", "rule" => array, "on" => null)
> $fieldName = "name"
>
> $this->invalidate($fieldName,
> $validator['message']);
>
> } elseif (isset($data[$fieldName])) {
>
> if (is_array($validator['rule'])) {
>
> $rule = $validator['rule'][0];
>
> unset($validator['rule'][0]);
>
> Model::invalidFields() - CORE/cake/libs/model/model.php, line 1683
> Model::validates() - CORE/cake/libs/model/model.php, line 1633
> Model::save() - CORE/cake/libs/model/model.php, line 993
> AssetsController::admin_edit() - APP/controllers/
> assets_controller.php, line 123
> Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 348
> Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 330
> [main] - ROOT//www/index.php, line 84
>
> Notice (8): Undefined property: SessionComponent::$themeWeb [CORE/
> cake/libs/controller/components/session.php, line 195]
>
> Context | Code
>
> $flashMessage = "Asset updated"
> $layout = "flash_success"
> $params = array()
> $key = "flash"
> $ctrl = null
> $view = View object
>
> $view->params = $this->params;
>
> $view->action = $this->action;
>
> $view->data = $this->data;
>
> $view->themeWeb = $this->themeWeb;
>
> $view->plugin = $this->plugin;
>
> SessionComponent::setFlash() - CORE/cake/libs/controller/components/
> session.php, line 195
> AssetsController::admin_edit() - APP/controllers/
> assets_controller.php, line 125
> Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 348
> Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 330
> [main] - ROOT//www/index.php, line 84
>
> Warning (2): Cannot modify header information - headers already sent
> by (output started at /web/lib/1.2.x.x_04.03.2007/cake/basics.php:869)
> [CORE/cake/libs/controller/controller.php, line 498]
>
> Any idea?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---