Catalin Farcas
Catalin Farcas

Reputation: 655

Silverstripe fields validation in CMS

Note: version 3.1

enter image description here

Trying to validate the input of this fields from CMS:

I tried even a simple validation (required) but doesn't work.

public function updateCMSFields(FieldList $fields) {
    $publishDatetimeField = new DatetimeField( 'PublishDate', 'Publish Date'   );
    $expiryDatetimeField = new DatetimeField( 'ExpiryDate', 'Expiry Date' );
    $fields->addFieldToTab('Root.Options', $publishDatetimeField);
    $fields->addFieldToTab('Root.Options', $expiryDatetimeField);
}
public function getCMSValidator(){
    return new RequiredFields('publishDatetimeField');
}

I can manipulate the values and compare them, but i can't access them.

Any ideas, are welcome.

Upvotes: 1

Views: 1157

Answers (1)

Barry
Barry

Reputation: 3318

You should be able to use the basic validator in terms of checking a field isn't blank... but you should be using the name of the field "PublishDate", not "publishDatetimeField".

In general this is how validations are fully set in silverstripe...

class MyDataObject extends DataObject {

    static $db = array(
        'MyDateField'       => 'SS_DateTime',
    );

    function getCMSValidator() {
        return new MyDataObject_Validator();
    }
}

class MyDataObject_Validator extends RequiredFields {

    function php($data) {
        $bRet = parent::php($data);

        //do checking here
        if (empty($data['MyDateField']))
            $this->validationError('MyDateField','MyDateField cannot be empty','required');

        return count($this->getErrors());
    }
}

you can check for what the data...

die(var_dump($data));

and this should be a string in MYSQL format... like "2016-03-24 11:41:00"

Upvotes: 2

Related Questions