Skip to content

Commit

Permalink
Added validation rules for optional items (Nette < 2.4).
Browse files Browse the repository at this point in the history
  • Loading branch information
radekdostal committed Oct 24, 2016
1 parent 64cfeb4 commit aa283d8
Show file tree
Hide file tree
Showing 10 changed files with 330 additions and 762 deletions.
6 changes: 6 additions & 0 deletions examples/DatePicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
//->addRule(Form::MIN, NULL, new DateTime('2016-09-01'))
//->addRule(Form::MAX, NULL, new DateTime('2016-09-15'))
//->addRule(Form::RANGE, NULL, array(new DateTime('2016-09-01'), new DateTime('2016-09-15')))

// Nette < 2.4 where date is optional
//->addConditionalRule(Form::MIN, NULL, new DateTime('2016-09-01'))
//->addConditionalRule(Form::MAX, NULL, new DateTime('2016-09-15'))
//->addConditionalRule(Form::RANGE, NULL, array(new DateTime('2016-09-01'), new DateTime('2016-09-15')))

->setAttribute('size', 10);

$form->addSubmit('submit', 'Send');
Expand Down
6 changes: 6 additions & 0 deletions examples/DateTimePicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
//->addRule(Form::MIN, NULL, new DateTime('2016-09-01 13:20'))
//->addRule(Form::MAX, NULL, new DateTime('2016-09-15 15:30'))
//->addRule(Form::RANGE, NULL, array(new DateTime('2016-09-01 13:20'), new DateTime('2016-09-15 15:30')))

// Nette < 2.4 where date and time is optional
//->addConditionalRule(Form::MIN, NULL, new DateTime('2016-09-01 13:20'))
//->addConditionalRule(Form::MAX, NULL, new DateTime('2016-09-15 15:30'))
//->addConditionalRule(Form::RANGE, NULL, array(new DateTime('2016-09-01 13:20'), new DateTime('2016-09-15 15:30')))

->setAttribute('size', 16);

$form->addSubmit('submit', 'Send');
Expand Down
6 changes: 6 additions & 0 deletions examples/TbDatePicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
//->addRule(Form::MIN, NULL, new DateTime('2016-09-01'))
//->addRule(Form::MAX, NULL, new DateTime('2016-09-15'))
//->addRule(Form::RANGE, NULL, array(new DateTime('2016-09-01'), new DateTime('2016-09-15')))

// Nette < 2.4 where date is optional
//->addConditionalRule(Form::MIN, NULL, new DateTime('2016-09-01'))
//->addConditionalRule(Form::MAX, NULL, new DateTime('2016-09-15'))
//->addConditionalRule(Form::RANGE, NULL, array(new DateTime('2016-09-01'), new DateTime('2016-09-15')))

->setAttribute('class', 'form-control')
->getLabelPrototype()
->setAttribute('class', 'control-label col-sm-3');
Expand Down
6 changes: 6 additions & 0 deletions examples/TbDateTimePicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
//->addRule(Form::MIN, NULL, new DateTime('2016-09-01 13:20'))
//->addRule(Form::MAX, NULL, new DateTime('2016-09-15 15:30'))
//->addRule(Form::RANGE, NULL, array(new DateTime('2016-09-01 13:20'), new DateTime('2016-09-15 15:30')))

// Nette < 2.4 where date and time is optional
//->addConditionalRule(Form::MIN, NULL, new DateTime('2016-09-01 13:20'))
//->addConditionalRule(Form::MAX, NULL, new DateTime('2016-09-15 15:30'))
//->addConditionalRule(Form::RANGE, NULL, array(new DateTime('2016-09-01 13:20'), new DateTime('2016-09-15 15:30')))

