public function setValidator($validator = null)
 {
     if (count($this->getValidators()) === 0 && $validator instanceof FormHandler\Validator\FunctionCallable && is_array($validator->getCallable())) {
         $callable = $validator->getCallable();
         //detect if it is an optional validator
         if ($callable[0] instanceof Validator && substr($callable[1], 0, 1) !== '_') {
             parent::setValidator(new \FormHandler\Validator\NotEmpty());
         }
     }
     return parent::setValidator(FormHandler::parseValidator($validator));
 }
 /**
  * FormHandler::hiddenField()
  *
  * Create a hidden field
  *
  * @param string $name The name of the field
  * @param string $value The value of the field
  * @param string $validator The validator which should be used to validate the value of the field
  * @param string $extra CSS, Javascript or other which are inserted into the HTML tag
  * @return \FormHandler\Field\Hidden
  * @author Teye Heimans
  * @deprecated Use \FormHandler\Field\Hidden::set() instead
  */
 public function hiddenField($name, $value = null, $validator = null, $extra = null)
 {
     $fld = \FormHandler\Field\Hidden::set($this, $name);
     $fld->setValidator(self::parseValidator($validator, $fld))->setExtra($extra);
     // only set the hidden field value if there is not a value in the $_POST array
     if (!is_null($value)) {
         $fld->setValue($value);
     }
     return $fld;
 }
Example #3
0
 /**
  * Constructor
  *
  * Create a new file field
  *
  * @param FormHandler $form The form where this field is located on
  * @param string $name The name of the field
  * @return \FormHandler\Field\File
  * @author Teye Heimans
  * @author Marien den Besten
  */
 public function __construct(FormHandler $form, $name)
 {
     static $bSetJS = false;
     // needed javascript included yet ?
     if (!$bSetJS) {
         // include the needed javascript
         $bSetJS = true;
         $form->_setJS(\FormHandler\Configuration::get('fhtml_dir') . "js/upload.js", true);
     }
     parent::__construct($form, $name);
     $form->setEncoding(FormHandler::ENCODING_MULTIPART);
     //upload fields are required by default
     $this->setRequired(true);
     $this->accept = array();
     $this->drop_zone_enabled = false;
     $this->drop_zone_language = array('drop_file' => 'Drop your file here', 'too_large' => 'Given file is too large (Given: %given%, allowed: %allowed%)');
     $this->async = !is_null(filter_input(INPUT_POST, $name . '_submit'));
     $this->button_upload = new Button($form, $name . '_button');
     $this->button_upload->setCaption('Choose File')->setExtra('style="display:none;" class="upload"');
     $this->button_edit = new \FormHandler\Button\Submit($form, $name . '_change');
     $this->button_edit->setCaption('Change file')->setExtra('class="upload"')->onClick(function (FormHandler $form) use($name) {
         $form->setValidationDisabled(true);
         $form->setIsCorrect(false);
         $form->getField($name)->updateState(\FormHandler\Field\File::STATE_EMPTY, null, null);
     });
     //read all open uploads
     $status = $this->readOpenUploads();
     //by default generate a new token
     $generated_token = $this->generateToken();
     //create field
     $this->token = new \FormHandler\Field\Hidden($form, $name . '_token');
     $this->token->setDefaultValue($generated_token);
     $this->filename = new \FormHandler\Field\Hidden($form, $name . '_filename');
     $this->filename->setDefaultValue('');
     $this->state = new \FormHandler\Field\Hidden($form, $name . '_state');
     $this->state->setDefaultValue(self::STATE_EMPTY);
     //process token
     $unsecure_token = $this->token->getValue();
     $token = $this->form_object->isPosted() && !array_key_exists($unsecure_token, $status) ? $generated_token : $unsecure_token;
     $this->token->setValue($token);
     $data = $this->readState();
     try {
         $result = $this->processUpload();
     } catch (Exception $e) {
         $result = false;
         $data = $this->updateState($e->getCode(), null, null);
     }
     if ($result !== false && is_array($result)) {
         $data = $this->updateState(self::STATE_UPLOADED, $result[0], $result[1]);
     }
     if ($this->async) {
         $ajax = array('state' => $data['state'], 'name' => $data['last_processed']);
         //there was an javascript file submit, return state
         //do not send json header because IE will behave strange
         echo json_encode($ajax);
         exit;
     }
     //incorporate max file size
     if (!$form->fieldExists('MAX_FILE_SIZE')) {
         \FormHandler\Field\Hidden::set($form, 'MAX_FILE_SIZE')->setValue($this->getMaxUploadSize(), true)->hideFromOnCorrect();
     }
     return $this;
 }