From 301989d2035c6b2c6f0ba2510d501a6541136273 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Thu, 18 Jun 2009 08:22:30 +0000 Subject: [PATCH] BUGFIX #3922 simon_w: Perform form validation after the clicked button has been set. git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@79567 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- forms/Form.php | 65 +++++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/forms/Form.php b/forms/Form.php index f1f3b84fe..be35065ee 100644 --- a/forms/Form.php +++ b/forms/Form.php @@ -200,38 +200,6 @@ class Form extends RequestHandler { // Populate the form $this->loadDataFrom($vars, true); - // Validate the form - if(!$this->validate()) { - if(Director::is_ajax()) { - // Special case for legacy Validator.js implementation (assumes eval'ed javascript collected through FormResponse) - if($this->validator->getJavascriptValidationHandler() == 'prototype') { - return FormResponse::respond(); - } else { - $acceptType = $request->getHeader('Accept'); - if(strpos($acceptType, 'application/json') !== FALSE) { - // Send validation errors back as JSON with a flag at the start - $response = new HTTPResponse(Convert::array2json($this->validator->getErrors())); - $response->addHeader('Content-Type', 'application/json'); - } else { - $this->setupFormErrors(); - // Send the newly rendered form tag as HTML - $response = new HTTPResponse($this->forTemplate()); - $response->addHeader('Content-Type', 'text/html'); - } - - return $response; - } - } else { - if($this->getRedirectToFormOnValidationError()) { - if($pageURL = $request->getHeader('Referer')) { - return Director::redirect($pageURL . '#' . $this->FormName()); - } - } - return Director::redirectBack(); - } - } - - // Protection against CSRF attacks if($this->securityTokenEnabled()) { $securityID = Session::get('SecurityID'); @@ -267,7 +235,38 @@ class Form extends RequestHandler { if(isset($funcName)) { $this->setButtonClicked($funcName); } - + + // Validate the form + if(!$this->validate()) { + if(Director::is_ajax()) { + // Special case for legacy Validator.js implementation (assumes eval'ed javascript collected through FormResponse) + if($this->validator->getJavascriptValidationHandler() == 'prototype') { + return FormResponse::respond(); + } else { + $acceptType = $request->getHeader('Accept'); + if(strpos($acceptType, 'application/json') !== FALSE) { + // Send validation errors back as JSON with a flag at the start + $response = new HTTPResponse(Convert::array2json($this->validator->getErrors())); + $response->addHeader('Content-Type', 'application/json'); + } else { + $this->setupFormErrors(); + // Send the newly rendered form tag as HTML + $response = new HTTPResponse($this->forTemplate()); + $response->addHeader('Content-Type', 'text/html'); + } + + return $response; + } + } else { + if($this->getRedirectToFormOnValidationError()) { + if($pageURL = $request->getHeader('Referer')) { + return Director::redirect($pageURL . '#' . $this->FormName()); + } + } + return Director::redirectBack(); + } + } + // First, try a handler method on the controller if($this->controller->hasMethod($funcName)) { return $this->controller->$funcName($vars, $this, $request);