Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS

getCMSValidator not working


Reply

4 Posts   378 Views

Avatar
lozhowlett

16 April 2014 at 2:43am Community Member, 146 Posts

Hi everyone

This is an odd one, as on some dataobjects its working fine, however here is an example bit of code of it not working. It simply doesnt show the validation, i can even put a die() in there and still it passes. So i guess its just not listening to my class?

Any ideas?

<?php

class TextStatement extends DataObject {

private static $db = array(   
    'PersonName' => 'Varchar',
'Testimonial' => 'Text',
"DateSent" => "Date",
'SortOrder' => 'Int'
);

// One-to-one relationship with gallery page
private static $has_one = array(
'HomePage' => 'HomePage'   
);

// tidy up the CMS by not showing these fields
public function getCMSFields() {
      $fields = parent::getCMSFields();

$fields->removeByName('DateSent');
$enquiry = new DateField('DateSent', 'Date');

      $fields->removeFieldFromTab("Root.Main","HomePageID");
$fields->addFieldToTab('Root.Main', $enquiry->setConfig('showcalendar', true));
      return $fields;      
}

// Tell the datagrid what fields to show in the table
private static $summary_fields = array(
    'PersonName' => 'PersonName' ,
'Testimonial' => 'Testimonial',
"DateSent" => "DateSent"
);
public function getCMSValidator() {
return new TextStatement_Validator();
}

public function getValidator() {
return new TextStatement_Validator();
}

}

class TextStatement_Validator extends RequiredFields {
function php($data) {
$bRet = parent::php($data);
if (empty($data['PersonName'])) {
$this->validationError('PersonName','Persons name is required',"required");
$bRet = false;
}

if (empty($data['Testimonial'])) {
$this->validationError('Testimonial','Testimonial is required',"required");
$bRet = false;
}
if (empty($data['DateSent'])) {
$this->validationError('DateSent','Date sent is required',"required");
$bRet = false;
}
if (empty($data['EnquiryEmailsTo'])) {
$this->validationError('EnquiryEmailsTo','EnquiryEmailsTo is required',"required");
$bRet = false;
}

return $bRet;
}
}

Avatar
Phat

1 May 2014 at 1:50pm Community Member, 8 Posts

Can you try to set the validator manually when you create the GridField to see if it works ? It should work with getCMSValidator automatically with SilverStripe 3.1.4.

$grid = new GridField('TextStatement ', 'Text Statements', $this->TextStatements(),GridFieldConfig_RecordEditor::create());
$grid->getConfig()->getComponentByType('GridFieldDetailForm')
->setValidator(singleton('TextStatement')->getCMSValidator());
$fields->addFieldToTab('Root.Main', $grid);

Avatar
lozhowlett

15 May 2014 at 2:37am Community Member, 146 Posts

Thanks Phat, that works. Odd that it doesnt from the dataobject tho?

Avatar
Phat

16 May 2014 at 6:10pm Community Member, 8 Posts

Which SS version are you running?
For SS 3.1.4, you don't need my code above since SS framework will hook getCMSValidator for you.
Try to update your CMS and framework with the latest version to see if it is working from dataobject.