2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-01-09 05:18:36 +01:00
|
|
|
/**
|
2010-07-11 12:44:59 +02:00
|
|
|
* CustomRequiredFields allow you to create your own validation on forms, while still having the ability to have required fields (as used in [RequiredFields](http://api.silverstripe.org/current/sapphire/form/RequiredFields.html)).
|
|
|
|
*
|
|
|
|
* The constructor of CustomRequiredFields takes an array. Each array element is one of two things - either the name of a field that is required, or an array containing two items, 'js' and 'php'. These items are functions called to validate in javascript or php respectively.
|
|
|
|
*
|
|
|
|
* Some useful javascript:
|
|
|
|
* 1. _CURRENT_FORM is the current form
|
|
|
|
* 2. _CURRENT_FORM.elements is an array of the fields
|
|
|
|
* 3. validationError(element, message, type) will create a validation error
|
|
|
|
* 4. clearErrorMessage(element) will clear the validation error
|
|
|
|
* 5. require('FieldName') create a required field ($this->requireField('FieldName') is the php equivalent)
|
|
|
|
*
|
|
|
|
* An example for creating required fields only if payment type is CreditCard:
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* new CustomRequiredFields(
|
|
|
|
* array(
|
|
|
|
* "PaymentMethod",
|
|
|
|
* array(
|
|
|
|
* "js" => "
|
|
|
|
* for( var i = 0; i <= this.elements.PaymentMethod.length -1; i++){
|
|
|
|
* if(this.elements.PaymentMethod[i].value == 'CC' && this.elements.PaymentMethod[i].checked == true){
|
|
|
|
* require('CardHolderName');
|
|
|
|
* require('CreditCardNumber');
|
|
|
|
* require('DateExpiry');
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* ",
|
|
|
|
* "php" => 'if($data[PaymentMethod] == "CC") {
|
|
|
|
* $this->requireField($field,"$field is required","required");
|
|
|
|
* $this->requireField("CardHolderName", $data);
|
|
|
|
* $this->requireField("CreditCardNumber", $data);
|
|
|
|
* $this->requireField("DateExpiry", $data);
|
|
|
|
* }',
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* );
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* And example for confirming mobile number and email address:
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* $js = <<<JS
|
|
|
|
* if(_CURRENT_FORM.elements["MobileNumberConfirm"].value == _CURRENT_FORM.elements["MobileNumber"].value) {
|
|
|
|
* clearErrorMessage(_CURRENT_FORM.elements["MobileNumberConfirm"].parentNode);
|
|
|
|
* } else {
|
|
|
|
* validationError(_CURRENT_FORM.elements["MobileNumberConfirm"], "Mobile numbers do not match", "validation");
|
|
|
|
* }
|
|
|
|
* JS;
|
|
|
|
*
|
|
|
|
* $js2 = <<<JS2
|
|
|
|
* if(_CURRENT_FORM.elements["EmailConfirm"].value == _CURRENT_FORM.elements["Email"].value) {
|
|
|
|
* clearErrorMessage(_CURRENT_FORM.elements["EmailConfirm"].parentNode);
|
|
|
|
* } else {
|
|
|
|
* validationError(_CURRENT_FORM.elements["EmailConfirm"], "Email addresses do not match", "validation");
|
|
|
|
* }
|
|
|
|
* JS2;
|
|
|
|
*
|
|
|
|
* //create validator
|
|
|
|
* $validator=new CustomRequiredFields(array('FirstName', 'Surname', 'Email', 'MobileNumber', array('js' => $js, 'php' => 'return true;'), array('js' => $js2, 'php'=>'return true;')));
|
|
|
|
* </code>
|
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage validators
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class CustomRequiredFields extends RequiredFields{
|
|
|
|
protected $required;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pass each field to be validated as a seperate argument
|
2008-01-10 01:34:18 +01:00
|
|
|
* @param $required array The list of required fields
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function __construct($required) {
|
|
|
|
$this->required = $required;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the client side validation from form fields
|
|
|
|
* which is generated at the header of each page
|
|
|
|
*/
|
|
|
|
function javascript() {
|
2007-12-17 22:39:59 +01:00
|
|
|
$code = '';
|
2007-07-19 12:40:28 +02:00
|
|
|
$fields = $this->form->Fields();
|
|
|
|
foreach($fields as $field){
|
|
|
|
//if the field type has some special specific specification for validation of itself
|
|
|
|
$valid = $field->jsValidation();
|
|
|
|
if($valid){
|
|
|
|
$code .= $valid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(is_array($this->required)){
|
|
|
|
|
|
|
|
foreach($this->required as $field) {
|
2008-07-17 23:58:59 +02:00
|
|
|
if(is_array($field) && isset($field['js'])){
|
2007-10-04 23:58:10 +02:00
|
|
|
$code .= $field['js'] . "\n";
|
|
|
|
}else if($fields->dataFieldByName($field)) {
|
|
|
|
$code .= " require('$field');\n";
|
|
|
|
//Tabs for output tabbing :-)
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
USER_ERROR("CustomRequiredFields::requiredfields is not set / not an array",E_USER_WARNING);
|
|
|
|
}
|
|
|
|
return $code;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the server side validation from form fields
|
|
|
|
* which is executed on form submission
|
|
|
|
*/
|
|
|
|
function php($data) {
|
|
|
|
$fields = $this->form->Fields();
|
|
|
|
$valid = true;
|
|
|
|
foreach($fields as $field) {
|
|
|
|
$valid = ($field->validate($this) && $valid);
|
|
|
|
}
|
|
|
|
if($this->required){
|
2008-07-17 23:58:59 +02:00
|
|
|
foreach($this->required as $key => $fieldName) {
|
2009-08-25 03:51:56 +02:00
|
|
|
if(is_string($fieldName)) $formField = $fields->dataFieldByName($fieldName);
|
2008-07-17 23:58:59 +02:00
|
|
|
if(is_array($fieldName) && isset($fieldName['php'])){
|
|
|
|
eval($fieldName['php']);
|
|
|
|
}else if($formField) {
|
2007-10-04 23:58:10 +02:00
|
|
|
// if an error is found, the form is returned.
|
2010-07-26 08:25:59 +02:00
|
|
|
if(!strlen($data[$fieldName]) || preg_match('/^\s*$/', $data[$fieldName])) {
|
2007-10-25 04:47:45 +02:00
|
|
|
$this->validationError(
|
2008-07-17 23:58:59 +02:00
|
|
|
$fieldName,
|
2009-04-29 01:52:15 +02:00
|
|
|
sprintf(_t('Form.FIELDISREQUIRED', "%s is required."),
|
2008-07-17 23:58:59 +02:00
|
|
|
$formField->Title()),
|
2007-10-25 04:47:45 +02:00
|
|
|
"required"
|
|
|
|
);
|
2007-10-04 23:58:10 +02:00
|
|
|
return false;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* allows you too add more required fields to this object after construction.
|
|
|
|
*/
|
|
|
|
function appendRequiredFields($requiredFields){
|
|
|
|
$this->required = array_merge($this->required,$requiredFields->getRequired());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-17 23:58:59 +02:00
|
|
|
?>
|