mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
ENHANCEMENT Added support for custom CSS classes to Form (similiar to FormField implementation) through Form->addExtraClass()
MINOR Improved formatting and readability of Form->FormAttributes() git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@69742 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
085346f0e0
commit
a16825d9a3
@ -87,6 +87,11 @@ class Form extends RequestHandler {
|
|||||||
* @var boolean
|
* @var boolean
|
||||||
*/
|
*/
|
||||||
public $jsValidationIncluded = false;
|
public $jsValidationIncluded = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var $extraClasses array Extra CSS-classes for the formfield-container
|
||||||
|
*/
|
||||||
|
protected $extraClasses = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new form, with the given fields an action buttons.
|
* Create a new form, with the given fields an action buttons.
|
||||||
@ -442,19 +447,35 @@ class Form extends RequestHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the attributes of the form tag - used by the templates
|
* Return the attributes of the form tag - used by the templates.
|
||||||
|
*
|
||||||
* @return string The attribute string
|
* @return string The attribute string
|
||||||
*/
|
*/
|
||||||
function FormAttributes() {
|
function FormAttributes() {
|
||||||
|
$attributes = array();
|
||||||
|
|
||||||
// Forms shouldn't be cached, cos their error messages won't be shown
|
// Forms shouldn't be cached, cos their error messages won't be shown
|
||||||
HTTP::set_cache_age(0);
|
HTTP::set_cache_age(0);
|
||||||
|
|
||||||
|
// workaround to include javascript validation
|
||||||
if($this->validator && !$this->jsValidationIncluded) $this->validator->includeJavascriptValidation();
|
if($this->validator && !$this->jsValidationIncluded) $this->validator->includeJavascriptValidation();
|
||||||
if($this->target) $target = " target=\"".$this->target."\"";
|
|
||||||
else $target = "";
|
// compile attributes
|
||||||
|
$attributes['id'] = $this->FormName();
|
||||||
return "id=\"" . $this->FormName() . "\" action=\"" . $this->FormAction()
|
$attributes['action'] = $this->FormAction();
|
||||||
. "\" method=\"" . $this->FormMethod() . "\" enctype=\"" . $this->FormEncType() . "\"$target";
|
$attributes['method'] = $this->FormMethod();
|
||||||
|
$attributes['enctype'] = $this->FormEncType();
|
||||||
|
if($this->target) $attributes['target'] = $this->target;
|
||||||
|
if($this->extraClass()) $attributes['class'] = $this->extraClass();
|
||||||
|
|
||||||
|
// implode attributes into string
|
||||||
|
$preparedAttributes = '';
|
||||||
|
foreach($attributes as $k => $v) {
|
||||||
|
// Note: as indicated by the $k == value item here; the decisions over what to include in the attributes can sometimes get finicky
|
||||||
|
if(!empty($v) || $v === '0' || $k == 'value') $preparedAttributes .= " $k=\"" . Convert::raw2att($v) . "\"";
|
||||||
|
}
|
||||||
|
|
||||||
|
return $preparedAttributes;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1063,6 +1084,33 @@ class Form extends RequestHandler {
|
|||||||
self::$current_action = $action;
|
self::$current_action = $action;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compiles all CSS-classes.
|
||||||
|
*
|
||||||
|
* @return String CSS-classnames, separated by a space
|
||||||
|
*/
|
||||||
|
function extraClass() {
|
||||||
|
return implode($this->extraClasses, " ");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a CSS-class to the form-container.
|
||||||
|
*
|
||||||
|
* @param $class String
|
||||||
|
*/
|
||||||
|
function addExtraClass($class) {
|
||||||
|
$this->extraClasses[$class] = $class;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove a CSS-class from the form-container.
|
||||||
|
*
|
||||||
|
* @param $class String
|
||||||
|
*/
|
||||||
|
function removeExtraClass($class) {
|
||||||
|
if(array_key_exists($class, $this->extraClasses)) unset($this->extraClasses[$class]);
|
||||||
|
}
|
||||||
|
|
||||||
function debug() {
|
function debug() {
|
||||||
$result = "<h3>$this->class</h3><ul>";
|
$result = "<h3>$this->class</h3><ul>";
|
||||||
foreach($this->fields as $field) {
|
foreach($this->fields as $field) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user