CMSForm.php
2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/**
 * Deals with special form handling in CMS, mainly around
 * {@link PjaxResponseNegotiator}
 *
 * @package framework
 * @subpackage admin
 */
class CMSForm extends Form {
	/**
	 * @var array
	 */
	protected $validationExemptActions = array();
	/**
	 * Always return true if the current form action is exempt from validation
	 * 
	 * @return boolean
	 */
	public function validate() {
		$buttonClicked = $this->buttonClicked();
		return (
			($buttonClicked && in_array($buttonClicked->actionName(), $this->getValidationExemptActions()))
			|| parent::validate()
		);
	}
	
	/**
	 * Route validation error responses through response negotiator,
	 * so they return the correct markup as expected by the requesting client.
	 */
	protected function getValidationErrorResponse() {
		$request = $this->getRequest();
		$negotiator = $this->getResponseNegotiator();
		if($request->isAjax() && $negotiator) {
			$this->setupFormErrors();
			$result = $this->forTemplate();
			return $negotiator->respond($request, array(
				'CurrentForm' => function() use($result) {
					return $result;
				}
			));
		} else {
			return parent::getValidationErrorResponse();
		}
	}
	/**
	 * Set actions that are exempt from validation
	 * 
	 * @param array
	 */
	public function setValidationExemptActions($actions) {
		$this->validationExemptActions = $actions;
		return $this;
	}
	/**
	 * Get a list of actions that are exempt from validation
	 * 
	 * @return array
	 */
	public function getValidationExemptActions() {
		return $this->validationExemptActions;
	}
	/**
	 * Sets the response negotiator
	 * @param ResponseNegotiator $negotiator The response negotiator to use
	 * @return Form The current form
	 */
	public function setResponseNegotiator($negotiator) {
		$this->responseNegotiator = $negotiator;
		return $this;
	}
	/**
	 * Gets the current response negotiator
	 * @return ResponseNegotiator|null
	 */
	public function getResponseNegotiator() {
		return $this->responseNegotiator;
	}
	public function FormName() {
		if($this->htmlID) return $this->htmlID;
		else return 'Form_' . str_replace(array('.', '/'), '', $this->name);
	}
}