Created
October 31, 2011 01:29
-
-
Save frankmullenger/1326703 to your computer and use it in GitHub Desktop.
Form validation messages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SomeForm extends Form { | |
function validate(){ | |
if($this->validator){ | |
$errors = $this->validator->validate(); | |
if($errors){ | |
if(Director::is_ajax() && $this->validator->getJavascriptValidationHandler() == 'prototype') { | |
FormResponse::status_message(_t('Form.VALIDATIONFAILED', 'Validation failed'), 'bad'); | |
foreach($errors as $error) { | |
FormResponse::add(sprintf( | |
"validationError('%s', '%s', '%s');\n", | |
Convert::raw2js($error['fieldName']), | |
Convert::raw2js($error['message']), | |
Convert::raw2js($error['messageType']) | |
)); | |
} | |
} else { | |
$data = $this->getData(); | |
$formError = array(); | |
if ($formMessageType = $this->MessageType()) { | |
$formError['message'] = $this->Message(); | |
$formError['messageType'] = $formMessageType; | |
} | |
// Load errors into session and post back | |
Session::set("FormInfo.{$this->FormName()}", array( | |
'errors' => $errors, | |
'data' => $data, | |
'formError' => $formError | |
)); | |
} | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SomeValidator extends RequiredFields { | |
function php($data) { | |
$valid = parent::php($data); | |
$fields = $this->form->Fields(); | |
$this->form->sessionMessage( | |
'This message should be displayed as an error for the form.', | |
'bad' | |
); | |
//Have to set errors for validate() | |
$this->errors[] = true; | |
$valid = false; | |
return $valid; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment