Jump to:

7940 Posts in 1543 Topics by 946 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Popup validation in the CMS for DOM

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 1201 Views
  • splatEric
    Community Member
    15 Posts

    Popup validation in the CMS for DOM Link to this post


    (FYI this is a cross post from over on the CMS forum, where I was advised to post it over here)

    I've been trawling across the forums etc today, piecing together information to enable me to be able to validate field entries when editing them in a pop up. I've been able to do this (code for reference is below). However, the problem I am experiencing is that when the validation fails, all the form fields are reset to their default (empty) values. Is there any way of patching the validation process to avoid this. It seems to happen whether I create my own custom validator (as shown below), or if I use the RequiredFields validator.

    any help much appreciated.



    class PayPalPayment extends DataObject {
       static $db = array(
          'PaymentType'    => "Enum('One-off payment,Regular payment')",
          'Label'         => 'Varchar(127)',
          'Amount'    => 'Currency',
       static $has_one = array(
          'PageOwner' => 'DonationPage',
       public function getCMSFields_forPopup() {
          $fields = new FieldSet();
           new DropDownField(
           'Type of payment',
           ) );
          $fields->push( new TextField( 'Label', 'Payment description' ) );
          $fields->push( new NumericField( 'Amount', 'Value (£s)' ) );
          return $fields;
       // this is irrelevant when using a popup for it, but have left in place
       // in case of future changes
       public function getCMSValidator() {
        return new PayPalPayment_Validator();

    class PayPalPayment_Validator extends RequiredFields {

    protected $customRequired = array('Label');

    public function __construct() {
    $required = func_get_args();
    if(isset($required[0]) && is_array($required[0])) {
    $required = $required[0];
    $required = array_merge($required, $this->customRequired);


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

    $payment_type = $data['PaymentType'];
    $value = $data['Amount'];

    // Value is required if we are paying regularly
    if ( in_array($payment_type, array("Regular payment")) ) {
    if (!$value) {
    $message = sprintf('Need a value for payment type of %s', strtolower($payment_type));
             $valid = false;

    return $valid;


    and then with the owner ...

    class DonationPage extends Page {
       static $has_many = array(
          'Donations'   => 'PayPalPayment',
       function getCMSFields() {
          $fields = parent::getCMSFields();
          $tablefield = new DataObjectManager(
                'PaymentType'   => 'Type of payment',
                'Label'   => 'Description',
          $tablefield->popupClass = 'PayPalPaymentDataObjectManager_Popup';
          $tablefield->setAddTitle( 'a Donation');
          $fields->addFieldToTab( 'Root.Content.Donations', $tablefield);
          return $fields;

    class DonationPage_Controller extends Page_Controller {

    This bit is here to add the appropriate validator to the pop up form in the cms
    class PayPalPaymentDataObjectManager_Popup extends DataObjectManager_Popup {
    function __construct($controller, $name, $fields, $validator, $readonly, $dataObject) {
    parent::__construct($controller, $name, $fields, $validator, $readonly, $dataObject);
    $validator = new PayPalPayment_Validator();
    $this->validator = $validator;

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.