Webylon 3.1 API Docs
  • Package
  • Class
  • Tree
  • Deprecated
  • Download
Version: current
  • 3.2
  • 3.1

Packages

  • auth
  • Booking
  • cart
    • shipping
    • steppedcheckout
  • Catalog
  • cms
    • assets
    • batchaction
    • batchactions
    • bulkloading
    • comments
    • content
    • core
    • export
    • newsletter
    • publishers
    • reports
    • security
    • tasks
  • Dashboard
  • DataObjectManager
  • event
  • faq
  • forms
    • actions
    • core
    • fields-basic
    • fields-dataless
    • fields-datetime
    • fields-files
    • fields-formatted
    • fields-formattedinput
    • fields-relational
    • fields-structural
    • transformations
    • validators
  • googlesitemaps
  • guestbook
  • installer
  • newsletter
  • None
  • photo
    • gallery
  • PHP
  • polls
  • recaptcha
  • sapphire
    • api
    • bulkloading
    • control
    • core
    • cron
    • dev
    • email
    • fields-formattedinput
    • filesystem
    • formatters
    • forms
    • i18n
    • integration
    • misc
    • model
    • parsers
    • search
    • security
    • tasks
    • testing
    • tools
    • validation
    • view
    • widgets
  • seo
    • open
      • graph
  • sfDateTimePlugin
  • spamprotection
  • stealth
    • captha
  • subsites
  • userform
    • pagetypes
  • userforms
  • webylon
  • widgets

