/** * add validation for Input element * @param \WinkForm\Input\Input $input * @param string|array $rules * @param string $message custom message to overwrite default * @throws \Exception */ public function addValidation(Input $input, $rules, $message = null) { $rules = $this->checkRules($rules); $name = $input->getName(); // create entry in validations array for the input if it doesn't yet exist if (!array_key_exists($name, $this->validations)) { $this->validations[$name] = array('data' => $input->getPosted(), 'rules' => $rules, 'message' => $message); } else { // merge rules foreach ($rules as $rule) { if (!in_array($rule, $this->validations[$name]['rules'])) { $this->validations[$name]['rules'][] = $rule; } } // append message if (!empty($message)) { $this->validations[$name]['message'] = trim($this->validations[$name]['message'] . ' ' . $message); } } }
/** * construct DateInput * @param string $name * @param mixed $value */ function __construct($name, $value = null) { // set date format before construction to be able to validate $value $config = Config::all(); $this->setDateFormat($config['date_format']); parent::__construct($name, $value); // create TextInput object with all same properties as this DateInput object $this->text = new TextInput($this->name, $value); $this->text->setWidth(80)->setMaxLength(10); $this->attachObserver($this->text); // set the calendar image specified in the config file if it is a valid url to an image $this->setCalendarImage($config['calendar_image']); if (empty($this->calendarImage)) { // set default calendar image (base64 encoded, so no actual image file required to use this class) // It is of course better to use an actual image for performance reasons $this->calendarImage = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAACpklEQVRYheWXPWgU' . 'QRTHf5dLxIgiBEQtEgRR4gdiLShWojZ+gbYWthaijSCWgoWVBK1URJAjhYKKX6DEFEpADeYM+IGiSBJzaESNXvwai/cm+9yb3dsNF1L' . '4YGF+M/tm/vPmzcxuwTnHTFrTjI4O4JyjThS2AQeBlpxdb1e/Wanj1hHQAlQAB6zLMXiz8duUJqDZ1K0F2mPvfQZ+a7kTaAMKGQRUjd' . '8qJArWbwh4+I8S4KIqtk8JeKnlrkB70nMTeKHl84H2y35cm4QTsVl8AU4Bs5VvAb0ZZv8VOA60Kt8GemLvTI6VtgvKQB8wT3kUuJpBQ' . 'Bl4YPwqwLWkl9MEdCDreANZhjKSJ/WsPebXl+pncuActWvVDawGlgOHteMsOXDJ+B0K+JVC2zAkYLqeUmgbeqsAp/VFkGUqEG2rrBby' . '24ss7aSFBAwDR3MOltU2xAWEkrCYUN8Iq5lwKAJJtgjYqeV+4DWwQ/k+MAZsVe4BvgOble8hu6jWAklYJhyB9URJdBLYaPgIsMvwfmC' . 'P4QPaRy+xJJxqqH3HWTnR8izBIDJLkHN+yPAAcgR7fowct5778wpwwFLgmPJ15ILZolxE1tjzODBi+ANyl3geA17lFbAA2K38Cbk+9y' . 'lXkbvB8xskQp6fxNoHgbt5BAD8IjpEvmnZsm2vAj8NTyg7w0FLEtAEPAXWKH9EwujZh9jze+CH4WEV0Kk8mlfAHyQHSsoXkIupW/ksc' . 'EfrHXACORe6DJ8BnicNXE8AwFxgpZaXIB8YnjuQ+36F8mIkTyxnsjQB48AzLb9Fst7zO2TbeR5RAZanLMAnzgDRGnrLy0l9pwooAvOR' . 'PPAO/os2D8e/nl1ovJCAZUShbLS1pQloNXULp0mAtzkhAVdI2a8Ntke+UPjv/47/Ajh1PTcN0TPzAAAAAElFTkSuQmCC'; } }
/** * construct AddressInput object * @param string $name * @param string $value */ function __construct($name, $value = null) { parent::__construct($name, $value); $translator = Translator::getInstance(); // create the text inputs // NOTE: values must be the same as the titles for the jquery script $postcode = $translator->get('inputs.postal-code'); $this->postcode = Form::text($name . '-' . $postcode)->setPlaceholder(str_replace('-', ' ', $postcode))->setTitle(str_replace('-', ' ', $postcode))->setWidth(100); $hnr = $translator->get('inputs.house-number'); $this->houseNumber = Form::text($name . '-' . $hnr)->setTitle(str_replace('-', ' ', $hnr))->setPlaceholder(str_replace('-', ' ', $hnr))->setWidth(50); $ext = $translator->get('inputs.extension'); $this->houseNumberExtension = Form::text($name . '-' . $ext)->setPlaceholder(str_replace('-', ' ', $ext))->setTitle(str_replace('-', ' ', $ext))->setWidth(150); $this->attachObserver($this->postcode); $this->attachObserver($this->houseNumber); $this->attachObserver($this->houseNumberExtension); // set the global placeholder style that will get copied down $this->addClass('address'); }
/** * Invalidate input field (and let this form know it is invalid) * @param Input\Input $input * @param string $invalidation */ public function invalidate(Input\Input $input, $invalidation) { $this->isValid = false; $input->addInvalidation($invalidation); }
/** * validate a single input object using default validations or * custom validations assigned to the object or to the form * @param \WinkForm\Input\Input $input */ protected function validateInput(\WinkForm\Input\Input $input) { // skip non-required fields that are not posted if (!$input->isPosted() && !$input->isRequired()) { return; } // always validate that posted value(s) of checkbox, radio or dropdown are in the array of values of the Input element $values = $input->getValues(); if (!empty($values)) { $this->validator->addValidation($input, 'all_in:' . implode(',', $values)); } // always validate the date if ($input instanceof DateInput) { $this->validator->addValidation($input, 'date_format:' . $input->getDateFormat()); } elseif ($input instanceof DateRangeInput) { $this->validator->addValidation($input->getDateFrom(), 'date_format:' . $input->getDateFrom()->getDateFormat()); $this->validator->addValidation($input->getDateTo(), 'date_format:' . $input->getDateTo()->getDateFormat()); } // validations added to the Input element if ($input->hasValidations()) { $this->validator->addValidation($input, $input->getValidations()); } // place found invalidations after the input element if (!$this->validator->isValid()) { $this->invalidate($input, implode("<br/>\n", $this->validator->getAttributeErrors($input->getName()))); } // clear the validator for the next input $this->validator->reset(); }
/** * construct InputButton * @param string $name * @param mixed $value */ function __construct($name, $value = null) { parent::__construct($name, $value); // always set btn class $this->addClass('btn'); }