Pinoco0.5ã¯æè»ãªããªãã¼ã¿ã売ã
ã¤ããã®å0.4ãåºããã¨ããã§ãããã¾ãæ©è½çã«ã²ã¨ç®ãããæããªã®ã§ã0.5ã¨ãã¦Pinocoãã¤ãã¼ãã¼ã¸ã§ã³ã®æ¡ãä¸ãã¾ããã
Downloads · tanakahisateru/pinoco · GitHub
ãã¡ããå¤æ´ç¹ã®ä¸è¦§ã§ãã
Changelog · tanakahisateru/pinoco Wiki · GitHub
ä¸çªããããããã®ã¯ãã©ã¤ã»ã³ã¹ãLGPLããMITã«å¤æ´ããç¹ã§ããããã®åæ©ã¨ãªã£ãã®ã¯ãæ©è½ãå¢ãã¦æç¨æ§ãå¤æ§åãã¦ããã¨æã£ãããã§ãã
å½åã¯ãããã¯/ã¬ã³ããªã³ã°ã¨ããããã¼(åºæã®æ©è½)以å¤ã«æç¨ãªã¨ããã¯ãªããåºæã®æ©è½ã¯ã使ãã使ããªããã¦ã¼ã¶ãé¸ã¶ã ãã§ãããããæ¸ãæãã¦ä½¿ããããªãã®ã§ããªãã£ããã®ã§ããããã¾ãç¬ç«ãã¦æç¨ãªãä¸è¬åé¡ã解決ãã¦ããç®æãå¢ãã¦ãã¦ãããããä¾åæ§ãæé¤ããããæ¸ãæãã¦æãåºãã°ãåç¬ã§åå©ç¨ã§ããã©ã¤ãã©ãªãåãåºããã¨ããå¯è½æ§ãã¯ã¬ã¸ãããæ®ãã¦ããããããã°ã誰ã§ãå ã ã¨æ¹é ãã¦ä½¿ããããã«ãããã£ãã
ã§ããã®åå©ç¨ãã¦æç¨ãªæ©è½ã®å¤§åæ°äººããPinoco_Validatorã§ãã
Pinoco0.5ã®ããªãã¼ã¿ã¯PHPã®æç¶ãã³ã¼ãã®ä»£æ¿ã§ããMVCãªææ³ã®ãã¬ã¼ã ã¯ã¼ã¯ã§ã¯ãæ°¸ç¶åãããã¢ãã«ã ããã¨ãã©ã¼ã ã¢ãã«ã ããã¨ãããªãã¼ã¿ã宣è¨ã§ãã¾ãããã§ããPinocoã¯æ®å¿µãªããMVCã§ã¯ããã¾ããããªã®ã§ãããã¾ã§ã®ãã¼ã¸ã§ã³ã§æ®éã«ããã¨ããããªãµãã«æ¸ããªãããããªãã
<?php if(empty($_POST['title'])) { $errors['title'] = "å ¥åãã¦ãã ãã"; } if(empty($_POST['body'])) { $errors['body'] = "å ¥åãã¦ãã ãã"; } else if(strlen($_POST['body']) < 2) { $errors['body'] = "2æå以ä¸å ¥åãã¦ãã ãã"; } //ç¥ if(count($errors)) { //ã¨ã©ã¼ç³» $this->errors = $errors; $this->page = "input.html"; } else { //æ£å¸¸ç³» } ?>
ããã¾ã§æ¸ããªãã¨ããããåããªãã®ã¯ãããã©ãããã§ããã
ããªãã¼ã¿ã使ãã¨ãã¾ãããã ãæ¸ãã°åããã§ãã¯ãã§ãã¾ãã
<?php $v = Pinoco::newObj('Pinoco/Validator.php/Pinoco_Validator', $_POST); $v->check('title')->is('not-empty'); $v->check('body')->is('not-empty')->is('min-length 2'); if($v->invalid) { $this->form = $v->result; $this->page = "input.html"; } else { //æ£å¸¸ç³» } ?>
å ¨ä½çã«è±æã¨ãã¦èªç¶ã§ãããã¡ãã£ã¨ DSLã£ã½ãã§ãããã§ãã¯å¯¾è±¡ã®ãã£ã¼ã«ãåãå·¦ã«æãã®ã§ãããã¶ãã³ã¼ããè¦ããããªãã¾ãããã§ãã¯ãããæ¡ä»¶ãå¢ããã°ãå³ã«è¶³ãã¦ããã°ããã ããªã®ã§ã³ã¼ãã®è¦ãç®ãæªããã¾ããã
ã¨ã©ã¼ããã£ããã©ããã§åå²ããé¨åããããã¨è¦ã§åå²ã®æå³ãæ確ã§ããã§ãresultãformã¨å¼ãã§ã¬ã³ããªã³ã°ã«ä½¿ããã¨ãã¦ãã¨ãããããã«ãã½ã§ãããããã£ã¦ããã¨ãHTMLã§ã®ãã¥ã¼å®è£ ã¯ãããªãµãã«æ¸ãã¾ãã
<form method="POST" tal:define="form this/form"> <label>ã¿ã¤ãã«</label> <input name="title" type="input" tal:attributes="value form/title/value" /> <span class="err" tal:condition="form/title/invalid" tal:content="form/title/message">ã¨ã©ã¼</span> <br /> <label>æ¬æ</label> <textarea name="body" tal:content="form/body/value"></textarea> <span class="err" tal:condition="form/body/invalid" tal:content="form/body/message">ã¨ã©ã¼</span> </form>
å¤æ°(ã«è¦ãããã®)ãæµãããã ãã®ãTALçã«ç´ ç´ãªè¨è¿°ã«ã§ãã¾ãã
ãããé«åº¦ãªãã©ã¼ã ãã«ãã®ã»ãããããã§ããããã©ããªã«ãHTMLãã¶ã¤ãã¼ã«XHTMLäºæãç´æãã以ä¸ãé«åº¦ãããæ½è±¡åã¯é¿ãããã§ããããã§ãã ããã¨ãã£ã¦ãé«åº¦ãªè¨èªæ©è½ãå¼·å¶ããã®ããããããªãããã®ããããã¡ããã©ãããã¿å æ¸ãã¨æãã¾ãã
resultã«ã¯checkãããã£ã¼ã«ãããå ¥ããªãã®ã§ããã§ãã¯æ¼ããããã°è¦åããã¾ãããããã¨ãããããã®ã¾ã¾ãã¼ãã§ãã¯ã§ããã¨ããå ´åã¯ããããã¦ããã°ããã§ãããã
$v->check('field_name')->is('pass'); // å¿ ãæåã
ã§ããã®ã¾ã¾ã§ã¯ãã¨ã©ã¼ã¡ãã»ã¼ã¸ãããã©ã«ãã®ã¾ã¾ã§ããã¡ãã»ã¼ã¸ã®ã«ã¹ã¿ãã¤ãºããã¾ããããã¨ã£ã¦ãç°¡åã
<?php $v = Pinoco::newObj('Pinoco/Validator.php/Pinoco_Validator', $_POST, array( 'not-empty' => "å ¥åãã¦ãã ãã", 'min-length' => "{param}æå以ä¸å ¥åãã¦ãã ãã", )); $v->check('title')->is('not-empty'); $v->check('body')->is('not-empty')->is('min-length 2'); ?>
ãããã¯ããã
<?php $v = Pinoco::newObj('Pinoco/Validator.php/Pinoco_Validator', $_POST); $v->check('title') ->is('not-empty', "ã¿ã¤ãã«ãå ¥åãã¦ãã ãã"); $v->check('body') ->is('not-empty', "æ¬æãå ¥åãã¦ãã ãã") ->is('min-length 2', "æ¬æãçãããã¾ã"); ?>
ããããã®ã§ãããã§ãã
<?php $v = Pinoco::newObj('Pinoco/Validator.php/Pinoco_Validator', $_POST, array( 'not-empty' => "{label}ãå ¥åãã¦ãã ãã", )); $v->check('title', "ã¿ã¤ãã«")->is('not-empty'); $v->check('body', "æ¬æ")->is('not-empty')->is('min-length 2', "{label}ãçãããã¾ãã"); ?>
ã¨ããã§ããããã¯ã¤ãã¹ãã¼ã¹ãæåæ°ã¨ãã¦æ°ãã¦ãã¾ãã¨ã¾ããã§ããããé ç®ã®ãã§ãã¯ããã¼ã®ãã§ã¤ã³ã®ä¸ã«ã¯ãå¤ãæ£è¦åããããã®ãã£ã«ã¿ãå ¥ãããã¨ãã§ãã¾ãã
<?php $v->check('title')->is('not-empty')->filter('trim'); $v->check('body')->is('not-empty')->filter('trim')->is('min-length 2'); ?>
ãã«ãã¤ã³ã®ãã§ãã¯ã ãã§ã¯ä¸ååãªå ´åãã¢ãããã¯ã«ãã§ãã«ã追å ãããã¨ãå¯è½ã§ãã
<?php $v->defineValidityTest('zip-code', function($value) { return preg_match('/^[0-9]{3}-[0-9]{4}$/', $value); }, "Zip code only."); $v->check('zip_code')->is('zip-code'); ?>
ãããé¢åãªäººã¯ãããããã®ã§ãOKã§ããã
<?php $v->check('zip_code')->is(function($value) { return preg_match('/^[0-9]{3}-[0-9]{4}$/', $value); }, "éµä¾¿çªå·ãå ¥åãã¦ãã ãã"); ?>
ã¾ãã¾ãã¯ãã¼ã¸ã£ä¸æ³ã§ããã¾ããã¯ãã¼ã¸ã£ã使ããªãå ´åã¯ã³ã¼ã«ããã¯ã§ãããã§ããã
ãã¨ãã®ããªãã¼ã¿ã®æ大ã®ã¡ãªããã¯ãããã宣è¨ã§ã¯ãªãæç¶ãã ã¨ãããã¨ã§ããã¤ã¾ããæ§é åããã°ã©ãã³ã°ã¨ã®çµã¿åãããã§ããããã§ãã宣è¨çããªãã¼ã¿ã§ã¯ããã¯ããã¾ããã
<?php // ãURLããããã§ãã¯ããã¯ã¹ããªã³ã®ã¨ãã®ã¿URLãæ¤è¨¼ãããã¨ã if($v->contextFor('checkbox_has_url')->is('== 1')->valid) { $v->check('link')->is('not-empty')->is('url'); } ?>
ããããåå²ãªãã¦ãããªãã宣è¨çããªãã¼ã¿ã®ã»ãã好ããã¨æãããªãããããªã®ã¯ããããã¼ã¹ã«ç°¡åã«ä½ãã¾ãããã
ã¡ãªã¿ã«ãnot-emptyãã§ãã¯ããã«ä»ã®ãã§ãã¯ããå ´åãå¤ç©ºã£ã½ãéãã¦ãã¾ãã®ã§ã注æãããªãã·ã§ãã«ãªãã©ã¼ã ãã£ã¼ã«ãã¯ãå¤ããã£ãã妥å½æ§æ¤è¨¼ããªãããããªããã©ã空ã£ã½ãªãããã¯ã¹ã«ã¼ãã¦ãããã»ãã便å©ã§ããã
ãããããããPinocoã§ã®éçºãæèãã¾ãã£ã¦APIè¨è¨ããã®ã§ãããå ´åã«ãã£ã¦ã¯ä»ã®ãã¬ã¼ã ã¯ã¼ã¯ã«çµ±åãããããããã¯ãã»ãã¨ã«çã®PHPã¹ã¯ãªããã§ä½¿ã£ã¦ã便å©ãªããããªãã ããããã¨æãã¾ãããã¨ããããã§ãMITã«ãªã£ãã®ã§ããããããã¯ãã¦ä½¿ããããªã¨ããã ã使ã£ã¦ãã ãããGitHubã§ãã£ã¼ãããã¯ããã°ã¨ã¦ãå¬ããã§ãã
次åãPinoco0.5ã®PHPTALã®ãã¼ã ã¹ãã¼ã¹æ¡å¼µã«ã¤ã¥ãã