If you use ModelAdmin for your DataObjects, this might be a solution for you:
class MyDO extends DataObject{
:
:
/**
* Validate fields
* @return RequiredFields
*/
public function getCMSValidator(){
$controller = ExtededModelAdmin::curr();
$request = $controller->getRequest();
/* Skip validation when user creates new Translation */
if($request->getVar('action_createtranslation') == 1)
return new RequiredFields();
return new My_Validator();
}
:
:
}
class My_Validator extends RequiredFields{
protected $customRequired = array(your db-array fields to check);
public function __construct() {
$required = func_get_args();
if(isset($required[0]) && is_array($required[0])) {
$required = $required[0];
}
$required = array_merge($required, $this->customRequired);
parent::__construct($required);
}
public function php($data){
/* Check required Fields */
parent::php($data);
/* Special checks */
//Unique & Correct Code
if(!isset($data['Locale'])) $data['Locale'] = Translatable::get_current_locale();
$DOSet = DataObject::get('My_DO', "DO_attr = '$data[DO_attr]' AND Locale = '$data[Locale]'");
if($DOSet instanceof DataObjectSet){
if(($DOSet->Count() > 1) || ($DOSet->First()->ID != $data['ID'])){
$this->validationError('My_DO', 'DO_attr already in use, choose another one.');
}
}
return empty($this->errors);
}
}