2015-08-17 00:43:51 +02:00
|
|
|
<?php
|
|
|
|
|
2017-08-09 01:55:09 +02:00
|
|
|
namespace SilverStripe\UserForms\Model\EditableFormField;
|
|
|
|
|
|
|
|
use SilverStripe\Forms\RequiredFields;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\UserForms\Model\EditableCustomRule;
|
2017-08-09 01:55:09 +02:00
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
class Validator extends RequiredFields
|
2016-07-21 07:53:59 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var EditableFormField
|
|
|
|
*/
|
|
|
|
protected $record = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param EditableFormField $record
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setRecord($record)
|
|
|
|
{
|
|
|
|
$this->record = $record;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @return EditableFormField
|
|
|
|
*/
|
|
|
|
public function getRecord()
|
|
|
|
{
|
|
|
|
return $this->record;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function php($data)
|
|
|
|
{
|
|
|
|
if (!parent::php($data)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-15 11:15:30 +02:00
|
|
|
// When the record is unsaved and the classname is not set throw an error
|
|
|
|
if ((!$this->record || !$this->record->exists()) && (!isset($data['ClassName']) || empty($data['ClassName']))) {
|
|
|
|
$this->validationError(
|
|
|
|
'ClassName',
|
|
|
|
_t(
|
|
|
|
__CLASS__ . 'CLASSNAME_ERROR',
|
|
|
|
'You need to select a field type before you can create the field'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// Skip unsaved records
|
|
|
|
if (!$this->record || !$this->record->exists()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip validation if not required
|
|
|
|
if (empty($data['Required'])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip validation if no rules
|
|
|
|
$count = EditableCustomRule::get()->filter('ParentID', $this->record->ID)->count();
|
|
|
|
if ($count == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Both required = true and rules > 0 should error
|
|
|
|
$this->validationError(
|
|
|
|
'Required_Error',
|
|
|
|
_t(
|
2017-08-11 01:33:06 +02:00
|
|
|
__CLASS__.'.REQUIRED_ERROR',
|
|
|
|
'Form fields cannot be required and have conditional display rules.'
|
2016-07-21 07:53:59 +02:00
|
|
|
),
|
|
|
|
'error'
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-17 00:43:51 +02:00
|
|
|
}
|