Version: current

     1 <?php
     2 /**
     3  * Specify special required fields to be executed as part of form validation
     4  * @package forms
     5  * @subpackage validators
     6  */
     7 class CustomRequiredFields extends RequiredFields{
     8     protected $required;
     9
    10     /**
    11      * Pass each field to be validated as a seperate argument
    12      * @param $required array The list of required fields
    13      */
    14     function __construct($required) {
    15         $this->required = $required;
    16     }
    17
    18     /**
    19      * Creates the client side validation from form fields
    20      * which is generated at the header of each page
    21      */
    22     function javascript() {
    23         $code = '';
    24         $fields = $this->form->Fields();
    25         foreach($fields as $field){
    26             //if the field type has some special specific specification for validation of itself
    27             $valid = $field->jsValidation();
    28             if($valid){
    29                 $code .= $valid;
    30             }
    31         }
    32         if(is_array($this->required)){
    33
    34             foreach($this->required as $field) {
    35                 if(is_array($field)) {
    36                     if (isset($field['js']))
    37                         $code .= $field['js'] . "\n";
    38                 }else if($fields->dataFieldByName($field)) {
    39                     //$code .= "                        require('$field');\n";
    40                     //Tabs for output tabbing :-)
    41                     $code .= <<<JS
    42                         if(typeof fromAnOnBlur != 'undefined'){\n
    43                             if(fromAnOnBlur.name == '$field')\n
    44                                 require(fromAnOnBlur);\n
    45                         }else{
    46                             require('$field');
    47                         }
    48 JS;
    49
    50                 }
    51             }
    52         }else{
    53             USER_ERROR("CustomRequiredFields::requiredfields is not set / not an array",E_USER_WARNING);
    54         }
    55         return $code;
    56     }
    57
    58     /**
    59      * Creates the server side validation from form fields
    60      * which is executed on form submission
    61      */
    62     function php($data) {
    63         $fields = $this->form->Fields();
    64         $valid = true;
    65         foreach($fields as $field) {
    66             $valid = ($field->validate($this) && $valid);
    67         }
    68         if($this->required){
    69             foreach($this->required as $key => $fieldName) {
    70                 if(is_string($fieldName)) $formField = $fields->dataFieldByName($fieldName);
    71                 if(is_array($fieldName) && isset($fieldName['php'])){
    72                     eval($fieldName['php']);
    73                 }else if($formField) {
    74                     // if an error is found, the form is returned.
    75                     if(!$data[$fieldName] || preg_match('/^\s*$/', $data[$fieldName])) {
    76                         $this->validationError(
    77                             $fieldName,
    78                                                         sprintf(_t('Form.FIELDISREQUIRED', "%s is required."),
    79                                                                 $formField->Title()),
    80                             "required"
    81                         );
    82                         return false;
    83                     }
    84                 }
    85             }
    86         }
    87         return $valid;
    88     }
    89
    90     /**
    91      * allows you too add more required fields to this object after construction.
    92      */
    93     function appendRequiredFields($requiredFields){
    94         $this->required = array_merge($this->required,$requiredFields->getRequired());
    95     }
    96 }
    97
    98 ?>

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -