Webylon 3.1 API Docs
  • Package
  • Class
  • Tree
  • Deprecated
  • Download
Version: current
  • 3.2
  • 3.1

Packages

  • auth
  • Booking
  • cart
    • shipping
    • steppedcheckout
  • Catalog
  • cms
    • assets
    • batchaction
    • batchactions
    • bulkloading
    • comments
    • content
    • core
    • export
    • newsletter
    • publishers
    • reports
    • security
    • tasks
  • Dashboard
  • DataObjectManager
  • event
  • faq
  • forms
    • actions
    • core
    • fields-basic
    • fields-dataless
    • fields-datetime
    • fields-files
    • fields-formatted
    • fields-formattedinput
    • fields-relational
    • fields-structural
    • transformations
    • validators
  • googlesitemaps
  • guestbook
  • installer
  • newsletter
  • None
  • photo
    • gallery
  • PHP
  • polls
  • recaptcha
  • sapphire
    • api
    • bulkloading
    • control
    • core
    • cron
    • dev
    • email
    • fields-formattedinput
    • filesystem
    • formatters
    • forms
    • i18n
    • integration
    • misc
    • model
    • parsers
    • search
    • security
    • tasks
    • testing
    • tools
    • validation
    • view
    • widgets
  • seo
    • open
      • graph
  • sfDateTimePlugin
  • spamprotection
  • stealth
    • captha
  • subsites
  • userform
    • pagetypes
  • userforms
  • webylon
  • widgets

Classes

  • CustomRequiredFields
  • RequiredFields
  • Validator
  1 <?php
  2 /**
  3  * Required Fields allows you to set which fields
  4  * need to be present before submitting the form
  5  * Submit an array of arguments or each field as a
  6  * seperate argument. Validation is performed on a name by
  7  * name basis.
  8  *
  9  * @package forms
 10  * @subpackage validators
 11  */
 12 class RequiredFields extends Validator {
 13     
 14     protected $required;
 15     protected $useLabels = true;
 16 
 17     /**
 18      * Pass each field to be validated as a seperate argument
 19      * to the constructor of this object. (an array of elements are ok)
 20      */
 21     function __construct() {
 22         $Required = func_get_args();
 23         if( isset($Required[0]) && is_array( $Required[0] ) )
 24             $Required = $Required[0];
 25         $this->required = $Required;
 26 
 27         parent::__construct();
 28     }
 29 
 30     public function useLabels($flag) {
 31         $this->useLabels = $flag;
 32     }
 33 
 34     /**
 35      * Clears all the validation from this object.
 36      */
 37     public function removeValidation(){
 38         $this->required = null;
 39     }
 40 
 41     /**
 42      * Debug helper
 43      */
 44     function debug() {
 45      if(!is_array($this->required)) return false;
 46 
 47      $result = "<ul>";
 48      foreach( $this->required as $name ){
 49         $result .= "<li>$name</li>";
 50      }
 51 
 52      $result .= "</ul>";
 53      return $result;
 54     }
 55 
 56     function javascript() {
 57         $js = "";
 58         $fields = $this->form->Fields();
 59         $dataFields = $this->form->Fields()->dataFields();
 60         if($dataFields) {
 61             foreach($dataFields as $field) {
 62                 // if the field type has some special specific specification for validation of itself
 63                 $validationFunc = $field->jsValidation();
 64                 if($validationFunc) $js .= $validationFunc . "\n";
 65             }
 66         }
 67         $useLabels = $this->useLabels ? 'true' : 'false';
 68 
 69         if($this->required) {
 70             foreach($this->required as $field) {
 71                 if($fields->dataFieldByName($field)) {
 72                     //$js .= "\t\t\t\t\trequire('$field', false, $useLabels);\n";
 73                     $js .= <<<JS
 74                         if(typeof fromAnOnBlur != 'undefined'){
 75                             if(fromAnOnBlur.name == '$field')
 76                                 require(fromAnOnBlur);
 77                         }else{
 78                             require('$field');
 79                         }
 80 JS;
 81                 }
 82             }
 83         }
 84 
 85         return $js;
 86     }
 87 
 88 
 89     /**
 90     * Allows validation of fields via specification of a php function for validation which is executed after
 91     * the form is submitted
 92     */
 93     function php($data) {
 94         $valid = true;
 95 
 96         $fields = $this->form->Fields();
 97         foreach($fields as $field) {
 98             $valid = ($field->validate($this) && $valid);
 99         }
100         if($this->required) {
101             foreach($this->required as $fieldName) { 
102                 $formField = $fields->dataFieldByName($fieldName); 
103                 if($formField && !$data[$fieldName]) {
104                     $errorMessage = sprintf(_t('Form.FIELDISREQUIRED').'.', strip_tags('"' . ($formField->Title() ? $formField->Title() : $fieldName) . '"'));
105                     if($msg = $formField->getCustomValidationMessage()) {
106                         $errorMessage = $msg;
107                     }
108                     $this->validationError(
109                         $fieldName,
110                         $errorMessage,
111                         "required"
112                     );
113                     $valid = false;
114                 }
115             }
116 
117         }
118 
119         return $valid;
120     }
121 
122     /**
123      * Add's a single required field to requiredfields stack
124      */
125     function addRequiredField( $field ) {
126         $this->required[] = $field;
127     }
128 
129     function removeRequiredField($field) {
130         for($i=0; $i<count($this->required); $i++) {
131             if($field == $this->required[$i]) {
132                 unset($this->required[$i]);
133             }
134         }
135     }
136 
137     /**
138      * allows you too add more required fields to this object after construction.
139      */
140     function appendRequiredFields($requiredFields){
141         $this->required = array_merge($this->required,$requiredFields->getRequired());
142     }
143 
144     /**
145      * Returns true if the named field is "required".
146      * Used by FormField to return a value for FormField::Required(), to do things like show *s on the form template.
147      */
148     function fieldIsRequired($fieldName) {
149         return (is_array($this->required))  ? in_array($fieldName, $this->required) : false;
150     }
151 
152     /**
153      * getter function for append
154      */
155     function getRequired(){
156         return $this->required;
157     }
158 }
159 
160 ?>
[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. -
Webylon 3.1 API Docs API documentation generated by ApiGen 2.8.0