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

CMS popup form validation problem


Go to End


3 Posts   2970 Views

Avatar
splatEric

Community Member, 15 Posts

28 September 2010 at 6:49am

Hi,

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.

thanks

mike

<?php
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();
		$fields->push( 
		    new DropDownField( 
		        'PaymentType', 
		        'Type of payment', 
		        singleton('PayPalPayment')->dbObject('PaymentType')->enumValues()
		    ) );
		$fields->push( new TextField( 'Label', 'Payment description' ) );
		$fields->push( new NumericField( 'Amount', 'Value (£s)' ) );
		return $fields;
	}
	
}

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

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

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

        if ( in_array($payment_type, array("Regular payment")) ) {
            if (!$value) {
                $message = sprintf('Need a value for payment type of %s', strtolower($payment_type));
                $this->validationError(
    				"Amount",
    				$message,
    				"required"
    			);
    			$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(
			$this,
			'Donations',
			'PayPalPayment',
			array(
				'PaymentType'	=> 'Type of payment',
				'Label'	=> 'Description',
			),
			'getCMSFields_forPopup'
		);
		$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(); 
      $validator->setForm($this); 
      $this->validator = $validator; 
   } 
}

Avatar
swaiba

Forum Moderator, 1899 Posts

28 September 2010 at 7:21am

I'm not that familiar with DOM, but normally for an SS form add this to the rendering of the form...

if($data = Session::get('PreviousFormsData')) { 
$form->loadDataFrom($data); 
}

then add this to the part that validates...

Session::set('PreviousFormsData',$data))

...
//and if success clear

Session::clear('PreviousFormsData'))

that is what I know, I'd advise asking in the dataobjectmanager section as it is an external module and the code you have would work fine within ModelAdmin (core code) I think...

Barry

Avatar
splatEric

Community Member, 15 Posts

28 September 2010 at 9:10am

Thanks Barry, I've cross posted over to the DOM forum here