Classes

  • DataObjectManager_Popup
  • FileDataObjectManager_Popup
  • Form
  • Form_FieldMap
  • FormField
  • FormResponse
  • ImageDataObjectManager_Popup
  • MediawebPage_Popup
  • Order_CancelForm
  • PhotoAlbumManager_Popup
   1 <?php
   2 /**
   3  * Base class for all forms.
   4  * The form class is an extensible base for all forms on a sapphire application.  It can be used
   5  * either by extending it, and creating processor methods on the subclass, or by creating instances
   6  * of form whose actions are handled by the parent controller.
   7  *
   8  * In either case, if you want to get a form to do anything, it must be inextricably tied to a
   9  * controller.  The constructor is passed a controller and a method on that controller.  This method
  10  * should return the form object, and it shouldn't require any arguments.  Parameters, if necessary,
  11  * can be passed using the URL or get variables.  These restrictions are in place so that we can
  12  * recreate the form object upon form submission, without the use of a session, which would be too
  13  * resource-intensive.
  14  * 
  15  * You will need to create at least one method for processing the submission (through {@link FormAction}).
  16  * This method will be passed two parameters: the raw request data, and the form object.
  17  * Usually you want to save data into a {@link DataObject} by using {@link saveInto()}.
  18  * If you want to process the submitted data in any way, please use {@link getData()} rather than
  19  * the raw request data.
  20  * 
  21  * <h2>Validation</h2>
  22  * Each form needs some form of {@link Validator} to trigger the {@link FormField->validate()} methods for each field.
  23  * You can't disable validator for security reasons, because crucial behaviour like extension checks for file uploads depend on it.
  24  * The default validator is an instance of {@link RequiredFields}.
  25  * If you want to enforce serverside-validation to be ignored for a specific {@link FormField},
  26  * you need to subclass it.
  27  *
  28  * <h2>URL Handling</h2>
  29  * The form class extends {@link RequestHandler}, which means it can
  30  * be accessed directly through a URL. This can be handy for refreshing
  31  * a form by ajax, or even just displaying a single form field.
  32  * You can find out the base URL for your form by looking at the
  33  * <form action="..."> value. For example, the edit form in the CMS would be located at
  34  * "admin/EditForm". This URL will render the form without its surrounding
  35  * template when called through GET instead of POST. 
  36  * 
  37  * By appending to this URL, you can render invidual form elements
  38  * through the {@link FormField->FieldHolder()} method.
  39  * For example, the "URLSegment" field in a standard CMS form would be
  40  * accessible through "admin/EditForm/field/URLSegment/FieldHolder".
  41  *
  42  * @package forms
  43  * @subpackage core
  44  */
  45 class Form extends RequestHandler {
  46     
  47     /**
  48      * @var boolean $includeFormTag Accessed by Form.ss; modified by {@link formHtmlContent()}.
  49      * A performance enhancement over the generate-the-form-tag-and-then-remove-it code that was there previously
  50      */
  51     public $IncludeFormTag = true;
  52     
  53     protected $fields;
  54     
  55     protected $actions;
  56     
  57     protected $controller;
  58     
  59     protected $name;
  60 
  61     protected $validator;
  62     
  63     protected $formMethod = "post";
  64     
  65     protected static $current_action;
  66     
  67     /**
  68      * @var Dataobject $record Populated by {@link loadDataFrom()}.
  69      */
  70     protected $record;
  71 
  72     /**
  73      * Keeps track of whether this form has a default action or not.
  74      * Set to false by $this->disableDefaultAction();
  75      */
  76     protected $hasDefaultAction = true;
  77 
  78     /**
  79      * Target attribute of form-tag.
  80      * Useful to open a new window upon
  81      * form submission.
  82      *
  83      * @var string
  84      */
  85     protected $target;
  86     
  87     /**
  88      * Legend value, to be inserted into the 
  89      * <legend> element before the <fieldset>
  90      * in Form.ss template.
  91      *
  92      * @var string
  93      */
  94     protected $legend;
  95     
  96     /**
  97      * The SS template to render this form HTML into.
  98      * Default is "Form", but this can be changed to
  99      * another template for customisation.
 100      * 
 101      * @see Form->setTemplate()
 102      * @var string
 103      */
 104     protected $template;
 105     
 106     protected $buttonClickedFunc;
 107     
 108     protected $message;
 109     
 110     protected $messageType;
 111     
 112     /**
 113      * Should we redirect the user back down to the 
 114      * the form on validation errors rather then just the page
 115      * 
 116      * @var bool
 117      */
 118     protected $redirectToFormOnValidationError = false;
 119     
 120     protected $security = true;
 121     
 122     /**
 123      * HACK This is a temporary hack to allow multiple calls to includeJavascriptValidation on
 124      * the validator (if one is present).
 125      *
 126      * @var boolean
 127      */
 128     public $jsValidationIncluded = false;
 129     
 130     /**
 131      * @var $extraClasses array Extra CSS-classes for the formfield-container
 132      */
 133     protected $extraClasses = array();
 134 
 135     /**
 136      * Create a new form, with the given fields an action buttons.
 137      * 
 138      * @param Controller $controller The parent controller, necessary to create the appropriate form action tag.
 139      * @param String $name The method on the controller that will return this form object.
 140      * @param FieldSet $fields All of the fields in the form - a {@link FieldSet} of {@link FormField} objects.
 141      * @param FieldSet $actions All of the action buttons in the form - a {@link FieldSet} of {@link FormAction} objects
 142      * @param Validator $validator Override the default validator instance (Default: {@link RequiredFields})
 143      */
 144     function __construct($controller, $name, FieldSet $fields, FieldSet $actions, $validator = null) {
 145         parent::__construct();
 146 
 147         $fields->setForm($this);
 148         $actions->setForm($this);
 149 
 150         $this->fields = $fields;
 151         $this->actions = $actions;
 152         $this->controller = $controller;
 153         $this->name = $name;
 154         
 155         if(!$this->controller) user_error("$this->class form created without a controller", E_USER_ERROR);
 156 
 157         // Form validation
 158         $this->validator = ($validator) ? $validator : new RequiredFields();
 159         $this->validator->setForm($this);
 160 
 161         // Form error controls
 162         $this->setupFormErrors();
 163         
 164         // Check if CSRF protection is enabled, either on the parent controller or from the default setting. Note that
 165         // method_exists() is used as some controllers (e.g. GroupTest) do not always extend from Object.
 166         if(method_exists($controller, 'securityTokenEnabled')) {
 167             $this->security = $controller->securityTokenEnabled();
 168         } else {
 169             $this->security = self::$default_security;
 170         }
 171     }
 172     
 173     static $url_handlers = array(
 174         'field/$FieldName!' => 'handleField',
 175         '$Action!' => 'handleAction',
 176         'POST ' => 'httpSubmission',
 177         'GET ' => 'httpSubmission',
 178     );
 179     
 180     /**
 181      * Set up current form errors in session to
 182      * the current form if appropriate.
 183      */
 184     function setupFormErrors() {
 185         $errorInfo = Session::get("FormInfo.{$this->FormName()}");
 186 
 187         if(isset($errorInfo['errors']) && is_array($errorInfo['errors'])) {
 188             foreach($errorInfo['errors'] as $error) {
 189                 $field = $this->fields->dataFieldByName($error['fieldName']);
 190 
 191                 if(!$field) {
 192                     $errorInfo['message'] = $error['message'];
 193                     $errorInfo['type'] = $error['messageType'];
 194                 } else {
 195                     $field->setError($error['message'], $error['messageType']);
 196                 }
 197             }
 198 
 199             // load data in from previous submission upon error
 200             if(isset($errorInfo['data'])) $this->loadDataFrom($errorInfo['data']);
 201         }
 202 
 203         if(isset($errorInfo['message']) && isset($errorInfo['type'])) {
 204             $this->setMessage($errorInfo['message'], $errorInfo['type']);
 205         }
 206     }
 207     
 208     /**
 209      * Handle a form submission.  GET and POST requests behave identically.
 210      * Populates the form with {@link loadDataFrom()}, calls {@link validate()},
 211      * and only triggers the requested form action/method
 212      * if the form is valid.
 213      */
 214     function httpSubmission($request) {
 215         $vars = $request->requestVars();
 216         if(isset($funcName)) {
 217             Form::set_current_action($funcName);
 218         }
 219         
 220         // Populate the form
 221         $this->loadDataFrom($vars, true);
 222         
 223         // Protection against CSRF attacks
 224         if($this->securityTokenEnabled()) {
 225             $securityID = Session::get('SecurityID');
 226 
 227             if(!$securityID || !isset($vars['SecurityID']) || $securityID != $vars['SecurityID']) {
 228                 $this->httpError(400, "SecurityID doesn't match, possible CSRF attack.");
 229             }
 230         }
 231         
 232         // Determine the action button clicked
 233         $funcName = null;
 234         foreach($vars as $paramName => $paramVal) {
 235             if(substr($paramName,0,7) == 'action_') {
 236                 // Break off querystring arguments included in the action
 237                 if(strpos($paramName,'?') !== false) {
 238                     list($paramName, $paramVars) = explode('?', $paramName, 2);
 239                     $newRequestParams = array();
 240                     parse_str($paramVars, $newRequestParams);
 241                     $vars = array_merge((array)$vars, (array)$newRequestParams);
 242                 }
 243                 
 244                 // Cleanup action_, _x and _y from image fields
 245                 $funcName = preg_replace(array('/^action_/','/_x$|_y$/'),'',$paramName);
 246                 break;
 247             }
 248         }
 249         
 250         // If the action wasnt' set, choose the default on the form.
 251         if(!isset($funcName) && $defaultAction = $this->defaultAction()){
 252             $funcName = $defaultAction->actionName();
 253         }
 254             
 255         if(isset($funcName)) {
 256             $this->setButtonClicked($funcName);
 257         }
 258         
 259         // Validate the form
 260         if(!$this->validate()) {
 261             if(Director::is_ajax()) {
 262                 // Special case for legacy Validator.js implementation (assumes eval'ed javascript collected through FormResponse)
 263                 if($this->validator->getJavascriptValidationHandler() == 'prototype') {
 264                     return FormResponse::respond();
 265                 } else {
 266                     $acceptType = $request->getHeader('Accept');
 267                     if(strpos($acceptType, 'application/json') !== FALSE) {
 268                         // Send validation errors back as JSON with a flag at the start
 269                         $response = new SS_HTTPResponse(Convert::array2json($this->validator->getErrors()));
 270                         $response->addHeader('Content-Type', 'application/json');
 271                     } else {
 272                         $this->setupFormErrors();
 273                         // Send the newly rendered form tag as HTML
 274                         $response = new SS_HTTPResponse($this->forTemplate());
 275                         $response->addHeader('Content-Type', 'text/html');
 276                     }
 277                     
 278                     return $response;
 279                 }
 280             } else {
 281                 if($this->getRedirectToFormOnValidationError()) {
 282                     if($pageURL = $request->getHeader('Referer')) {
 283                         if(Director::is_site_url($pageURL)) {
 284                             // Remove existing pragmas
 285                             $pageURL = preg_replace('/(#.*)/', '', $pageURL);
 286                             return Director::redirect($pageURL . '#' . $this->FormName());
 287                         }
 288                     }
 289                 }
 290                 return Director::redirectBack();
 291             }
 292         }
 293         
 294         // First, try a handler method on the controller
 295         if($this->controller->hasMethod($funcName)) {
 296             return $this->controller->$funcName($vars, $this, $request);
 297 
 298         // Otherwise, try a handler method on the form object
 299         } else {
 300             if($this->hasMethod($funcName)) {
 301                 return $this->$funcName($vars, $this, $request);
 302             }
 303         }
 304     }
 305     
 306     /**
 307      * Handle a field request.
 308      * Uses {@link Form->dataFieldByName()} to find a matching field,
 309      * and falls back to {@link FieldSet->fieldByName()} to look
 310      * for tabs instead. This means that if you have a tab and a
 311      * formfield with the same name, this method gives priority
 312      * to the formfield.
 313      * 
 314      * @param SS_HTTPRequest $request
 315      * @return FormField
 316      */
 317     function handleField($request) {
 318         $field = $this->dataFieldByName($request->param('FieldName'));
 319         
 320         if($field) {
 321             return $field;
 322         } else {
 323             // falling back to fieldByName, e.g. for getting tabs
 324             return $this->Fields()->fieldByName($request->param('FieldName'));
 325         }
 326     }
 327 
 328     /**
 329      * Convert this form into a readonly form
 330      */
 331     function makeReadonly() {
 332         $this->transform(new ReadonlyTransformation());
 333     }
 334     
 335     /**
 336      * Set whether the user should be redirected back down to the 
 337      * form on the page upon validation errors in the form or if 
 338      * they just need to redirect back to the page
 339      *
 340      * @param bool Redirect to the form
 341      */
 342     public function setRedirectToFormOnValidationError($bool) {
 343         $this->redirectToFormOnValidationError = $bool;
 344     }
 345     
 346     /**
 347      * Get whether the user should be redirected back down to the
 348      * form on the page upon validation errors
 349      *
 350      * @return bool
 351      */
 352     public function getRedirectToFormOnValidationError() {
 353         return $this->redirectToFormOnValidationError;
 354     }
 355 
 356     /**
 357      * Add an error message to a field on this form.  It will be saved into the session
 358      * and used the next time this form is displayed.
 359      */
 360     function addErrorMessage($fieldName, $message, $messageType) {
 361         Session::add_to_array("FormInfo.{$this->FormName()}.errors",  array(
 362             'fieldName' => $fieldName,
 363             'message' => $message,
 364             'messageType' => $messageType,
 365         ));
 366     }
 367 
 368     function transform(FormTransformation $trans) {
 369         $newFields = new FieldSet();
 370         foreach($this->fields as $field) {
 371             $newFields->push($field->transform($trans));
 372         }
 373         $this->fields = $newFields;
 374 
 375         $newActions = new FieldSet();
 376         foreach($this->actions as $action) {
 377             $newActions->push($action->transform($trans));
 378         }
 379         $this->actions = $newActions;
 380 
 381 
 382         // We have to remove validation, if the fields are not editable ;-)
 383         if($this->validator)
 384             $this->validator->removeValidation();
 385     }
 386     
 387     /**
 388      * Get the {@link Validator} attached to this form.
 389      * @return Validator
 390      */
 391     function getValidator() {
 392         return $this->validator;
 393     }
 394 
 395     /**
 396      * Set the {@link Validator} on this form.
 397      */
 398     function setValidator( Validator $validator ) {
 399         if($validator) {
 400             $this->validator = $validator;
 401             $this->validator->setForm($this);
 402         }
 403     }
 404 
 405     /**
 406      * Remove the {@link Validator} from this from.
 407      */
 408     function unsetValidator(){
 409         $this->validator = null;
 410     }
 411 
 412     /**
 413      * Convert this form to another format.
 414      */
 415     function transformTo(FormTransformation $format) {
 416         $newFields = new FieldSet();
 417         foreach($this->fields as $field) {
 418             $newFields->push($field->transformTo($format));
 419         }
 420         $this->fields = $newFields;
 421 
 422         // We have to remove validation, if the fields are not editable ;-)
 423         if($this->validator)
 424             $this->validator->removeValidation();
 425     }
 426 
 427         
 428     /**
 429      * Generate extra special fields - namely the SecurityID field
 430      * 
 431      * @return FieldSet
 432      */
 433     public function getExtraFields() {
 434         $extraFields = new FieldSet();
 435         
 436         if(!$this->fields->fieldByName('SecurityID') && $this->securityTokenEnabled()) {
 437             if(Session::get('SecurityID')) {
 438                 $securityID = Session::get('SecurityID');
 439             } else {
 440                 $securityID = rand();
 441                 Session::set('SecurityID', $securityID);
 442             }
 443             
 444             $securityField = new HiddenField('SecurityID', '', $securityID);
 445             $securityField->setForm($this);
 446             $extraFields->push($securityField);
 447             $this->securityTokenAdded = true;
 448         }
 449         
 450         // add the "real" HTTP method if necessary (for PUT, DELETE and HEAD)
 451         if($this->FormMethod() != $this->FormHttpMethod()) {
 452             $methodField = new HiddenField('_method', '', $this->FormHttpMethod());
 453             $methodField->setForm($this);
 454             $extraFields->push($methodField);
 455         }
 456         
 457         return $extraFields;
 458     }
 459     
 460     /**
 461      * Return the form's fields - used by the templates
 462      * 
 463      * @return FieldSet The form fields
 464      */
 465     function Fields() {
 466         foreach($this->getExtraFields() as $field) {
 467             if(!$this->fields->fieldByName($field->Name())) $this->fields->push($field);
 468         }
 469         
 470         return $this->fields;
 471     }
 472     
 473     /**
 474      * Return all <input type="hidden"> fields
 475      * in a form - including fields nested in {@link CompositeFields}.
 476      * Useful when doing custom field layouts.
 477      * 
 478      * @return FieldSet
 479      */
 480     function HiddenFields() {
 481         return $this->fields->HiddenFields();
 482     }
 483     
 484     /**
 485      * Setter for the form fields.
 486      *
 487      * @param FieldSet $fields
 488      */
 489     function setFields($fields) {
 490         $this->fields = $fields;
 491     }
 492     
 493     /**
 494      * Get a named field from this form's fields.
 495      * It will traverse into composite fields for you, to find the field you want.
 496      * It will only return a data field.
 497      * 
 498      * @return FormField
 499      */
 500     function dataFieldByName($name) {
 501         foreach($this->getExtraFields() as $field) {
 502             if(!$this->fields->dataFieldByName($field->Name())) $this->fields->push($field);
 503         }
 504         
 505         return $this->fields->dataFieldByName($name);
 506     }
 507 
 508     /**
 509      * Return the form's action buttons - used by the templates
 510      * 
 511      * @return FieldSet The action list
 512      */
 513     function Actions() {
 514         return $this->actions;
 515     }
 516 
 517     /**
 518      * Setter for the form actions.
 519      *
 520      * @param FieldSet $actions
 521      */
 522     function setActions($actions) {
 523         $this->actions = $actions;
 524     }
 525     
 526     /**
 527      * Unset all form actions
 528      */
 529     function unsetAllActions(){
 530         $this->actions = new FieldSet();
 531     }
 532 
 533     /**
 534      * Unset the form's action button by its name.
 535      * 
 536      * @param string $name
 537      */
 538     function unsetActionByName($name) {
 539         $this->actions->removeByName($name);
 540     }
 541 
 542     /**
 543      * Unset the form's dataField by its name
 544      */
 545     function unsetDataFieldByName($fieldName){
 546         foreach($this->Fields()->dataFields() as $child) {
 547             if(is_object($child) && ($child->Name() == $fieldName || $child->Title() == $fieldName)) {
 548                 $child = null;
 549             }
 550         }
 551     }
 552     
 553     /**
 554      * Remove a field from the given tab.
 555      */
 556     public function unsetFieldFromTab($tabName, $fieldName) {
 557         // Find the tab
 558         $tab = $this->Fields()->findOrMakeTab($tabName);
 559         $tab->removeByName($fieldName);
 560     }
 561 
 562     /**
 563      * Return the attributes of the form tag - used by the templates.
 564      * 
 565      * @return string The attribute string
 566      */
 567     function FormAttributes() {
 568         $attributes = array();
 569         
 570         // Forms shouldn't be cached, cos their error messages won't be shown
 571         HTTP::set_cache_age(0);
 572 
 573         // workaround to include javascript validation
 574         if($this->validator && !$this->jsValidationIncluded) $this->validator->includeJavascriptValidation();
 575         
 576         // compile attributes       
 577         $attributes['id'] = $this->FormName();
 578         $attributes['action'] = $this->FormAction();
 579         $attributes['method'] = $this->FormMethod();
 580         $attributes['enctype'] = $this->FormEncType();
 581         if($this->target) $attributes['target'] = $this->target;
 582         if($this->extraClass()) $attributes['class'] = $this->extraClass();
 583         if($this->validator && $this->validator->getErrors()) {
 584             if(!isset($attributes['class'])) $attributes['class'] = '';
 585             $attributes['class'] .= ' validationerror';
 586         }
 587         
 588         // implode attributes into string
 589         $preparedAttributes = '';
 590         foreach($attributes as $k => $v) {
 591             // Note: as indicated by the $k == value item here; the decisions over what to include in the attributes can sometimes get finicky
 592             if(!empty($v) || $v === '0' || $k == 'value') $preparedAttributes .= " $k=\"" . Convert::raw2att($v) . "\"";
 593         }
 594         
 595         return $preparedAttributes;
 596     }
 597 
 598     /**
 599     * Set the target of this form to any value - useful for opening the form contents in a new window or refreshing another frame
 600     * 
 601     * @param target The value of the target
 602     */
 603     function setTarget($target) {
 604         $this->target = $target;
 605     }
 606     
 607     /**
 608      * Set the legend value to be inserted into
 609      * the <legend> element in the Form.ss template.
 610      */
 611     function setLegend($legend) {
 612         $this->legend = $legend;
 613     }
 614     
 615     /**
 616      * Set the SS template that this form should use
 617      * to render with. The default is "Form".
 618      * 
 619      * @param string $template The name of the template (without the .ss extension)
 620      */
 621     function setTemplate($template) {
 622         $this->template = $template;
 623     }
 624     
 625     /**
 626      * Return the template to render this form with.
 627      * If the template isn't set, then default to the
 628      * form class name e.g "Form".
 629      * 
 630      * @return string
 631      */
 632     function getTemplate() {
 633         if($this->template) return $this->template;
 634         else return $this->class;
 635     }
 636     
 637     /**
 638      * Returns the encoding type of the form.
 639      * This will be either "multipart/form-data"" if there are any {@link FileField} instances,
 640      * otherwise "application/x-www-form-urlencoded"
 641      * 
 642      * @return string The encoding mime type
 643      */
 644     function FormEncType() {
 645         if(is_array($this->fields->dataFields())){
 646             foreach($this->fields->dataFields() as $field) {
 647                 if(is_a($field, "FileField")) return "multipart/form-data";
 648             }
 649         }
 650         return "application/x-www-form-urlencoded";
 651     }
 652     
 653     /**
 654      * Returns the real HTTP method for the form:
 655      * GET, POST, PUT, DELETE or HEAD.
 656      * As most browsers only support GET and POST in
 657      * form submissions, all other HTTP methods are
 658      * added as a hidden field "_method" that
 659      * gets evaluated in {@link Director::direct()}.
 660      * See {@link FormMethod()} to get a HTTP method
 661      * for safe insertion into a <form> tag.
 662      * 
 663      * @return string HTTP method
 664      */
 665     function FormHttpMethod() {
 666         return $this->formMethod;
 667     }
 668     
 669     /**
 670      * Returns the form method to be used in the <form> tag.
 671      * See {@link FormHttpMethod()} to get the "real" method.
 672      * 
 673      * @return string Form tag compatbile HTTP method: 'get' or 'post'
 674      */
 675     function FormMethod() {
 676         if(in_array($this->formMethod,array('get','post'))) {
 677             return $this->formMethod;
 678         } else {
 679             return 'post';
 680         }
 681     }
 682     
 683     /**
 684      * Set the form method: GET, POST, PUT, DELETE.
 685      * 
 686      * @param $method string
 687      */
 688     function setFormMethod($method) {
 689         $this->formMethod = strtolower($method);
 690     }
 691     
 692     /**
 693      * Return the form's action attribute.
 694      * This is build by adding an executeForm get variable to the parent controller's Link() value
 695      * 
 696      * @return string 
 697      */
 698     function FormAction() {
 699         if ($this->formActionPath) {
 700             return $this->formActionPath;
 701         } elseif($this->controller->hasMethod("FormObjectLink")) {
 702             return $this->controller->FormObjectLink($this->name);
 703         } else {
 704             return Controller::join_links($this->controller->Link(), $this->name);
 705         }
 706     }
 707     
 708     /** @ignore */
 709     private $formActionPath = false;
 710     
 711     /**
 712      * Set the form action attribute to a custom URL.
 713      * 
 714      * Note: For "normal" forms, you shouldn't need to use this method.  It is recommended only for situations where you have
 715      * two relatively distinct parts of the system trying to communicate via a form post.
 716      */
 717     function setFormAction($path) {
 718         $this->formActionPath = $path;
 719     }
 720 
 721     /**
 722      * @ignore
 723      */
 724     private $htmlID = null;
 725     
 726     /**
 727      * Returns the name of the form
 728      */
 729     function FormName() {
 730         if($this->htmlID) return $this->htmlID;
 731         else return $this->class . '_' . str_replace(array('.','/'),'',$this->name);
 732     }
 733 
 734     /**
 735      * Set the HTML ID attribute of the form
 736      */
 737     function setHTMLID($id) {
 738         $this->htmlID = $id;
 739     }
 740     
 741     /**
 742      * Returns this form's controller
 743      */
 744     function Controller() {
 745         return $this->controller;
 746     }
 747     
 748     /**
 749      * @return string
 750      */
 751     function Name() {
 752         return $this->name;
 753     }
 754     
 755     /**
 756      * Returns an object where there is a method with the same name as each data field on the form.
 757      * That method will return the field itself.
 758      * It means that you can execute $firstNameField = $form->FieldMap()->FirstName(), which can be handy
 759      */
 760     function FieldMap() {
 761         return new Form_FieldMap($this);
 762     }
 763 
 764     /**
 765      * The next functions store and modify the forms
 766      * message attributes. messages are stored in session under
 767      * $_SESSION[formname][message];
 768      * 
 769      * @return string
 770      */
 771     function Message() {
 772         $this->getMessageFromSession();
 773         $message = $this->message;
 774         $this->clearMessage();
 775         return $message;
 776     }
 777     
 778     /**
 779      * @return string
 780      */
 781     function MessageType() {
 782         $this->getMessageFromSession();
 783         return $this->messageType;
 784     }
 785 
 786     protected function getMessageFromSession() {
 787         if($this->message || $this->messageType) {
 788             return $this->message;
 789         }else{
 790             $this->message = Session::get("FormInfo.{$this->FormName()}.formError.message");
 791             $this->messageType = Session::get("FormInfo.{$this->FormName()}.formError.type");
 792 
 793             Session::clear("FormInfo.{$this->FormName()}");
 794         }
 795     }
 796 
 797     /**
 798      * Set a status message for the form.
 799      * 
 800      * @param message the text of the message
 801      * @param type Should be set to good, bad, or warning.
 802      */
 803     function setMessage($message, $type) {
 804         $this->message = $message;
 805         $this->messageType = $type;
 806     }
 807 
 808     /**
 809      * Set a message to the session, for display next time this form is shown.
 810      * 
 811      * @param message the text of the message
 812      * @param type Should be set to good, bad, or warning.
 813      */
 814     function sessionMessage($message, $type) {
 815         Session::set("FormInfo.{$this->FormName()}.formError.message", $message);
 816         Session::set("FormInfo.{$this->FormName()}.formError.type", $type);
 817     }
 818 
 819     static function messageForForm( $formName, $message, $type ) {
 820         Session::set("FormInfo.{$formName}.formError.message", $message);
 821         Session::set("FormInfo.{$formName}.formError.type", $type);
 822     }
 823 
 824     function clearMessage() {
 825         $this->message  = null;
 826         Session::clear("FormInfo.{$this->FormName()}.errors");
 827         Session::clear("FormInfo.{$this->FormName()}.formError");
 828     }
 829     function resetValidation() {
 830         Session::clear("FormInfo.{$this->FormName()}.errors");
 831     }
 832 
 833     /**
 834      * Returns the DataObject that has given this form its data
 835      * through {@link loadDataFrom()}.
 836      * 
 837      * @return DataObject
 838      */
 839     function getRecord() {
 840         return $this->record;
 841     }
 842     
 843     /**
 844      * Get the legend value to be inserted into the
 845      * <legend> element in Form.ss
 846      *
 847      * @return string
 848      */
 849     function getLegend() {
 850         return $this->legend;
 851     }
 852 
 853     /**
 854      * Processing that occurs before a form is executed.
 855      * This includes form validation, if it fails, we redirect back
 856      * to the form with appropriate error messages.
 857      * Triggered through {@link httpSubmission()} which is triggered
 858      */
 859      function validate(){
 860         if($this->validator){
 861             $errors = $this->validator->validate();
 862 
 863             if($errors){
 864                 if(Director::is_ajax() && $this->validator->getJavascriptValidationHandler() == 'prototype') {
 865                     FormResponse::status_message(_t('Form.VALIDATIONFAILED', 'Validation failed'), 'bad');
 866                     foreach($errors as $error) {
 867                         FormResponse::add(sprintf(
 868                             "validationError('%s', '%s', '%s');\n",
 869                             Convert::raw2js($error['fieldName']),
 870                             Convert::raw2js($error['message']),
 871                             Convert::raw2js($error['messageType'])
 872                         ));
 873                     }
 874                 } else {
 875                     $data = $this->getData();
 876 
 877                     // Load errors into session and post back
 878                     Session::set("FormInfo.{$this->FormName()}", array(
 879                         'errors' => $errors,
 880                         'data' => $data,
 881                     ));
 882 
 883                 }
 884                 return false;
 885             }
 886         }
 887         return true;
 888     }
 889 
 890     /**
 891      * Load data from the given DataObject or array.
 892      * It will call $object->MyField to get the value of MyField.
 893      * If you passed an array, it will call $object[MyField].
 894      * Doesn't save into dataless FormFields ({@link DatalessField}),
 895      * as determined by {@link FieldSet->dataFields()}.
 896      * 
 897      * By default, if a field isn't set (as determined by isset()),
 898      * its value will not be saved to the field, retaining
 899      * potential existing values.
 900      * 
 901      * Passed data should not be escaped, and is saved to the FormField instances unescaped.
 902      * Escaping happens automatically on saving the data through {@link saveInto()}.
 903      * 
 904      * @uses FieldSet->dataFields()
 905      * @uses FormField->setValue()
 906      * 
 907      * @param array|DataObject $data
 908      * @param boolean $clearMissingFields By default, fields which don't match
 909      *  a property or array-key of the passed {@link $data} argument are "left alone",
 910      *  meaning they retain any previous values (if present). If this flag is set to true,
 911      *  those fields are overwritten with null regardless if they have a match in {@link $data}.
 912      * @param $fieldList An optional list of fields to process.  This can be useful when you have a 
 913      * form that has some fields that save to one object, and some that save to another.
 914      */
 915     function loadDataFrom($data, $clearMissingFields = false, $fieldList = null) {
 916         if(!is_object($data) && !is_array($data)) {
 917             user_error("Form::loadDataFrom() not passed an array or an object", E_USER_WARNING);
 918             return false;
 919         }
 920 
 921         // if an object is passed, save it for historical reference through {@link getRecord()}
 922         if(is_object($data)) $this->record = $data;
 923 
 924         // dont include fields without data
 925         $dataFields = $this->fields->dataFields();
 926         if($dataFields) foreach($dataFields as $field) {
 927             $name = $field->Name();
 928             
 929             // Skip fields that have been exlcuded
 930             if($fieldList && !in_array($name, $fieldList)) continue;
 931             
 932             // First check looks for (fieldname)_unchanged, an indicator that we shouldn't overwrite the field value
 933             if(is_array($data) && isset($data[$name . '_unchanged'])) continue;
 934             
 935             // get value in different formats
 936             $hasObjectValue = false;
 937             if(
 938                 is_object($data) 
 939                 && (
 940                     isset($data->$name)
 941                     || $data->hasMethod($name)
 942                     || ($data->hasMethod('hasField') && $data->hasField($name))
 943                 )
 944             ) {
 945                 // We don't actually call the method because it might be slow.  
 946                 // In a later release, relation methods will just return references to the query that should be executed, 
 947                 // and so we will be able to safely pass the return value of the 
 948                 // relation method to the first argument of setValue
 949                 $val = $data->__get($name);
 950                 $hasObjectValue = true;
 951             } else if(strpos($name,'[') && is_array($data) && !isset($data[$name])) {
 952                 // if field is in array-notation, we need to resolve the array-structure PHP creates from query-strings
 953                 preg_match('/' . addcslashes($name,'[]') . '=([^&]*)/', urldecode(http_build_query($data)), $matches);
 954                 $val = isset($matches[1]) ? $matches[1] : null;
 955             } elseif(is_array($data) && array_key_exists($name, $data)) {
 956                 // else we assume its a simple keyed array
 957                 $val = $data[$name];
 958             } else {
 959                 $val = null;
 960             }
 961 
 962             // save to the field if either a value is given, or loading of blank/undefined values is forced
 963             if(isset($val) || $hasObjectValue || $clearMissingFields) {
 964                 // pass original data as well so composite fields can act on the additional information
 965                 $field->setValue($val, $data);
 966             }
 967         }
 968     }
 969     
 970     /**
 971      * Save the contents of this form into the given data object.
 972      * It will make use of setCastedField() to do this.
 973      * 
 974      * @param $dataObject The object to save data into
 975      * @param $fieldList An optional list of fields to process.  This can be useful when you have a 
 976      * form that has some fields that save to one object, and some that save to another.
 977      */
 978     function saveInto(DataObjectInterface $dataObject, $fieldList = null) {
 979         $dataFields = $this->fields->saveableFields();
 980         $lastField = null;
 981 
 982         if($dataFields) foreach($dataFields as $field) {
 983             // Skip fields that have been exlcuded
 984             if($fieldList && is_array($fieldList) && !in_array($field->Name(), $fieldList)) continue;
 985 
 986 
 987             $saveMethod = "save{$field->Name()}";
 988 
 989             if($field->Name() == "ClassName"){
 990                 $lastField = $field;
 991             }else if( $dataObject->hasMethod( $saveMethod ) ){
 992                 $dataObject->$saveMethod( $field->dataValue());
 993             } else if($field->Name() != "ID"){
 994                 $field->saveInto($dataObject);
 995             }
 996         }
 997         if($lastField) $lastField->saveInto($dataObject);
 998     }
 999     
1000     /**
1001      * Get the submitted data from this form through
1002      * {@link FieldSet->dataFields()}, which filters out
1003      * any form-specific data like form-actions.
1004      * Calls {@link FormField->dataValue()} on each field,
1005      * which returns a value suitable for insertion into a DataObject
1006      * property.
1007      * 
1008      * @return array
1009      */
1010     function getData() {
1011         $dataFields = $this->fields->dataFields();
1012         $data = array();
1013         
1014         if($dataFields){
1015             foreach($dataFields as $field) {
1016                 if($field->Name()) {
1017                     $data[$field->Name()] = $field->dataValue();
1018                 }
1019             }
1020         }
1021         return $data;
1022     }
1023 
1024     /**
1025      * Resets a specific field to its passed default value.
1026      * Does NOT clear out all submitted data in the form.
1027      * 
1028      * @param string $fieldName
1029      * @param mixed $fieldValue
1030      */
1031     function resetField($fieldName, $fieldValue = null) {
1032         $dataFields = $this->fields->dataFields();
1033         if($dataFields) foreach($dataFields as $field) {
1034             if($field->Name()==$fieldName) {
1035                 $field = $field->setValue($fieldValue);
1036             }
1037         }
1038     }
1039     
1040     /**
1041      * Call the given method on the given field.
1042      * This is used by Ajax-savvy form fields.  By putting '&action=callfieldmethod' to the end
1043      * of the form action, they can access server-side data.
1044      * @param fieldName The name of the field.  Can be overridden by $_REQUEST[fieldName]
1045      * @param methodName The name of the field.  Can be overridden by $_REQUEST[methodName]
1046      */
1047     function callfieldmethod($data) {
1048         $fieldName = $data['fieldName'];
1049         $methodName = $data['methodName'];
1050         $fields = $this->fields->dataFields();
1051 
1052         // special treatment needed for TableField-class and TreeDropdownField
1053         if(strpos($fieldName, '[')) {
1054             preg_match_all('/([^\[]*)/',$fieldName, $fieldNameMatches);
1055             preg_match_all('/\[([^\]]*)\]/',$fieldName, $subFieldMatches);
1056             $tableFieldName = $fieldNameMatches[1][0];
1057             $subFieldName = $subFieldMatches[1][1];
1058         }
1059 
1060         if(isset($tableFieldName) && isset($subFieldName) && is_a($fields[$tableFieldName], 'TableField')) {
1061             $field = $fields[$tableFieldName]->getField($subFieldName, $fieldName);
1062             return $field->$methodName();
1063         } else if(isset($fields[$fieldName])) {
1064             return $fields[$fieldName]->$methodName();
1065         } else {
1066             user_error("Form::callfieldmethod() Field '$fieldName' not found", E_USER_ERROR);
1067         }
1068 
1069     }
1070 
1071     /**
1072      * Return a rendered version of this form.
1073      * 
1074      * This is returned when you access a form as $FormObject rather
1075      * than <% control FormObject %>
1076      */
1077     function forTemplate() {
1078         return $this->renderWith(array(
1079             $this->getTemplate(),
1080             'Form'
1081         ));
1082     }
1083 
1084     /**
1085      * Return a rendered version of this form, suitable for ajax post-back.
1086      * It triggers slightly different behaviour, such as disabling the rewriting of # links
1087      */
1088     function forAjaxTemplate() {
1089         $view = new SSViewer(array(
1090             $this->getTemplate(),
1091             'Form'
1092         ));
1093         
1094         return $view->dontRewriteHashlinks()->process($this);
1095     }
1096 
1097     /**
1098      * Returns an HTML rendition of this form, without the <form> tag itself.
1099      * Attaches 3 extra hidden files, _form_action, _form_name, _form_method, and _form_enctype.  These are
1100      * the attributes of the form.  These fields can be used to send the form to Ajax.
1101      */
1102     function formHtmlContent() {
1103         // Call FormAttributes to force inclusion of custom client-side validation of fields
1104         // because it won't be included by the template
1105         if($this->validator && !$this->jsValidationIncluded) $this->validator->includeJavascriptValidation();
1106         
1107         $this->IncludeFormTag = false;
1108         $content = $this->forTemplate();
1109         $this->IncludeFormTag = true;
1110 
1111         $content .= "<input type=\"hidden\" name=\"_form_action\" id=\"" . $this->FormName . "_form_action\" value=\"" . $this->FormAction() . "\" />\n";
1112         $content .= "<input type=\"hidden\" name=\"_form_name\" value=\"" . $this->FormName() . "\" />\n";
1113         $content .= "<input type=\"hidden\" name=\"_form_method\" value=\"" . $this->FormMethod() . "\" />\n";
1114         $content .= "<input type=\"hidden\" name=\"_form_enctype\" value=\"" . $this->FormEncType() . "\" />\n";
1115 
1116         return $content;
1117     }
1118 
1119     /**
1120      * Render this form using the given template, and return the result as a string
1121      * You can pass either an SSViewer or a template name
1122      */
1123     function renderWithoutActionButton($template) {
1124         $custom = $this->customise(array(
1125             "Actions" => "",
1126         ));
1127 
1128         if(is_string($template)) $template = new SSViewer($template);
1129         return $template->process($custom);
1130     }
1131 
1132 
1133     /**
1134      * Sets the button that was clicked.  This should only be called by the Controller.
1135      * @param funcName The name of the action method that will be called.
1136      */
1137     function setButtonClicked($funcName) {
1138         $this->buttonClickedFunc = $funcName;
1139     }
1140 
1141     function buttonClicked() {
1142         foreach($this->actions as $action) {
1143             if($this->buttonClickedFunc == $action->actionName()) return $action;
1144         }
1145     }
1146 
1147     /**
1148      * Return the default button that should be clicked when another one isn't available
1149      */
1150     function defaultAction() {
1151         if($this->hasDefaultAction && $this->actions)
1152             return $this->actions->First();
1153     }
1154 
1155     /**
1156      * Disable the default button.
1157      * Ordinarily, when a form is processed and no action_XXX button is available, then the first button in the actions list
1158      * will be pressed.  However, if this is "delete", for example, this isn't such a good idea.
1159      */
1160     function disableDefaultAction() {
1161         $this->hasDefaultAction = false;
1162     }
1163     
1164     /**
1165      * Disable the requirement of a SecurityID in the Form. This security protects
1166      * against CSRF attacks, but you should disable this if you don't want to tie 
1167      * a form to a session - eg a search form.
1168      */
1169     function disableSecurityToken() {
1170         $this->security = false;
1171     }
1172     
1173     
1174     private static $default_security = true;
1175     
1176     /**
1177      * Disable security tokens for every form on this site.
1178      */
1179     static function disable_all_security_tokens() {
1180         self::$default_security = false;
1181     }
1182     
1183     /**
1184      * Returns true if security is enabled - that is if the SecurityID
1185      * should be included and checked on this form.
1186      *
1187      * @return bool
1188      */
1189     function securityTokenEnabled() {
1190         return $this->security;
1191     }
1192 
1193     /**
1194      * Returns the name of a field, if that's the only field that the current controller is interested in.
1195      * It checks for a call to the callfieldmethod action.
1196      * This is useful for optimising your forms
1197      * 
1198      * @return string
1199      */
1200     static function single_field_required() {
1201         if(self::current_action() == 'callfieldmethod') return $_REQUEST['fieldName'];
1202     }
1203 
1204     /**
1205      * Return the current form action being called, if available.
1206      * This is useful for optimising your forms
1207      */
1208     static function current_action() {
1209         return self::$current_action;
1210     }
1211 
1212     /**
1213      * Set the current form action.  Should only be called by Controller.
1214      */
1215     static function set_current_action($action) {
1216         self::$current_action = $action;
1217     }
1218     
1219     /**
1220      * Compiles all CSS-classes. 
1221      * 
1222      * @return String CSS-classnames, separated by a space
1223      */
1224     function extraClass() {
1225         return implode($this->extraClasses, " ");
1226     }
1227     
1228     /**
1229      * Add a CSS-class to the form-container.
1230      * 
1231      * @param $class String
1232      */
1233     function addExtraClass($class) {
1234         $this->extraClasses[$class] = $class;
1235     }
1236 
1237     /**
1238      * Remove a CSS-class from the form-container.
1239      * 
1240      * @param $class String
1241      */
1242     function removeExtraClass($class) {
1243         if(array_key_exists($class, $this->extraClasses)) unset($this->extraClasses[$class]);
1244     }
1245     
1246     function debug() {
1247         $result = "<h3>$this->class</h3><ul>";
1248         foreach($this->fields as $field) {
1249             $result .= "<li>$field" . $field->debug() . "</li>";
1250         }
1251         $result .= "</ul>";
1252 
1253         if( $this->validator )
1254                 $result .= '<h3>'._t('Form.VALIDATOR', 'Validator').'</h3>' . $this->validator->debug();
1255 
1256         return $result;
1257     }
1258     
1259     
1260     //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1261     // TESTING HELPERS
1262     //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1263     
1264     /**
1265      * Test a submission of this form.
1266      * @return SS_HTTPResponse the response object that the handling controller produces.  You can interrogate this in your unit test.
1267      */
1268     function testSubmission($action, $data) {
1269         $data['action_' . $action] = true;
1270         
1271         return Director::test($this->FormAction(), $data, Controller::curr()->getSession());
1272         
1273         //$response = $this->controller->run($data);
1274         //return $response;
1275     }
1276     
1277     /**
1278      * Test an ajax submission of this form.
1279      * @return SS_HTTPResponse the response object that the handling controller produces.  You can interrogate this in your unit test.
1280      */
1281     function testAjaxSubmission($action, $data) {
1282         $data['ajax'] = 1;
1283         return $this->testSubmission($action, $data);
1284     }
1285 }
1286 
1287 /**
1288  * @package forms
1289  * @subpackage core
1290  */
1291 class Form_FieldMap extends ViewableData {
1292     protected $form;
1293     
1294     function __construct($form) {
1295         $this->form = $form;
1296         parent::__construct();
1297     }
1298     
1299     /**
1300      * Ensure that all potential method calls get passed to __call(), therefore to dataFieldByName
1301      */
1302     function hasMethod($method) {
1303         return true;
1304     }
1305 
1306     function __call($method, $args = null) {
1307         return $this->form->Fields()->fieldByName($method);
1308     }
1309 }
1310 
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -
Webylon 3.1 API Docs API documentation generated by ApiGen 2.8.0