Jump to:

5513 Posts in 1732 Topics by 1218 members

Customising the CMS

SilverStripe Forums » Customising the CMS » getCMSValidator not working

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

Page: 1
Go to End
Author Topic: 308 Views
  • lozhowlett
    Avatar
    Community Member
    146 Posts

    getCMSValidator not working Link to this post

    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;
    }
    }

  • Phat
    Avatar
    Community Member
    8 Posts

    Re: getCMSValidator not working Link to this post

    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);

  • lozhowlett
    Avatar
    Community Member
    146 Posts

    Re: getCMSValidator not working Link to this post

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

  • Phat
    Avatar
    Community Member
    8 Posts

    Re: getCMSValidator not working Link to this post

    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.

    308 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.