Skip to content

Commit

Permalink
MNT Add custom validator for test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabina Talipova committed Nov 9, 2023
1 parent a5aeaa3 commit 2483724
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
16 changes: 8 additions & 8 deletions code/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use SilverStripe\Versioned\Versioned;
use RelationFieldsTestPage;
use GridFieldTestPage;
use SilverStripe\Forms\CompositeValidator;
use SilverStripe\Forms\RequiredFields;

/**
*
Expand Down Expand Up @@ -100,15 +102,13 @@ public function getCMSFields()
return $fields;
}

public function validate()
public function getCMSCompositeValidator(): CompositeValidator
{
$result = parent::validate();

if (!$this->Name) {
$result->addFieldError('Name', 'Name is required');
}

return $result;
$validator = parent::getCMSCompositeValidator();
$validator->addValidator(RequiredFields::create([
'Name',
]));
return $validator;
}

public function DynamicProperty()
Expand Down
14 changes: 8 additions & 6 deletions code/Employee.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace SilverStripe\FrameworkTest\Model;

use SilverStripe\Assets\Image;
use SilverStripe\Forms\CompositeValidator;
use SilverStripe\Forms\EmailField;
use SilverStripe\Forms\NumericField;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\TextField;
use SilverStripe\ORM\Connect\MySQLSchemaManager;
use SilverStripe\ORM\DataObject;
Expand Down Expand Up @@ -141,13 +143,13 @@ public function requireDefaultRecords()
srand();
}

public function validate()
public function getCMSCompositeValidator(): CompositeValidator
{
$result = parent::validate();
if (!$this->Name) {
$result->addFieldError('Name', '"Name" can\'t be blank');
}
return $result;
$validator = parent::getCMSCompositeValidator();
$validator->addValidator(RequiredFields::create([
'Name',
]));
return $validator;
}

/**
Expand Down

0 comments on commit 2483724

Please sign in to comment.