->setAttribute('class', 'form-control')
->getLabelPrototype()
->setAttribute('class', 'control-label col-sm-3');
Expand Down
10 changes: 8 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,18 @@ tbDatePicker:
format: j. n. Y
```

Part of form definition:
Part of form definition where date is optional (**Nette 2.4**):

```php
$this->addTbDatePicker('date', 'Date')
->setRequired()
->setRequired(FALSE)
->addRule(self::RANGE, NULL, array(new \DateTime('2016-09-01'), new \DateTime('2016-09-15')));
```

Part of form definition where date is optional (**Nette &lt; 2.4**):
```php
$this->addTbDatePicker('date', 'Date')
->addConditionalRule(self::RANGE, NULL, array(new \DateTime('2016-09-01'), new \DateTime('2016-09-15')));
```

Learn more in [examples](https://github.com/radekdostal/Nette-DateTimePicker/tree/master/examples).
286 changes: 286 additions & 0 deletions src/AbstractDateTimePicker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
<?php
/**
* Abstract DateTime Picker Input Control
*
* @package RadekDostal\NetteComponents\DateTimePicker
* @example https://componette.com/radekdostal/nette-datetimepicker/
* @author Ing. Radek Dostál, Ph.D. <radek.dostal@gmail.com>
* @copyright Copyright (c) 2016 Radek Dostál
* @license GNU Lesser General Public License
* @link http://www.radekdostal.cz
*/

namespace RadekDostal\NetteComponents\DateTimePicker;

use Nette\Forms\Controls\TextInput;
use Nette\Forms\Form;
use Nette\Forms\IControl;
use Nette\Forms\Rules;
use Nette\Forms\Validator;

/**
* Abstract DateTime Picker Input Control
*
* @author Radek Dostál
*/
abstract class AbstractDateTimePicker extends TextInput
{
/**
* Default format
*
* @var string
*/
protected $format = '';

/**
* Range
*
* @var array
*/
protected $range = array(
'min' => NULL,
'max' => NULL
);

/**
* Validation message
*
* @var string
*/
protected $message = NULL;

/**
* Initialization
*
* @param string $label label
* @param int $maxLength maximum count of chars
*/
public function __construct($label = NULL, $maxLength = NULL)
{
parent::__construct($label, $maxLength);
}

/**
* Sets custom format
*
* @param string $format format
* @return self
*/
public function setFormat($format)
{
$this->format = $format;

return $this;
}

/**
* Sets date and time
*
* @param string $value date and time
* @return void
*/
public function setValue($value)
{
if ($value instanceof \DateTime)
$value = $value->format($this->format);

if ($this->message !== NULL && $this->value !== NULL)
{
if ($this->range['min'] !== NULL && $this->range['max'] !== NULL)
$this->addRule(Form::RANGE, $this->message, array($this->range['min'], $this->range['max']));
elseif ($this->range['min'] !== NULL)
$this->addRule(Form::MIN, $this->message, $this->range['min']);
else
$this->addRule(Form::MAX, $this->message, $this->range['max']);
}

parent::setValue($value);
}

/**
* Adds a validation rule
*
* @param mixed $validator rule type
* @param string $message message to display for invalid data
* @param mixed $arg optional rule arguments
* @return self
*/
public function addRule($validator, $message = NULL, $arg = NULL)
{
$class = get_called_class();

switch ($validator)
{
case Form::MIN:
$this->range['min'] = $arg;
$arg = $arg->format($this->format);
$validator = $class.'::validateMin';
break;

case Form::MAX:
$this->range['max'] = $arg;
$arg = $arg->format($this->format);
$validator = $class.'::validateMax';
break;

case Form::RANGE:
$this->range['min'] = $arg[0];
$this->range['max'] = $arg[1];

$arg[0] = $arg[0]->format($this->format);
$arg[1] = $arg[1]->format($this->format);

$validator = $class.'::validateRange';
break;

default:
break;
}

return parent::addRule($validator, $message, $arg);
}

/**
* Adds a conditional validation rule
*
* @param mixed $validator rule type
* @param string $message message to display for invalid data
* @param mixed $arg optional rule arguments
* @return self
*/
public function addConditionalRule($validator, $message, $arg)
{
if (in_array($validator, array(Form::MIN, Form::MAX, Form::RANGE)) === TRUE)
{
if (method_exists($this->getForm(), 'getPresenter') === TRUE)
{
$this->message = $message;

switch ($validator)
{
case Form::MIN:
$this->range['min'] = $arg;
break;

case Form::MAX:
$this->range['max'] = $arg;
break;

case Form::RANGE:
$this->range['min'] = $arg[0];
$this->range['max'] = $arg[1];
break;

default:
break;
}

return $this;
}
}

return $this->getValue() instanceof \DateTime ? $this->addRule($validator, $message, $arg) : $this;
}

/**
* Validates minimum date and time
*
* @param \Nette\Forms\IControl $control control
* @param mixed $minimum minimum date and time
* @return bool
*/
public static function validateMin(IControl $control, $minimum)
{
if ($control->getValue() !== '')
return $control->getValue() >= $control->range['min'];

return TRUE;
}

/**
* Validates maximum date and time
*
* @param \Nette\Forms\IControl $control control
* @param mixed $maximum maximum date and time
* @return bool
*/
public static function validateMax(IControl $control, $maximum)
{
if ($control->getValue() !== '')
return $control->getValue() <= $control->range['max'];

return TRUE;
}

/**
* Validates range
*
* @param \Nette\Forms\IControl $control control
* @param array $range minimum and maximum dates and times
* @return bool
*/
public static function validateRange(IControl $control, $range)
{
if ($control->getValue() !== '')
{
if ($control->range['min'] !== NULL)
{
if ($control->getValue() < $control->range['min'])
return FALSE;
}

if ($control->range['max'] !== NULL)
{
if ($control->getValue() > $control->range['max'])
return FALSE;
}
}

return TRUE;
}

/**
* Sets validation messages
*
* @return void
*/
protected static function setValidationMessages()
{
$class = get_called_class();
$valueErrorMessage = 'Please enter a valid datetime format.';

// Nette >= 2.3
if (class_exists('\Nette\Forms\Validator') === TRUE)
{
Validator::$messages[$class.'::validateMin'] = Validator::$messages[Form::MIN];
Validator::$messages[$class.'::validateMax'] = Validator::$messages[Form::MAX];
Validator::$messages[$class.'::validateRange'] = Validator::$messages[Form::RANGE];
Validator::$messages[$class.'::valueError'] = $valueErrorMessage;
}
else
{
Rules::$defaultMessages[$class.'::validateMin'] = Rules::$defaultMessages[Form::MIN];
Rules::$defaultMessages[$class.'::validateMax'] = Rules::$defaultMessages[Form::MAX];
Rules::$defaultMessages[$class.'::validateRange'] = Rules::$defaultMessages[Form::RANGE];
Rules::$defaultMessages[$class.'::valueError'] = $valueErrorMessage;
}
}

/**
* Gets value error message
*
* @return string
*/
protected function getValueErrorMessage()
{
$class = get_called_class();

// Nette >= 2.3
if (class_exists('\Nette\Forms\Validator') === TRUE)
$msg = Validator::$messages[$class.'::valueError'];
else
$msg = Rules::$defaultMessages[$class.'::valueError'];

return sprintf($msg);
}
}
Loading

0 comments on commit aa283d8

Please sign in to comment.