requireField('FieldName') is the php equivalent)
*
* An example for creating required fields only if payment type is CreditCard:
*
*
* 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);
* }',
* )
* )
* );
*
*
* And example for confirming mobile number and email address:
*
*
* $js = << $js, 'php' => 'return true;'), array('js' => $js2, 'php'=>'return true;')));
*
*
* @package forms
* @subpackage validators
*/
class CustomRequiredFields extends RequiredFields{
protected $required;
/**
* Pass each field to be validated as a seperate argument
* @param $required array The list of required fields
*/
function __construct($required) {
$this->required = $required;
}
/**
* 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){
foreach($this->required as $key => $fieldName) {
if(is_string($fieldName)) $formField = $fields->dataFieldByName($fieldName);
if(is_array($fieldName) && isset($fieldName['php'])){
eval($fieldName['php']);
}else if($formField) {
// if an error is found, the form is returned.
if(!strlen($data[$fieldName]) || preg_match('/^\s*$/', $data[$fieldName])) {
$this->validationError(
$fieldName,
sprintf(_t('Form.FIELDISREQUIRED', "%s is required."),
$formField->Title()),
"required"
);
return false;
}
}
}
}
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());
}
}