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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

getCMSValidator not working


Go to End


4 Posts   2095 Views

Avatar
lozhowlett

Community Member, 151 Posts

16 April 2014 at 2:43am

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

Community Member, 8 Posts

1 May 2014 at 1:50pm

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

Community Member, 151 Posts

15 May 2014 at 2:37am

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

Avatar
Phat

Community Member, 8 Posts

16 May 2014 at 6:10pm

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.