mirror of
https://github.com/silverstripe/silverstripe-userforms.git
synced 2024-10-22 15:05:42 +00:00
ENHANCEMENT: moved validation of user defined forms to jquery based using the validate plugin. MINOR: moved submitted field infomation to its own folder
This commit is contained in:
parent
984bfdd42d
commit
e429137bb8
@ -227,9 +227,18 @@ class UserDefinedForm extends Page {
|
|||||||
|
|
||||||
class UserDefinedForm_Controller extends Page_Controller {
|
class UserDefinedForm_Controller extends Page_Controller {
|
||||||
|
|
||||||
function init() {
|
/**
|
||||||
Requirements::javascript(THIRDPARTY_DIR . '/prototype.js');
|
* Load all the custom jquery needed to run the custom
|
||||||
Requirements::javascript(THIRDPARTY_DIR . '/behaviour.js');
|
* validation
|
||||||
|
*/
|
||||||
|
public function init() {
|
||||||
|
// block prototype validation
|
||||||
|
Validator::set_javascript_validation_handler('none');
|
||||||
|
|
||||||
|
// load the jquery
|
||||||
|
Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
|
||||||
|
Requirements::javascript(THIRDPARTY_DIR . '/jquery/plugins/validate/jquery.validate.min.js');
|
||||||
|
|
||||||
parent::init();
|
parent::init();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -241,10 +250,10 @@ class UserDefinedForm_Controller extends Page_Controller {
|
|||||||
* @return Array
|
* @return Array
|
||||||
*/
|
*/
|
||||||
public function index() {
|
public function index() {
|
||||||
if($this->Content && $this->Form()) {
|
if($this->Content && $form = $this->Form()) {
|
||||||
$hasLocation = stristr($this->Content, '$UserDefinedForm');
|
$hasLocation = stristr($this->Content, '$UserDefinedForm');
|
||||||
if($hasLocation) {
|
if($hasLocation) {
|
||||||
$content = str_ireplace('$UserDefinedForm', $this->Form()->forTemplate(), $this->Content);
|
$content = str_ireplace('$UserDefinedForm', $form->forTemplate(), $this->Content);
|
||||||
return array(
|
return array(
|
||||||
'Content' => $content,
|
'Content' => $content,
|
||||||
'Form' => ""
|
'Form' => ""
|
||||||
@ -268,31 +277,45 @@ class UserDefinedForm_Controller extends Page_Controller {
|
|||||||
*
|
*
|
||||||
* @return Form
|
* @return Form
|
||||||
*/
|
*/
|
||||||
function Form() {
|
public function Form() {
|
||||||
// Build fields
|
|
||||||
$fields = new FieldSet();
|
$fields = new FieldSet();
|
||||||
$required = array();
|
$fieldValidation = array();
|
||||||
|
$fieldValidationRules = array();
|
||||||
|
|
||||||
if(!$this->SubmitButtonText) {
|
$this->SubmitButtonText = ($this->SubmitButtonText) ? $this->SubmitButtonText : _t('UserDefinedForm.SUBMITBUTTON', 'Submit');
|
||||||
$this->SubmitButtonText = _t('UserDefinedForm.SUBMITBUTTON', 'Submit');
|
|
||||||
}
|
if($this->Fields()) {
|
||||||
foreach($this->Fields() as $field) {
|
foreach($this->Fields() as $field) {
|
||||||
$fieldToAdd = $field->getFormField();
|
$fieldToAdd = $field->getFormField();
|
||||||
if($field->CustomErrorMessage) {
|
$fieldValidationOptions = array();
|
||||||
$fieldToAdd->setCustomValidationMessage($field->CustomErrorMessage);
|
|
||||||
}
|
|
||||||
|
|
||||||
$fields->push($fieldToAdd);
|
// Set the Error Messages
|
||||||
|
$errorMessage = sprintf(_t('Form.FIELDISREQUIRED').'.', strip_tags("'". ($field->Title ? $field->Title : $field->Name) . "'"));
|
||||||
|
$errorMessage = ($field->CustomErrorMessage) ? $field->CustomErrorMessage : $errorMessage;
|
||||||
|
$fieldToAdd->setCustomValidationMessage($errorMessage);
|
||||||
|
|
||||||
|
// Is this field required
|
||||||
if($field->Required) {
|
if($field->Required) {
|
||||||
$required[] = $field->Name;
|
$fieldValidation[$field->Name] = $errorMessage;
|
||||||
}
|
$fieldValidationOptions['required'] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!isset($_SERVER['HTTP_REFERER'])) {
|
// Add field to the form
|
||||||
$_SERVER['HTTP_REFERER'] = "";
|
$fields->push($fieldToAdd);
|
||||||
|
|
||||||
|
// Ask our form field for some more information on hour it should be validated
|
||||||
|
$fieldValidationOptions = array_merge($fieldValidationOptions, $field->getValidation());
|
||||||
|
|
||||||
|
// Check if we have need to update the global validation
|
||||||
|
if($fieldValidationOptions) {
|
||||||
|
$fieldValidationRules[$field->Name] = $fieldValidationOptions;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
|
$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
|
||||||
|
|
||||||
|
// Keep track of the referer
|
||||||
$fields->push( new HiddenField( "Referrer", "", $referer ) );
|
$fields->push( new HiddenField( "Referrer", "", $referer ) );
|
||||||
|
|
||||||
// Build actions
|
// Build actions
|
||||||
@ -304,13 +327,54 @@ class UserDefinedForm_Controller extends Page_Controller {
|
|||||||
if($this->ShowClearButton) {
|
if($this->ShowClearButton) {
|
||||||
$actions->push(new ResetFormAction("clearForm"));
|
$actions->push(new ResetFormAction("clearForm"));
|
||||||
}
|
}
|
||||||
|
// return the form
|
||||||
$form = new Form( $this, "Form", $fields, $actions, new RequiredFields($required));
|
$form = new Form( $this, "Form", $fields, $actions, new RequiredFields(array_keys($fieldValidation)));
|
||||||
$form->loadDataFrom($this->failover);
|
$form->loadDataFrom($this->failover);
|
||||||
|
|
||||||
|
$FormName = $form->FormName();
|
||||||
|
|
||||||
|
$rules = $this->array2json($fieldValidationRules);
|
||||||
|
$messages = $this->array2json($fieldValidation);
|
||||||
|
|
||||||
|
// set the custom script for this form
|
||||||
|
Requirements::customScript(<<<JS
|
||||||
|
(function($) {
|
||||||
|
$(document).ready(function() {
|
||||||
|
$("#$FormName").validate({
|
||||||
|
errorClass: "required",
|
||||||
|
messages:
|
||||||
|
$messages
|
||||||
|
,
|
||||||
|
|
||||||
|
rules:
|
||||||
|
$rules
|
||||||
|
});
|
||||||
|
});
|
||||||
|
})(jQuery);
|
||||||
|
JS
|
||||||
|
);
|
||||||
|
|
||||||
return $form;
|
return $form;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a PHP array to a JSON string. We cannot use {@link Convert::array2json}
|
||||||
|
* as it escapes our values with "" which appears to break the validate plugin
|
||||||
|
*
|
||||||
|
* @param Array array to convert
|
||||||
|
* @return JSON
|
||||||
|
*/
|
||||||
|
protected function array2json($array) {
|
||||||
|
foreach($array as $key => $value)
|
||||||
|
if(is_array( $value )) {
|
||||||
|
$result[] = "$key:" . $this->array2json($value);
|
||||||
|
} else {
|
||||||
|
$value = (is_bool($value)) ? $value : "\"$value\"";
|
||||||
|
$result[] = "$key:$value \n";
|
||||||
|
}
|
||||||
|
return (isset($result)) ? "{\n".implode( ', ', $result ) ."} \n": '';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Process the form that is submitted through the site
|
* Process the form that is submitted through the site
|
||||||
*
|
*
|
||||||
|
@ -7,7 +7,9 @@
|
|||||||
* @package userforms
|
* @package userforms
|
||||||
*/
|
*/
|
||||||
class EditableDateField extends EditableFormField {
|
class EditableDateField extends EditableFormField {
|
||||||
|
|
||||||
static $singular_name = 'Date field';
|
static $singular_name = 'Date field';
|
||||||
|
|
||||||
static $plural_name = 'Date fields';
|
static $plural_name = 'Date fields';
|
||||||
|
|
||||||
function DefaultField() {
|
function DefaultField() {
|
||||||
@ -30,8 +32,28 @@ class EditableDateField extends EditableFormField {
|
|||||||
parent::populateFromPostData( $data );
|
parent::populateFromPostData( $data );
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFormField() {
|
/**
|
||||||
return new CalendarDateField( $this->Name, $this->Title, $this->getField('Default') );
|
* Return the form field.
|
||||||
|
*
|
||||||
|
* @todo Make a jQuery safe form field. The current CalendarDropDown
|
||||||
|
* breaks on the front end.
|
||||||
|
*/
|
||||||
|
public function getFormField() {
|
||||||
|
return new TextField( $this->Name, $this->Title, $this->Default);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the validation information related to this field. This is
|
||||||
|
* interrupted as a JSON object for validate plugin and used in the
|
||||||
|
* PHP.
|
||||||
|
*
|
||||||
|
* @see http://docs.jquery.com/Plugins/Validation/Methods
|
||||||
|
* @return Array
|
||||||
|
*/
|
||||||
|
public function getValidation() {
|
||||||
|
return array(
|
||||||
|
'date' => true
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
@ -17,7 +17,6 @@ class EditableEmailField extends EditableFormField {
|
|||||||
|
|
||||||
function populateFromPostData( $data ) {
|
function populateFromPostData( $data ) {
|
||||||
$this->SendCopy = !empty($data['SendCopy']) ? "1" : "0";
|
$this->SendCopy = !empty($data['SendCopy']) ? "1" : "0";
|
||||||
|
|
||||||
parent::populateFromPostData($data);
|
parent::populateFromPostData($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,5 +49,19 @@ class EditableEmailField extends EditableFormField {
|
|||||||
|
|
||||||
return '<div class="field text"><label class="left">'._t('EditableEmailField.DEFAULTTEXT', 'Default Text').' </label><input class="defaultText" name="Fields['.Convert::raw2att( $this->ID ).'][Default]" type="text" value="'.Convert::raw2att( $this->getField('Default') ).'"'.$disabled.' /></div>';
|
return '<div class="field text"><label class="left">'._t('EditableEmailField.DEFAULTTEXT', 'Default Text').' </label><input class="defaultText" name="Fields['.Convert::raw2att( $this->ID ).'][Default]" type="text" value="'.Convert::raw2att( $this->getField('Default') ).'"'.$disabled.' /></div>';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the validation information related to this field. This is
|
||||||
|
* interrupted as a JSON object for validate plugin and used in the
|
||||||
|
* PHP.
|
||||||
|
*
|
||||||
|
* @see http://docs.jquery.com/Plugins/Validation/Methods
|
||||||
|
* @return Array
|
||||||
|
*/
|
||||||
|
public function getValidation() {
|
||||||
|
return array(
|
||||||
|
'email' => true
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
@ -177,13 +177,23 @@ class EditableFormField extends DataObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a FormField to appear on the front end
|
* Return a FormField to appear on the front end. Implement on
|
||||||
|
* your subclass
|
||||||
|
*
|
||||||
|
* @return FormField
|
||||||
*/
|
*/
|
||||||
function getFormField() {
|
public function getFormField() {
|
||||||
|
user_error("Please implement a getFormField() on your EditableFormClass "+ $this->ClassName, E_USER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFilterField() {
|
/**
|
||||||
|
* Return the form field to appear on the filter form
|
||||||
|
* in the cms view
|
||||||
|
*
|
||||||
|
* @return FormField
|
||||||
|
*/
|
||||||
|
public function getFilterField() {
|
||||||
|
user_error("Please implement a getFilterField() on your EditableFormClass "+ $this->ClassName, E_USER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -242,5 +252,17 @@ class EditableFormField extends DataObject {
|
|||||||
function CustomParameter() {
|
function CustomParameter() {
|
||||||
return $this->CustomParameter;
|
return $this->CustomParameter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the validation information related to this field. This is
|
||||||
|
* interrupted as a JSON object for validate plugin and used in the
|
||||||
|
* PHP.
|
||||||
|
*
|
||||||
|
* @see http://docs.jquery.com/Plugins/Validation/Methods
|
||||||
|
* @return Array
|
||||||
|
*/
|
||||||
|
public function getValidation() {
|
||||||
|
return array();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
@ -6,22 +6,42 @@
|
|||||||
*/
|
*/
|
||||||
class EditableFormHeading extends EditableFormField {
|
class EditableFormHeading extends EditableFormField {
|
||||||
|
|
||||||
|
static $db = array(
|
||||||
|
'Level' => 'Varchar(1)'
|
||||||
|
);
|
||||||
|
|
||||||
static $singular_name = 'Form heading';
|
static $singular_name = 'Form heading';
|
||||||
|
|
||||||
static $plural_name = 'Form headings';
|
static $plural_name = 'Form headings';
|
||||||
|
|
||||||
|
function populateFromPostData($data) {
|
||||||
|
$this->Level = (isset($data['Level'])) ? $data['Level'] : 2;
|
||||||
|
|
||||||
|
parent::populateFromPostData($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
function ExtraOptions() {
|
||||||
|
$levels = array('1','2','3','4','5','6');
|
||||||
|
$default = ($this->Level) ? $this->Level : 2;
|
||||||
|
$extraFields = new FieldSet(
|
||||||
|
new DropdownField("Fields[$this->ID][Level]", _t('EditableFormHeading.LEVEL', 'Select Heading Level'), $levels, $default)
|
||||||
|
);
|
||||||
|
|
||||||
|
if($this->readonly) {
|
||||||
|
$extraFields = $extraFields->makeReadonly();
|
||||||
|
}
|
||||||
|
return $extraFields;
|
||||||
|
}
|
||||||
|
|
||||||
function getFormField() {
|
function getFormField() {
|
||||||
$labelField = new LabelField('FormHeadingLabel',$this->Title);
|
$labelField = new HeaderField('FormHeadingLabel',$this->Title, $this->Level);
|
||||||
$labelField->addExtraClass('FormHeading');
|
$labelField->addExtraClass('FormHeading');
|
||||||
|
|
||||||
return $labelField;
|
return $labelField;
|
||||||
}
|
}
|
||||||
|
|
||||||
function showInReports() {
|
function showInReports() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function showExtraOptions() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
?>
|
?>
|
@ -94,5 +94,21 @@ class EditableTextField extends EditableFormField {
|
|||||||
return '<div class="field text"><label class="left">'._t('EditableTextField.DEFAULTTEXT', 'Default Text').' </label> <textarea class="defaultText" name="Fields['.Convert::raw2att( $this->ID ).'][Default]"'.$disabled.'>'.Convert::raw2att( $this->getField('Default') ).'</textarea></div>';
|
return '<div class="field text"><label class="left">'._t('EditableTextField.DEFAULTTEXT', 'Default Text').' </label> <textarea class="defaultText" name="Fields['.Convert::raw2att( $this->ID ).'][Default]"'.$disabled.'>'.Convert::raw2att( $this->getField('Default') ).'</textarea></div>';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the validation information related to this field. This is
|
||||||
|
* interrupted as a JSON object for validate plugin and used in the
|
||||||
|
* PHP.
|
||||||
|
*
|
||||||
|
* @see http://docs.jquery.com/Plugins/Validation/Methods
|
||||||
|
* @return Array
|
||||||
|
*/
|
||||||
|
public function getValidation() {
|
||||||
|
$options = array();
|
||||||
|
if($this->MinLength) $options['minlength'] = $this->MinLength;
|
||||||
|
if($this->MaxLength) $options['maxlength'] = $this->MinLength;
|
||||||
|
|
||||||
|
return $options;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
Loading…
x
Reference in New Issue
Block a user