2015-08-12 07:18:43 +02:00
|
|
|
<?php
|
|
|
|
|
2017-08-09 01:55:09 +02:00
|
|
|
namespace SilverStripe\UserForms\Extension;
|
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\Forms\RequiredFields;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroup;
|
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroupEnd;
|
2017-08-11 02:36:28 +02:00
|
|
|
use SilverStripe\UserForms\Model\EditableFormField;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableFormStep;
|
2015-08-12 07:18:43 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
class UserFormValidator extends RequiredFields
|
|
|
|
{
|
|
|
|
public function php($data)
|
|
|
|
{
|
|
|
|
if (!parent::php($data)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-12 07:18:43 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// Skip unsaved records
|
|
|
|
if (empty($data['ID']) || !is_numeric($data['ID'])) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-08-12 07:18:43 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$fields = EditableFormField::get()->filter('ParentID', $data['ID'])->sort('"Sort" ASC');
|
2015-08-12 07:18:43 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// Current nesting
|
|
|
|
$stack = array();
|
|
|
|
$conditionalStep = false; // Is the current step conditional?
|
|
|
|
foreach ($fields as $field) {
|
|
|
|
if ($field instanceof EditableFormStep) {
|
|
|
|
// Page at top level, or after another page is ok
|
|
|
|
if (empty($stack) || (count($stack) === 1 && $stack[0] instanceof EditableFormStep)) {
|
|
|
|
$stack = array($field);
|
|
|
|
$conditionalStep = $field->EffectiveDisplayRules()->count() > 0;
|
|
|
|
continue;
|
|
|
|
}
|
2015-08-12 07:18:43 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->validationError(
|
|
|
|
'FormFields',
|
|
|
|
_t(
|
2017-08-11 01:33:06 +02:00
|
|
|
__CLASS__.".UNEXPECTED_BREAK",
|
2016-07-21 07:53:59 +02:00
|
|
|
"Unexpected page break '{name}' inside nested field '{group}'",
|
|
|
|
array(
|
|
|
|
'name' => $field->CMSTitle,
|
|
|
|
'group' => end($stack)->CMSTitle
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'error'
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-12 07:18:43 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// Validate no pages
|
|
|
|
if (empty($stack)) {
|
|
|
|
$this->validationError(
|
|
|
|
'FormFields',
|
|
|
|
_t(
|
2017-08-11 01:33:06 +02:00
|
|
|
__CLASS__.".NO_PAGE",
|
2016-07-21 07:53:59 +02:00
|
|
|
"Field '{name}' found before any pages",
|
|
|
|
array(
|
|
|
|
'name' => $field->CMSTitle
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'error'
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-12 07:18:43 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// Nest field group
|
|
|
|
if ($field instanceof EditableFieldGroup) {
|
|
|
|
$stack[] = $field;
|
|
|
|
continue;
|
|
|
|
}
|
2015-08-12 07:18:43 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// Unnest field group
|
|
|
|
if ($field instanceof EditableFieldGroupEnd) {
|
|
|
|
$top = end($stack);
|
2015-08-12 07:18:43 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// Check that the top is a group at all
|
|
|
|
if (!$top instanceof EditableFieldGroup) {
|
|
|
|
$this->validationError(
|
|
|
|
'FormFields',
|
|
|
|
_t(
|
2017-08-11 01:33:06 +02:00
|
|
|
__CLASS__.".UNEXPECTED_GROUP_END",
|
2016-07-21 07:53:59 +02:00
|
|
|
"'{name}' found without a matching group",
|
|
|
|
array(
|
|
|
|
'name' => $field->CMSTitle
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'error'
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-12 07:18:43 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// Check that the top is the right group
|
|
|
|
if ($top->EndID != $field->ID) {
|
|
|
|
$this->validationError(
|
|
|
|
'FormFields',
|
|
|
|
_t(
|
2017-08-11 01:33:06 +02:00
|
|
|
__CLASS__.".WRONG_GROUP_END",
|
2016-07-21 07:53:59 +02:00
|
|
|
"'{name}' found closes the wrong group '{group}'",
|
|
|
|
array(
|
|
|
|
'name' => $field->CMSTitle,
|
|
|
|
'group' => $top->CMSTitle
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'error'
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-12 07:18:43 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// Unnest group
|
|
|
|
array_pop($stack);
|
|
|
|
}
|
2015-08-17 00:43:51 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// Normal field type
|
|
|
|
if ($conditionalStep && $field->Required) {
|
|
|
|
$this->validationError(
|
|
|
|
'FormFields',
|
|
|
|
_t(
|
2017-08-11 01:33:06 +02:00
|
|
|
__CLASS__.".CONDITIONAL_REQUIRED",
|
2016-07-21 07:53:59 +02:00
|
|
|
"Required field '{name}' cannot be placed within a conditional page",
|
|
|
|
array(
|
|
|
|
'name' => $field->CMSTitle
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'error'
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-08-12 07:18:43 +02:00
|
|
|
}
|