Last active
February 6, 2017 14:17
-
-
Save miroslav-mrazek/315176c2f0c455f1d84983963c4e43d3 to your computer and use it in GitHub Desktop.
Nette + JQuery UI datepicker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- nutné scripty --> | |
<script src="{$basePath}/vendor/jquery/dist/jquery.min.js"></script> | |
<!-- JQuery UI --> | |
<script src="{$basePath}/vendor/jquery-ui/jquery-ui.js"></script> | |
<!-- Konfigurace datepickeru. --> | |
script src="{$basePath}/vendor/jquery-ui/ui/i18n/datepicker-cs.js"></script> | |
<script src="{$basePath}/js/components/ui.datepicker.init.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Utils; | |
class Filters | |
{ | |
const STRIP_SPACES = 'App\Utils\Filters::stripSpaces'; | |
/** | |
* Odstraní mezery z celého řetězce. | |
*/ | |
public static function stripSpaces($string) | |
{ | |
return str_replace(' ', '', $string); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Nette\Application\UI\Form; | |
use App\Utils\Validators; | |
use App\Utils\Filters; | |
class AssignmentFormFactory | |
{ | |
/** | |
* @return Form | |
*/ | |
public function create() | |
{ | |
$form->addText('dateFrom', 'Datum počátku') | |
->setAttribute('class', 'date') | |
->addFilter(Filters::STRIP_SPACES) | |
->addRule(Form::FILLED, "Vyplňte, prosím, datum počátku.") | |
->addRule(Validators::DATE, "Zadejte platné datum počátku ve formátu den.měsíc.rok"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Oživení DatePicker inputů | |
* @author MMR | |
*/ | |
$.nette.ext('datepicker', { | |
load: function () { | |
$("input.date").each(function () { // input[type=date] does not work in IE | |
$(this).datepicker(); | |
}); | |
} | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Utils; | |
use Nette\Forms\IControl; | |
use DateTime; | |
use Exception; | |
class Validators | |
{ | |
const DATE = 'App\Utils\Validators::isDate'; | |
/** | |
* Zkontroluje, že ze zadaného data lze vytvořit objekt DateTime. | |
* Je vyžadován tvar "d.m.Y". | |
*/ | |
public static function isDate(IControl $control) | |
{ | |
try { | |
$arr = explode(".", $control->getValue()); | |
if (count($arr) === 3) { | |
$d = $arr[0]; | |
$m = $arr[1]; | |
$y = $arr[2]; | |
if (is_numeric($d) && strlen($d) <= 2 && | |
is_numeric($m) && strlen($m) <= 2 && | |
is_numeric($y) && strlen($y) === 4 | |
) { | |
new DateTime($control->getValue()); | |
return checkdate($m, $d, $y); | |
} | |
} | |
return FALSE; | |
} | |
catch (Exception $e) { | |
return FALSE; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment