ãã®è¨äºãä¸è¡ã«ã¾ã¨ããã¨
ãã©ã¯ã¨ã£ã½ãã§ããããããã¨ãã渡ãæ¹æ³ãåããã¾ããã§ãã
ããã®é³ãããããåºãªãã§ããã©ãã
å
¥åããããã¼ã¿ã®ã¨ã©ã¼ãã§ãã¯ãè¡ãã«ããã£ã¦ãCakePHPã§ã¯ããªãã¼ã·ã§ã³ç¨ã®ã¯ã©ã¹ããä½ããããããããç¨æããã¦ãã¾ããããã使ãã°ãï¼ä¸ããèªä½ããããã¯ï¼ç°¡åã§æè»ãªã¨ã©ã¼ãã§ãã¯ãè¡ããã®ã§ãããå人çã«ã¯ãCakePHP2ã¨3ã§ããã®ããªãã¼ã·ã§ã³ã®ä½¿ãæ¹ã大ããå¤ãã£ãæ°ããã¾ããã£ã¦ããCakePHP3ã®æ¹ã®ããªãã¼ã·ã§ã³ãããã¾ãã¡ä¸æã使ãããªãããã
ãããªããã§ä»æ¥ã¯ã誰ãå©ãã¦ãã¨ããã¡ãã»ã¼ã¸ãè¾¼ãã¤ã¤ãããªãã¼ã·ã§ã³ã®ä½¿ãæ¹ãè¦ã¦è¡ãããã¨æãã¾ãã
æ£ç´ãããã®ãããé·ãè¨äºã«ãªããã¨ãäºæ³ããã¾ãããå¿ è¦ãªã¨ãã ãæ¾ã£ã¦èªãã§ãããããã¨æãã¾ãã
å 訳ã¯ãããªæãã§ãã
ã§ã¯é·ä¸å ´ã®æ ã«ããã£ã¤ããã¼ã
ããã§ã¯ãusersã¨ãããã¼ãã«ã«ãååã¨ã¡ã¼ã«ã¢ãã¬ã¹ãå ¥åããç»é¢ãç¨æãã¦ã¿ã¾ããCakePHPã®Formãã«ãã¼ã使ã£ã¦ãäºã¤ã®ããã¹ãããã¯ã¹ãçæãã¾ããã¢ãã«åã¯Userã
ãã£ããæ¸ãã¨ããããªæãã§ãããã
CakePHP2ã§ã¨ã©ã¼ãã§ãã¯ãè¡ãã«ã¯ãvalidates()ã¨ããã¡ã½ããã使ãã¾ãã
CakePHP3ã§ã¯ãã¨ã³ãã£ãã£ãä½æããéã«ãã¨ã©ã¼ãã§ãã¯ãè¡ããã¨ãã§ãã¾ãã
ç´°ãã説æã«ã¤ãã¦ã¯ãä»æ¥ã¯çç¥ãã¾ããããããã°ã¨ã©ã¼ãã§ãã¯ãã§ããã£ã¦ãã¨ã ããæ¼ããã¨ãããããã
newEntity()ã¨ãã®åãã«ã¤ãã¦ã¯ã以åã«ãè¨äºãæ¸ãããã¨ãããã®ã§ãããåèã«ãªãããã§ããã°ããã¡ããã©ã¼ããã¨ã©ã¼ãã§ãã¯ã«ã¤ãã¦ãå°ã触ãã¦ã¾ãã
CakePHP3ã触ã£ã¦ã¿ã¾ããããcreateãsetãã§ããªããã§ããã©ï¼ã
ä»åã¯ãååãã¡ã¼ã«ã¢ãã¬ã¹ãå¿ é å ¥åã§ããã¨ããåæã§ããã¾ããã¤ã¾ãã©ã¡ããæªå ¥åã ã£ããã¨ã©ã¼ã«ãªããã¤ãã§ã«ã¡ã¼ã«ã¢ãã¬ã¹ã«é¢ãã¦ã¯ãå½¢å¼ã®ã¨ã©ã¼ãã§ãã¯ãè¡ã£ã¦ã¿ã¾ãããã
CakePHP2ã ã¨ããããªãã¾ãã
ãruleããã¨ã©ã¼ãã§ãã¯ã®ã¡ã½ããããmessageããã¨ã©ã¼ã¡ãã»ã¼ã¸ã«ãªãã¾ãã
ãnotEmptyãããemailãã¨ããã®ã¯ããããããç¨æããã¦ããã¡ã½ããã§ããããã§ãvalidates()ã¡ã½ãããå¼ã³åºããã¨ãã«ãå¿ é å ¥åãã¡ã¼ã«ã¢ãã¬ã¹ã®å½¢å¼ã«é¢ããã¨ã©ã¼ãã§ãã¯ãè¡ããã¨ãã§ãã¾ãã
ä»ã«ãã©ããªã¡ã½ããããããã¯ãã³ã¢ã©ã¤ãã©ãªã®ãUtilityãã¨ãããã©ã«ãã®ä¸ã«ãValidation.phpãã¨ãããã¡ã¤ã«ãããã®ã§ããã®ä¸ãè¦ã¦ãã ããã
ä¸è¨ã®ããã«æ¸ããå ´åãã¨ã©ã¼ãããã¨ããã¥ã¼ã®ã$this->validationErrorsãã¨ããå¤æ°ã«ã¨ã©ã¼ã¡ãã»ã¼ã¸ãå ¥ã£ã¦ãã¾ãã
ç¶ãã¦CakePHP3ã
CakePHP2ã§ã¯ã$validateã¨ããã¡ã³ãå¤æ°ã«å¿ è¦ãªå 容ãæ¸ãã¾ããããCakePHP3ã§ã¯ãvalidation***ãã¨ããã¡ã½ããããã¼ãã«ã«ä½ãã¾ãã
ä¸è¨ã®ä¾ã§ã¯ãvalidationDefault()ãã¨ãªã£ã¦ãã¾ãããã¡ã½ããåã¯ä»»æã«å¤ãããã¨ãå¯è½ã§ãããvalidationOyado()ãã§ããvalidationOyaki()ãã§ããvalidationOyakata()ãã§ãä½ã§ãè¯ãã
ãã ãDefault以å¤ã«ããå ´åã¯ãnewEntity()ãpatchEntity()ã§ãã®ã¡ã½ãããå¼ã³åºãããã®è¨å®ãå¿ è¦ã«ãªãã
第äºå¼æ°ï¼patchEntity()ã®å ´åã¯ç¬¬ä¸å¼æ°ï¼ã«ããvalidation***ãã®***ã®é¨åãè¨è¿°ãã¾ããå é ã¯å°æåã§ãkã
ãã®ãvalidate => ‘***’ããçç¥ããå ´åã¯ãvalidationDefault()ãå¼ã°ãã¾ãã
CakePHP3ã§ã¯ãåºæ¬çã«ã¯$validatorã®add()ã¡ã½ããã使ã£ã¦ãã«ã¼ã«ã追å ãã¦ããã¾ããã§ãnotEmptyã¯ããã®ã¾ã¾notEmpty()ã¨ããã¡ã½ããã使ãã¿ãããä½ã§ããªï¼
CakePHP3ã§ãããããç¨æããã¦ããã¡ã½ããã¯ãã³ã¢ã©ã¤ãã©ãªã®ãValidationãã¨ãããã©ã«ãã®ä¸ã«ãããValidation.phpãã§ç¢ºèªã§ãã¾ãã
ã¨ã©ã¼ãããã¨ãåé ã§ãæ¸ãã¾ããããã$user->errors()ãã§ã¨ã©ã¼ã¡ãã»ã¼ã¸ãåå¾ã§ãã¾ãã
notEmpty()ã®å ´åã¯ã_emptyãã¨ãããã¼ï¼ãã¶ãåºå®ï¼ã«ã¡ãã»ã¼ã¸ãå ¥ãã¾ããadd()ã§è¿½å ããå ´åã¯ãèªåã§ãã¼ãæå®ã§ãã¾ãã
è¨å®æ¬¡ç¬¬ã§ãã好ã¿ã®ãã¼ã«ã§ãã¾ããokonomiyakiã¨ãã«ãã§ãã¾ãã
次ã¯ãã¨ã©ã¼å¤å®ç¨ã®ã¡ã½ãããèªä½ããå ´åã§ããä¾ãã°ãããã£ããâ¦â¦ååã«ã²ãããªãã使ããªããã¿ãããªã«ã¼ã«ãèªä½ãã¦ã¿ã¾ããããããã©ã¯ã¨ã£ã½ãã§ããããããã«ã¼ã«åããdraqueãã§è¡ãã¾ãããã
ããæ¹ã¯å¤§ããåãã¦äºã¤ããã¾ãã
ä¸ã¤ã¯ãã¢ãã«ããã¼ãã«ã«ç´æ¥ã¡ã½ãããæ¸ãããæ¹ã
CakePHP2ã®å ´åã¯ãã¢ãã«ã«æ¸ãã¾ãã
ãã£ãããããªæããä½ã§draque()ã®ä¸ã§array_shift()ãã¦ããã£ã¦ããã¨ãä¸è¨ã®ããã«ã¨ã©ã¼ãã§ãã¯ãè¡ã£ãå ´åã$dataã®ä¸èº«ã¯ãããªã£ã¦ãããã§ãã
ã$data[‘name’]ãã¨ãã¦ãè¯ããã ãã©ãããã ã¨ãã£ã¼ã«ãåãnameã®ã¨ããã対å¿ã§ããªããããä¸å¿ãããªé¢¨ã«æ¸ãã¦ã¾ãã
ãã¨ã¯æ£è¦è¡¨ç¾ã§ãå ¥åãããæååãã²ãããªã ãã ã£ãããtrueãè¿ãããã«ãã¦ã¾ãã
ã¡ãªã¿ã«ãä½ãããã®å¤ãä¸ç·ã«æ¸¡ãããå ´åã
ruleã®ã¨ãããé åã§æ¸ããã¨ã«ãã£ã¦ãå¼æ°ã«å¤ã渡ãã¾ãããã®å ´åã¯ããããããã¨ããããã¨ããããããã$value1ã¨$value2ã«å ¥ãã¾ãã
CakePHP3ã®å ´åã¯ããã¼ãã«ã«æ¸ãã¾ãã
åºæ¬ã®æ¸ãæ¹ã¨ã¡ãã£ã¨éãã®ã¯ãruleã®ã¨ããã§ããã[$this, ã¡ã½ããå]ã¨ããæ¸ãæ¹ã«ãªã£ã¦ãã¾ãã
CakePHP3ã®å ´åã¯ã$valueã«å ¥åãããå¤ãå ¥ã£ã¦ãã¾ãã®ã§ãarray_shift()çãªå¦çã¯ãããªãã§ãããã®ã¾ã¾ä½¿ãã¾ãããã¨è¿ãå¤ã¯ãbooleanã«ããªãã¨ä¸æãåããªãã£ã½ãã
ruleã®ä¸ã«ãç´æ¥ã¡ã½ãããæ¸ããã¨ãã§ãã¾ãã
çµæã¯ä¸ç·ã§ãã
CakePHP3ã®æ¹ã§ãã£ãå ´åããããããããããããã¨ãã渡ãæ¹æ³ãåããã¾ããã§ããã
試ãã«ãããªé¢¨ã«æ¸ãã¦ã¿ããããããã ãã©ãã¨ã©ã¼ã«ãªã£ããããªãã¼ã·ã§ã³ã®ã¨ã©ã¼ã§ã¯ãªãã¦ãã·ã¹ãã ã¨ã©ã¼ãã
ããããæ¸ãæ¹ã¯ã§ããªãã¿ããã§ããããä½ããããã¤ã¬ã¯ãã«å¤ã渡ãããã£ã¦ãªã£ãå ´åã¯ãã©ãããã°è¯ãã®ããªï¼
CakePHP2ã®å ´åã¯ãBehaviorã使ã£ã¦æ¸ããã¨ãªãããã§ãã¾ãã
ä¾ãã°ãValidateBehavior.phpã¨ãããã¡ã¤ã«ãä½ã£ã¦ã¿ã¾ãããããã¡ã¤ã«ã¯ãModelãã©ã«ãã®ä¸ã«Behaviorã¨ãããã©ã«ããããã®ã§ããã®ä¸ã«ä½æãã¾ãããªããã°Behaviorãã©ã«ããä½ã£ã¦ãã ããã
Behaviorãèªã¿è¾¼ãã«ã¯ã$actAsã¨ããã¡ã³ãå¤æ°ã使ãã¾ãã
ã¢ãã«ã«ç´æ¥æ¸ãå ´åã¨ã»ã¨ãã©ä¸ç·ã§ãããBehaviorã«æ¸ããå ´åã第ä¸å¼æ°ã«ã¯ã¢ãã«ã®ã¤ã³ã¹ã¿ã³ã¹ï¼ã§è¯ãã®ããªï¼ï¼ã渡ãã¦ãã¾ãã$dataã¯ç¬¬äºå¼æ°ããããã¨ã渡ããããã°ããã®å¾ãã«å¼æ°ã追å ããã°ãªã¼ã±ã¼ã§ãã
CakePHP3ã®å ´åã¯ãã«ã¹ã¿ã ããªãã¼ã·ã§ã³ã¨ãããã®ãä½æãããã¨ãã§ãã¾ãã
Modelãã©ã«ãã®ä¸ã«ãValidationãã¨ãããã©ã«ããä½ãããã®ä¸ã«ããªãã¼ã·ã§ã³ãã¡ã¤ã«ãä½æãã¾ããããã§ã¯ãCustomValidation.phpãã¨ãããã¡ã¤ã«ãä½ã£ã¦ã¿ã¾ãã絶対ã«ãModel/Validationãã®ä¸ã«ä½ããªãã¨ãããªãããã§ã¯ãªãã§ãããããã«ã¤ãã¦ã¯å¾ã§ã
CustomValidationã使ããããã«ããã«ã¯ãprovider()ã¨ããã¡ã½ãããå¿ è¦ã«ãªãã¾ãã第ä¸å¼æ°ã®ãcustomãã¯ä½ã§ãè¯ãã§ããadd()ã¡ã½ããã®ä¸ã§ã’provider’ => ‘custom’ãã¨ããã®ãããã¾ãããããã«ä½¿ç¨ããæååãæå®ãã¦ããã ããªã®ã§ãcustomã®ä»£ããã«gundamã¨ãã«ããããã’provider’ => ‘gundam’ãã¨ããã°è¯ãã ãã
第äºå¼æ°ã§ãCustomValidationããããã¹ãæå®ãããã¨namespaceããæ£ããå ´æãæãã¦ããã°ãCustomValidation.phpã¯ã©ãã«ãã£ã¦ãè¯ãã£ã½ãã§ããsrcãã©ã«ãã®ç´ä¸ã«ç½®ããããããæ¸ãã°åé¡ãªãåãã
ã«ã¹ã¿ã ã使ãå ´åã¯ãproviderã®æå®ãå¿ããªãã§ãã ããããããå¿ãã¡ããã¨ã¨ã©ã¼ã«ãªã£ã¡ããã
ãã¨ããä¸ã¤æ°ãã¤ããªãããããªãã®ããCustomValidation.phpã®ä¸ã«ã¡ã½ãããæ¸ãå ´åã¯ãstaticã§ãªããã°ãããªãã¨ãããã¨ã§ããstaticãæ¸ãå¿ãã¦ãã¨ããã£ã±ãã¨ã©ã¼ã«ãªã£ã¡ããã¾ãã
ãã¼ãã«ã«ç´æ¥æ¸ãå ´åãã«ã¹ã¿ã ãã¡ã¤ã«ãä½ãå ´åãã第äºå¼æ°ã«ã³ã³ããã¹ããã¼ã¿ã£ã¦ã®ãå ¥ã£ã¦ãã¾ããçç¥ãã¦æ¸ããã¨ãã§ããã®ã§ãä¸è¨ã®ä¾ã§ã¯æ¸ãã¦ã¾ããããã³ã³ããã¹ããã¼ã¿ãåå¾ããããã°ãã¡ã½ããã®ç¬¬äºå¼æ°ãæ¸ãã¦ãã ããã
$contextã®ä¸ã«ã¯ãããã§ã¯æ¸ããããªããããã®ããããªãã¼ã¿ãå ¥ã£ã¦ã¾ãããã¼ãã«ãªãã¸ã§ã¯ããå ¥ã£ã¦ãã
ãã¼ãã«ãªãã¸ã§ã¯ãããããªãããã使ã£ã¦ããããã¨å¦çãè¡ãã¦ãè¯ããããªãã®ãªãã§ããã©ãå¤æ°ãprotectedã«ãªã£ã¦ãããããã«ã¹ã¿ã ã®æ¹ã§ã¯ä½¿ããªããã®ããããã§ããã
ãã¼ã¨ãä¾ãã°ã¡ã¼ã«ã¢ãã¬ã¹ã®éè¤ç»é²ãé¿ãããããªå ´åãå ¥åãããã¢ãã¬ã¹ããã§ã«ãã¼ã¿ãã¼ã¹ã®ä¸ã«ãããã©ããããã§ãã¯ããããããªãã§ããã
ä¾ãã°ãããªãã¨ãã§ãããã§ããããã£ã¦ããã¨ã¯ãå ¥åãããã¡ã¼ã«ã¢ãã¬ã¹ãDBã®ä¸ã«ãããã©ããå¤å®ãã¦ã1件以ä¸ãã£ãããéè¤ã¨ã©ã¼ãåºãã¨ãããã®ãããããã¨èª¬æã¯çç¥ãã¡ããã¾ãããã$context[‘providers’][‘table’]ãã«ãã¼ãã«ãªãã¸ã§ã¯ããå ¥ã£ã¦ãã®ã§ããã使ãã°ããã¼ã¿ãã¼ã¹ã«ã¢ã¯ã»ã¹ãããã§ãã¾ããCustomValidation.phpã®æ¹ã«æ¸ãã¦ãåãããã«åãã¾ãã
æ°è¦ç»é²æã¯ããã§è¯ãã¨æãã¾ããã§ããã¼ã¿æ´æ°æã¯ãèªèº«ã®ãã¼ã¿ãåç §ãã¦ãã¾ãã¨ãå¿ ã$countã1以ä¸ã«ãªã£ã¡ããã¾ããã ããèªåã®IDã ãã¯é¤ããããªå¦çãå¿ è¦ã«ãªãã¨æããã§ããã
ä¾ãã°ãããªé¢¨ã«æ¸ããã¨ãã¾ããã$table->_primaryKeyãã«ã¯ããã©ã¤ããªã¼ãã¼ãå ¥ã£ã¦ã¾ãããã©ã¤ããªã¼ãã¼ã絶対ã«ãidãã§åºå®ã¨ããªãè¯ããã§ããã©ãid以å¤ã«ãããããããªãäºæ ãæ³å®ãã¦ãããã§ã¯ãã¼ãã«ãªãã¸ã§ã¯ããã¼ã¿ã®ä¸ããåç §ã§ããããã«æ¸ãã¦ãã¾ãã
ãã®æããã¼ãã«ã«ç´æ¥æ¸ããå ´åã¯ã¨ã©ã¼ã«ãªããªããã§ããã©ãCustomValidation.phpã®æ¹ã«æ¸ãã¨ããã©ã¤ããªã¼ãã¼ãåãã¾ãããã¨ã©ã¼ã«ãªãã¾ãã
ã ããæ¸ãæ¹æ¬¡ç¬¬ã§ã¯ããã£ããCustomValidation.phpãä½ã£ã¦ãç¬èªã¡ã½ãããããã§ä¸æ¬ç®¡çãããã¨æã£ã¦ããã®ã«ããã¼ãã«ã®æ¹ã«ãæ¸ããªãããããªããªã£ãããããã¨ãããã£ãããªãã£ããï¼
ããããè¨è¨ããããªãã£ã¦è¨ããã¡ããã°ããã¾ã§ãªãã§ããã©â¦â¦ããããæã¯ã©ããããè¯ããã§ããããï¼
ãã£ãã®draqueã¡ã½ããã使ã£ãã¨ã©ã¼ãã§ãã¯ããã³ã³ããã¼ã©ã¼ãã使ã£ã¦ã¿ã¾ãããã
ã¨ã³ãã£ãã£ãä½æå¾ãã³ã³ããã¼ã©ã¼ã§ç´æ¥ããªãã¼ã·ã§ã³ã¡ã½ãããå¼ã³åºãã¦ãã¾ãããã£ã¦ããã¨ã¯ã»ã¨ãã©ä¸ç·ã§ãã$validatorã®ã¤ã³ã¹ã¿ã³ã¹ãæ°è¦ã«ä½æãã¦ãã«ã¼ã«ã®æ¸ãæ¹ã¯ä»ã¾ã§ã¨ä¸ç·ãæå¾ã«erros()ã¨ããã¡ã½ããã§ã¨ã©ã¼ãã§ãã¯ãè¡ã£ã¦ãã¾ããerrors()ã«ã¯ãã¨ã³ãã£ãã£ã®ãªãã¸ã§ã¯ããã¼ã¿ã§ã¯ãªããå ¥åããããã¼ã¿ã®é åã渡ãå¿ è¦ãããã®ã§ãtoArray()ã¡ã½ããã使ã£ã¦ããã¼ã¿ã ãã渡ãããã«ãã¦ãã¾ãã
ã¨ã©ã¼ãããã¨$errosã«ã¨ã©ã¼å 容ãå ¥ã£ã¦ããã®ã§ãããã使ã£ã¦ã¨ã©ã¼ã¡ãã»ã¼ã¸ã表示ããããç»é²å¦çãè¡ã£ããã§ãã¾ãã
å ¥åããããã¼ã¿ããã¨ã³ãã£ãã£ã使ã£ã¦å å·¥ãããã¨ããããããªãã§ãããã¨ã³ãã£ãã£ã«ã¡ã½ãããä½ã£ã¦ã
ããªãã¼ã·ã§ã³ã®ã¨ãã«ãã®ã¨ã³ãã£ãã£ã§ä½ã£ãã¡ã½ããã使ããããªã¼ã£ã¦æã£ããã¨ããããã§ããã©ãvalidationDefault()ã¨ãã®ä¸ã§ããã使ããªããã§ãããï¼
ä½ãè¨ã£ã¦ããã£ã¦ã¼ã¨ãä¾ãã°â¦â¦ãã¼ã¿ãã¼ã¹ã§ã¯ãååã¯nameã¨ããä¸ã¤ã®ãã£ã¼ã«ãã§æã£ã¦ããã ãã©ãå ¥åã¯å§ã¨åãåãã¦å ¥åãããããªå ´åãèãã¦ã¿ã¾ããããã§å ¥åããéã«å§ã¨åããã£ã¤ãã¦ããã«ãã¼ã ã«ãã¦ããã¨ã©ã¼ãã§ãã¯ãè¡ããã¿ãããªã
ããããã¨ãã£ãã¼çç¥ãã¦æ¸ãã¡ããã¾ãããããããããããªæãã®ãã¨ãããããã¨ããã
ããã¯ãã¨ã³ãã£ãã£ã®fullName()ã£ã¦ããã¡ã½ããã使ã£ã¦ãã¨ã©ã¼ãã§ãã¯ãè¡ãåã«å§ã¨åãåãããå¤ãnameã«å ¥ãããããªããããªãã¨ãã§ãããè¯ããªã¨ããã®ä¸ã§ãnameãã£ã¼ã«ãã«é¢ããã¨ã©ã¼ãã§ãã¯ãè¡ããããªã¨ããããªç¨åº¦ã®ãã¨ã ã£ãããã¨ã©ã¼ãã§ãã¯ãè¡ã£ãå¾ã«å§ã¨åããã£ã¤ããã°è¯ããã§ããã©ããã£ã¨è¤éãªãã¼ã¿ãé£ãã§ããæãªã©ã«ãã¨ã©ã¼ãã§ãã¯åã«ã¨ã³ãã£ãã£ã§ãã¡ããã¡ããããã便å©ããªã¼ã£ã¦ã
ãããããã¨ã£ã¦ããã£ã±ãç¡çãªãã§ããããã
åã®ä¸ã§ã¯ãä»ãã¨ãç¡çã£ã¦ããçµè«ãåºã¦ãã®ã§ããããããªé¢¨ã«ã¨ã³ãã£ãã£ã®ã¡ã½ããã使ããããªæã¯ãç´æ¥ã³ã³ããã¼ã©ã¼ããããªãã¼ã·ã§ã³ã¡ã½ãããå¼ã³åºãããæ¹ã使ã£ã¦ã¾ããã¾ãnewEntityã§ã¨ã³ãã£ãã£ãä½æãã¦ããã®ãã¼ã¿ã使ã£ã¦ã¨ã©ã¼ãã§ãã¯ãããã¿ãããªã
newEntity()ã¨ãpatchEntity()ã£ã¦ãã¨ã³ãã£ãã£ãä½æããã¡ã½ããã ãããã©ã£ãã§ã¨ã³ãã£ãã£ãªãã¸ã§ã¯ãã¨ããåã渡ããããªå¦çãå ¥ã£ã¦ã¦ããã£ã¡ã§ããããåãã°ãããã使ããã¨ããã£ã¦ãè¯ããããªãã§ããã©ããåã調ã¹ãéãã§ã¯ãè¦å½ããã¾ããã§ããã
ã¨ãããã¨ã§ãããªãã¼ã·ã§ã³ã«é¢ããã話ã§ããã¼ãããã¾ã§èªãã§ãããæ¹ãæ¬å½ã«ãç²ããã¾ã§ããã§ãããããããé·ããªãã ããã¨ããäºæ³ã¯æåãããã£ãã®ã§ãç¹ã«åçã¯ãã¦ãã¾ããããã¿ã¾ãããã¦ã¸ãºãã§ããããã
åãããã£ãæãã§ã¯ããã«ããã¼ã®é ç®ã§æ¸ãã¦ãããã«ããã¼ã¿ãã¼ã¹ã¸ã®æ¥ç¶ã使ã£ãã¨ã©ã¼ãã§ãã¯ã¨ããã¨ã³ãã£ãã£ã¡ã½ããã使ã£ãã¨ã©ã¼ãã§ãã¯ã¨ããããããªãã¿ã¼ã³ãå¿ è¦ã«ãªãã¾ããããããããã«ã¹ã¿ã ããªãã¼ã·ã§ã³ãä½ã£ããããã¼ãã«ã«ç´æ¥æ¸ããããã³ã³ããã¼ã©ã¼ããç´æ¥å¼ã³åºããããªã¡ã½ããæ¸ãããã¨ãçµ±ä¸æã®ãªãã³ã¼ãã«ãªã£ã¦ãã¾ãã¾ããã
CustomValidation.phpã«ãã£ããã¾ã¨ããããã°ãCakePHP3ãã¸ã¹ã²ã¼ã£ã¦ãªã£ã¦ããã§ããã©ããæ®å¿µãªããããã¯ãããã§ãããã¶ã£ã¡ããããªãã¼ã·ã§ã³ã«é¢ãã¦ã¯ãBehaviorä¸ã¤ã«ã¾ã¨ãããã¨ãã§ããCakePHP2ã®æ¹ããåå人ã¨ãã¦ã¯ä½¿ãåæãè¯ãã£ããããªâ¦â¦ï¼
ãããã¡ããCakePHP3ã®ä»æ§ãæªãããããªãã¦ãåã®èããä»æ§ããè¨è¨ãæªãå¯è½æ§ãããããã«ãããã§ããã©ãããããã風ã«ãªããªãããã«è¨è¨ãèããã¹ããªãã ã£ã¦è¨ããã¦ãã¾ãã°ãããã®é³ãããããåºãªãã§ããã©ãããã±ãã®é³ãåºãããªãã¡ããã®é³(ry
ã§ãå®éã®ã¨ãããã¨ã³ãã£ãã£ã使ã£ãã¨ã©ã¼ãã§ãã¯ã¨ãè¡ããã¿ã¼ã³ã£ã¦ãååããå¾ãã¨æããã ãã©ãªãâ¦â¦ã©ãã£ãããï¼
ãã®ä»ã®CakePHP3ã触ã£ã¦ã¿ã¾ããã®è¨äºã¯ãã¡ã
ã¾ã¨ãã¨ããåã®ç®¸ä¼ã
ãããªããã§ä»æ¥ã¯ã誰ãå©ãã¦ãã¨ããã¡ãã»ã¼ã¸ãè¾¼ãã¤ã¤ãããªãã¼ã·ã§ã³ã®ä½¿ãæ¹ãè¦ã¦è¡ãããã¨æãã¾ãã
æ£ç´ãããã®ãããé·ãè¨äºã«ãªããã¨ãäºæ³ããã¾ãããå¿ è¦ãªã¨ãã ãæ¾ã£ã¦èªãã§ãããããã¨æãã¾ãã
å 訳ã¯ãããªæãã§ãã
- ã¨ã©ã¼ãã§ãã¯ã®æ¹æ³
- åºæ¬çãªããªãã¼ã·ã§ã³
- ã¢ãã«ããã¼ãã«ã«ç´æ¥æ¸ã
- ãã«ããã¼ãã®1
- ã«ã¹ã¿ã ãã¡ã¤ã«ãä½ã
- ãã«ããã¼ãã®2
- ã³ã³ããã¼ã©ã¼ããå¼ã³åºã
- ãã«ããã¼ãã®3
ã§ã¯é·ä¸å ´ã®æ ã«ããã£ã¤ããã¼ã
ã¨ã©ã¼ãã§ãã¯ã®æ¹æ³
ã¾ãã¯ãã¨ã©ã¼ãã§ãã¯ãã©ããã£ã¦è¡ããããç°¡åã«ããã£ã¨è¦ã¦ã¿ã¾ããã¼ãããã§ã¯ãusersã¨ãããã¼ãã«ã«ãååã¨ã¡ã¼ã«ã¢ãã¬ã¹ãå ¥åããç»é¢ãç¨æãã¦ã¿ã¾ããCakePHPã®Formãã«ãã¼ã使ã£ã¦ãäºã¤ã®ããã¹ãããã¯ã¹ãçæãã¾ããã¢ãã«åã¯Userã
<?php echo $this->Form->create('User') ?>
<?php echo $this->Form->text('name') ?>
<?php echo $this->Form->text('email') ?>
<?php echo $this->Form->end() ?>
ãã£ããæ¸ãã¨ããããªæãã§ãããã
CakePHP2ã§ã¨ã©ã¼ãã§ãã¯ãè¡ãã«ã¯ãvalidates()ã¨ããã¡ã½ããã使ãã¾ãã
$this->User->create($this->request->data);
if($this->User->validates()) {
//ã¨ã©ã¼ãªã
} else {
//ã¨ã©ã¼ãã
}
CakePHP3ã§ã¯ãã¨ã³ãã£ãã£ãä½æããéã«ãã¨ã©ã¼ãã§ãã¯ãè¡ããã¨ãã§ãã¾ãã
$user = $this->Users->newEntity($this->request->data);
if(!$user->errors()) {
//ã¨ã©ã¼ãªã
} else {
//ã¨ã©ã¼ãã
}
ç´°ãã説æã«ã¤ãã¦ã¯ãä»æ¥ã¯çç¥ãã¾ããããããã°ã¨ã©ã¼ãã§ãã¯ãã§ããã£ã¦ãã¨ã ããæ¼ããã¨ãããããã
newEntity()ã¨ãã®åãã«ã¤ãã¦ã¯ã以åã«ãè¨äºãæ¸ãããã¨ãããã®ã§ãããåèã«ãªãããã§ããã°ããã¡ããã©ã¼ããã¨ã©ã¼ãã§ãã¯ã«ã¤ãã¦ãå°ã触ãã¦ã¾ãã
CakePHP3ã触ã£ã¦ã¿ã¾ããããcreateãsetãã§ããªããã§ããã©ï¼ã
åºæ¬çãªããªãã¼ã·ã§ã³
ã»ãããããããªãã¼ã·ã§ã³ã®ä¸çªåºæ¬çãªæ¸ãæ¹ããããããç¨æããã¦ããã¡ã½ããã使ã£ãããæ¹ãããä»åã¯ãååãã¡ã¼ã«ã¢ãã¬ã¹ãå¿ é å ¥åã§ããã¨ããåæã§ããã¾ããã¤ã¾ãã©ã¡ããæªå ¥åã ã£ããã¨ã©ã¼ã«ãªããã¤ãã§ã«ã¡ã¼ã«ã¢ãã¬ã¹ã«é¢ãã¦ã¯ãå½¢å¼ã®ã¨ã©ã¼ãã§ãã¯ãè¡ã£ã¦ã¿ã¾ãããã
CakePHP2ã ã¨ããããªãã¾ãã
//User.php
class User extends AppModel {
public $validate = array(
'name' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'å¿
é å
¥åã§ã',
),
),
'email' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'å¿
é å
¥åã§ã',
),
'email' => array(
'rule' => 'email',
'message' => 'å½¢å¼ãä¸æ£ã§ã',
),
),
);
}
ãruleããã¨ã©ã¼ãã§ãã¯ã®ã¡ã½ããããmessageããã¨ã©ã¼ã¡ãã»ã¼ã¸ã«ãªãã¾ãã
ãnotEmptyãããemailãã¨ããã®ã¯ããããããç¨æããã¦ããã¡ã½ããã§ããããã§ãvalidates()ã¡ã½ãããå¼ã³åºããã¨ãã«ãå¿ é å ¥åãã¡ã¼ã«ã¢ãã¬ã¹ã®å½¢å¼ã«é¢ããã¨ã©ã¼ãã§ãã¯ãè¡ããã¨ãã§ãã¾ãã
ä»ã«ãã©ããªã¡ã½ããããããã¯ãã³ã¢ã©ã¤ãã©ãªã®ãUtilityãã¨ãããã©ã«ãã®ä¸ã«ãValidation.phpãã¨ãããã¡ã¤ã«ãããã®ã§ããã®ä¸ãè¦ã¦ãã ããã
ä¸è¨ã®ããã«æ¸ããå ´åãã¨ã©ã¼ãããã¨ããã¥ã¼ã®ã$this->validationErrorsãã¨ããå¤æ°ã«ã¨ã©ã¼ã¡ãã»ã¼ã¸ãå ¥ã£ã¦ãã¾ãã
//$this->validationErrors
array(
[name] => array(
[0] => 'å¿
é å
¥åã§ã'
)
[email] => array(
[0] => 'å½¢å¼ãä¸æ£ã§ã'
)
)
ç¶ãã¦CakePHP3ã
namespace App\Model\Table;
use Cake\Validation\Validator;
class UsersTable extends Table {
public function validationDefault(Validator $validator) {
$validator->notEmpty('name', 'å¿
é å
¥åã§ã');
ã $validator->notEmpty('email', 'ã¡ã¼ã«ã¢ãã¬ã¹ãæªå
¥åã§ã');
$validator->add('email', 'email', [
'rule' => 'email',
'message' => 'å½¢å¼ãä¸æ£ã§ã'
]);
return $validator;
}
}
CakePHP2ã§ã¯ã$validateã¨ããã¡ã³ãå¤æ°ã«å¿ è¦ãªå 容ãæ¸ãã¾ããããCakePHP3ã§ã¯ãvalidation***ãã¨ããã¡ã½ããããã¼ãã«ã«ä½ãã¾ãã
ä¸è¨ã®ä¾ã§ã¯ãvalidationDefault()ãã¨ãªã£ã¦ãã¾ãããã¡ã½ããåã¯ä»»æã«å¤ãããã¨ãå¯è½ã§ãããvalidationOyado()ãã§ããvalidationOyaki()ãã§ããvalidationOyakata()ãã§ãä½ã§ãè¯ãã
ãã ãDefault以å¤ã«ããå ´åã¯ãnewEntity()ãpatchEntity()ã§ãã®ã¡ã½ãããå¼ã³åºãããã®è¨å®ãå¿ è¦ã«ãªãã
//validationOyado()ã«ããå ´å
$this->Users->newEntity($this->request->data, ['validate' => 'oyado']);
//validationOyaki()ã«ããå ´å
$this->Users->newEntity($this->request->data, ['validate' => 'oyaki']);
//validationOyakata()ã«ããå ´å
$this->Users->newEntity($this->request->data, ['validate' => 'oyakata']);
第äºå¼æ°ï¼patchEntity()ã®å ´åã¯ç¬¬ä¸å¼æ°ï¼ã«ããvalidation***ãã®***ã®é¨åãè¨è¿°ãã¾ããå é ã¯å°æåã§ãkã
ãã®ãvalidate => ‘***’ããçç¥ããå ´åã¯ãvalidationDefault()ãå¼ã°ãã¾ãã
CakePHP3ã§ã¯ãåºæ¬çã«ã¯$validatorã®add()ã¡ã½ããã使ã£ã¦ãã«ã¼ã«ã追å ãã¦ããã¾ããã§ãnotEmptyã¯ããã®ã¾ã¾notEmpty()ã¨ããã¡ã½ããã使ãã¿ãããä½ã§ããªï¼
CakePHP3ã§ãããããç¨æããã¦ããã¡ã½ããã¯ãã³ã¢ã©ã¤ãã©ãªã®ãValidationãã¨ãããã©ã«ãã®ä¸ã«ãããValidation.phpãã§ç¢ºèªã§ãã¾ãã
ã¨ã©ã¼ãããã¨ãåé ã§ãæ¸ãã¾ããããã$user->errors()ãã§ã¨ã©ã¼ã¡ãã»ã¼ã¸ãåå¾ã§ãã¾ãã
//$user->erros()
array(
[name] => array(
[_empty] => å¿
é å
¥åã§ã
)
[email] => array(
[email] => å½¢å¼ãä¸æ£ã§ã
)
)
notEmpty()ã®å ´åã¯ã_emptyãã¨ãããã¼ï¼ãã¶ãåºå®ï¼ã«ã¡ãã»ã¼ã¸ãå ¥ãã¾ããadd()ã§è¿½å ããå ´åã¯ãèªåã§ãã¼ãæå®ã§ãã¾ãã
$validator->add('ãã£ã¼ã«ã', 'ãã¼', [
'rule' => 'email',
'message' => 'ã¡ãã»ã¼ã¸'
]);
//$user->erros()
array(
[ãã£ã¼ã«ã] => array(
[ãã¼] => ã¡ãã»ã¼ã¸
)
)
è¨å®æ¬¡ç¬¬ã§ãã好ã¿ã®ãã¼ã«ã§ãã¾ããokonomiyakiã¨ãã«ãã§ãã¾ãã
ã¢ãã«ããã¼ãã«ã«ç´æ¥æ¸ã
åºæ¬ã ãã§ãã ãã¶é·ããªã£ã¦ãã¾ãã¾ããããã§ããã£ããããã«é·ãã®ãç¶ãã¾ããã¼ã次ã¯ãã¨ã©ã¼å¤å®ç¨ã®ã¡ã½ãããèªä½ããå ´åã§ããä¾ãã°ãããã£ããâ¦â¦ååã«ã²ãããªãã使ããªããã¿ãããªã«ã¼ã«ãèªä½ãã¦ã¿ã¾ããããããã©ã¯ã¨ã£ã½ãã§ããããããã«ã¼ã«åããdraqueãã§è¡ãã¾ãããã
ããæ¹ã¯å¤§ããåãã¦äºã¤ããã¾ãã
ä¸ã¤ã¯ãã¢ãã«ããã¼ãã«ã«ç´æ¥ã¡ã½ãããæ¸ãããæ¹ã
CakePHP2ã®å ´åã¯ãã¢ãã«ã«æ¸ãã¾ãã
//User.php
class User extends AppModel{
public $validate = array(
'name' => array(
'rule' => 'draque',
'message' => 'ã²ãããªã§å
¥åãã¦ãã ãã',
),
);
public function draque($data) {
$check = is_array($data) ? array_shift($data) : $data;
return preg_match("/^[ã-ã]+$/u", $check);
}
}
ãã£ãããããªæããä½ã§draque()ã®ä¸ã§array_shift()ãã¦ããã£ã¦ããã¨ãä¸è¨ã®ããã«ã¨ã©ã¼ãã§ãã¯ãè¡ã£ãå ´åã$dataã®ä¸èº«ã¯ãããªã£ã¦ãããã§ãã
[name] => å
¥åãããæåå
ã$data[‘name’]ãã¨ãã¦ãè¯ããã ãã©ãããã ã¨ãã£ã¼ã«ãåãnameã®ã¨ããã対å¿ã§ããªããããä¸å¿ãããªé¢¨ã«æ¸ãã¦ã¾ãã
ãã¨ã¯æ£è¦è¡¨ç¾ã§ãå ¥åãããæååãã²ãããªã ãã ã£ãããtrueãè¿ãããã«ãã¦ã¾ãã
ã¡ãªã¿ã«ãä½ãããã®å¤ãä¸ç·ã«æ¸¡ãããå ´åã
//User.php
class User extends AppModel{
public $validate = array(
'name' => array(
'rule' => array('draque', 'ãããã', 'ãããã¨')
'message' => 'ã²ãããªã§å
¥åãã¦ãã ãã',
),
);
public function draque($data, $value1, $value2) {
$check = is_array($data) ? array_shift($data) : $data;
return preg_match("/^[ã-ã]+$/u", $check);
}
}
ruleã®ã¨ãããé åã§æ¸ããã¨ã«ãã£ã¦ãå¼æ°ã«å¤ã渡ãã¾ãããã®å ´åã¯ããããããã¨ããããã¨ããããããã$value1ã¨$value2ã«å ¥ãã¾ãã
CakePHP3ã®å ´åã¯ããã¼ãã«ã«æ¸ãã¾ãã
//UsersTable.php
class UsersTable extends Table {
public function validationDefault(Validator $validator) {
$validator->add('name', 'draque', [
'rule' => [$this, 'draque'],
'message' => 'ã²ãããªã§å
¥åãã¦ãã ãã',
]);
return $validator;
}
public function draque($value) {
return (bool) preg_match('/^[ã-ã]+$/u', $value);
}
}
åºæ¬ã®æ¸ãæ¹ã¨ã¡ãã£ã¨éãã®ã¯ãruleã®ã¨ããã§ããã[$this, ã¡ã½ããå]ã¨ããæ¸ãæ¹ã«ãªã£ã¦ãã¾ãã
CakePHP3ã®å ´åã¯ã$valueã«å ¥åãããå¤ãå ¥ã£ã¦ãã¾ãã®ã§ãarray_shift()çãªå¦çã¯ãããªãã§ãããã®ã¾ã¾ä½¿ãã¾ãããã¨è¿ãå¤ã¯ãbooleanã«ããªãã¨ä¸æãåããªãã£ã½ãã
ruleã®ä¸ã«ãç´æ¥ã¡ã½ãããæ¸ããã¨ãã§ãã¾ãã
//UsersTable.php
class UsersTable extends Table {
public function validationDefault(Validator $validator) {
$validator->add('name', 'draque', [
'rule' => function draque($value) {
return (bool) preg_match('/^[ã-ã]+$/u', $value);
},
'message' => 'ã²ãããªã§å
¥åãã¦ãã ãã',
]);
return $validator;
}
}
çµæã¯ä¸ç·ã§ãã
ãã«ããã¼ãã®1
ã§ã¯ããã§ã第ä¸ã®ãã«ããã¼ãã¤ã³ããCakePHP3ã®æ¹ã§ãã£ãå ´åããããããããããããã¨ãã渡ãæ¹æ³ãåããã¾ããã§ããã
//UsersTable.php
class UsersTable extends Table {
public function validationDefault(Validator $validator) {
$validator->add('name', 'draque', [
'rule' => [$this, 'draque', 'ãããã¨'],
'message' => 'ã²ãããªã§å
¥åãã¦ãã ãã',
]);
return $validator;
}
public function draque($value, $value2) {
return (bool) preg_match('/^[ã-ã]+$/u', $value);
}
}
試ãã«ãããªé¢¨ã«æ¸ãã¦ã¿ããããããã ãã©ãã¨ã©ã¼ã«ãªã£ããããªãã¼ã·ã§ã³ã®ã¨ã©ã¼ã§ã¯ãªãã¦ãã·ã¹ãã ã¨ã©ã¼ãã
ããããæ¸ãæ¹ã¯ã§ããªãã¿ããã§ããããä½ããããã¤ã¬ã¯ãã«å¤ã渡ãããã£ã¦ãªã£ãå ´åã¯ãã©ãããã°è¯ãã®ããªï¼
ã«ã¹ã¿ã ãã¡ã¤ã«ãä½ã
ããä¸ã¤ã¯ãã¢ãã«ããã¼ãã«ã¨ã¯å¥ã®ãã¡ã¤ã«ã«æ¸ãå ´åãCakePHP2ã®å ´åã¯ãBehaviorã使ã£ã¦æ¸ããã¨ãªãããã§ãã¾ãã
ä¾ãã°ãValidateBehavior.phpã¨ãããã¡ã¤ã«ãä½ã£ã¦ã¿ã¾ãããããã¡ã¤ã«ã¯ãModelãã©ã«ãã®ä¸ã«Behaviorã¨ãããã©ã«ããããã®ã§ããã®ä¸ã«ä½æãã¾ãããªããã°Behaviorãã©ã«ããä½ã£ã¦ãã ããã
//ValidateBehavior.php
class ValidateBehavior extends ModelBehavior {
public function draque(Model $model, $data) {
$check = is_array($data) ? array_shift($data) : $data;
return preg_match('/^[ã-ã]+$/u', $check);
}
}
//User.php
class User extends AppModel {
public $actAs = array('Validate');
public $validate = array(
'name' => array(
'rule' => 'draque',
'message' => 'ã²ãããªã§å
¥åãã¦ãã ãã',
),
);
}
Behaviorãèªã¿è¾¼ãã«ã¯ã$actAsã¨ããã¡ã³ãå¤æ°ã使ãã¾ãã
ã¢ãã«ã«ç´æ¥æ¸ãå ´åã¨ã»ã¨ãã©ä¸ç·ã§ãããBehaviorã«æ¸ããå ´åã第ä¸å¼æ°ã«ã¯ã¢ãã«ã®ã¤ã³ã¹ã¿ã³ã¹ï¼ã§è¯ãã®ããªï¼ï¼ã渡ãã¦ãã¾ãã$dataã¯ç¬¬äºå¼æ°ããããã¨ã渡ããããã°ããã®å¾ãã«å¼æ°ã追å ããã°ãªã¼ã±ã¼ã§ãã
CakePHP3ã®å ´åã¯ãã«ã¹ã¿ã ããªãã¼ã·ã§ã³ã¨ãããã®ãä½æãããã¨ãã§ãã¾ãã
Modelãã©ã«ãã®ä¸ã«ãValidationãã¨ãããã©ã«ããä½ãããã®ä¸ã«ããªãã¼ã·ã§ã³ãã¡ã¤ã«ãä½æãã¾ããããã§ã¯ãCustomValidation.phpãã¨ãããã¡ã¤ã«ãä½ã£ã¦ã¿ã¾ãã絶対ã«ãModel/Validationãã®ä¸ã«ä½ããªãã¨ãããªãããã§ã¯ãªãã§ãããããã«ã¤ãã¦ã¯å¾ã§ã
//CustomValidation.php
namespace App\Model\Validation;
use Cake\Validation\Validation;
class CustomValidation extends Validation {
//staticãå¿ããªãããã«
public static function draque($value) {
return (bool) preg_match('/^[ã-ã]+$/u', $value);
}
}
//UsersTable.php
class UsersTable extends Table {
public function validationDefault(Validator $validator) {
$validator->provider('custom', 'App\Model\Validation\CustomValidation');
$validator->add('name', 'draque', [
'rule' => 'draque',
'provider' => 'custom',
'message' => 'ã²ãããªã§å
¥åãã¦ãã ãã',
]);
return $validator;
}
}
CustomValidationã使ããããã«ããã«ã¯ãprovider()ã¨ããã¡ã½ãããå¿ è¦ã«ãªãã¾ãã第ä¸å¼æ°ã®ãcustomãã¯ä½ã§ãè¯ãã§ããadd()ã¡ã½ããã®ä¸ã§ã’provider’ => ‘custom’ãã¨ããã®ãããã¾ãããããã«ä½¿ç¨ããæååãæå®ãã¦ããã ããªã®ã§ãcustomã®ä»£ããã«gundamã¨ãã«ããããã’provider’ => ‘gundam’ãã¨ããã°è¯ãã ãã
第äºå¼æ°ã§ãCustomValidationããããã¹ãæå®ãããã¨namespaceããæ£ããå ´æãæãã¦ããã°ãCustomValidation.phpã¯ã©ãã«ãã£ã¦ãè¯ãã£ã½ãã§ããsrcãã©ã«ãã®ç´ä¸ã«ç½®ããããããæ¸ãã°åé¡ãªãåãã
//CustomValidation.php
namespace App;
class CustomValidation extends Validation {
}
//UsersTable.php
class UsersTable extends Table {
public function validationDefault(Validator $validator) {
$validator->provider('custom', 'App\CustomValidation');
}
}
ã«ã¹ã¿ã ã使ãå ´åã¯ãproviderã®æå®ãå¿ããªãã§ãã ããããããå¿ãã¡ããã¨ã¨ã©ã¼ã«ãªã£ã¡ããã
ãã¨ããä¸ã¤æ°ãã¤ããªãããããªãã®ããCustomValidation.phpã®ä¸ã«ã¡ã½ãããæ¸ãå ´åã¯ãstaticã§ãªããã°ãããªãã¨ãããã¨ã§ããstaticãæ¸ãå¿ãã¦ãã¨ããã£ã±ãã¨ã©ã¼ã«ãªã£ã¡ããã¾ãã
ãã«ããã¼ãã®2
ãã¦ãã§ã¯ããã§ã第äºã®ãã«ããã¼ãã¤ã³ãããã¼ãã«ã«ç´æ¥æ¸ãå ´åãã«ã¹ã¿ã ãã¡ã¤ã«ãä½ãå ´åãã第äºå¼æ°ã«ã³ã³ããã¹ããã¼ã¿ã£ã¦ã®ãå ¥ã£ã¦ãã¾ããçç¥ãã¦æ¸ããã¨ãã§ããã®ã§ãä¸è¨ã®ä¾ã§ã¯æ¸ãã¦ã¾ããããã³ã³ããã¹ããã¼ã¿ãåå¾ããããã°ãã¡ã½ããã®ç¬¬äºå¼æ°ãæ¸ãã¦ãã ããã
public function draque($value, $context) {
return (bool) preg_match('/^[ã-ã]+$/u', $value);
}
$contextã®ä¸ã«ã¯ãããã§ã¯æ¸ããããªããããã®ããããªãã¼ã¿ãå ¥ã£ã¦ã¾ãããã¼ãã«ãªãã¸ã§ã¯ããå ¥ã£ã¦ãã
ãã¼ãã«ãªãã¸ã§ã¯ãããããªãããã使ã£ã¦ããããã¨å¦çãè¡ãã¦ãè¯ããããªãã®ãªãã§ããã©ãå¤æ°ãprotectedã«ãªã£ã¦ãããããã«ã¹ã¿ã ã®æ¹ã§ã¯ä½¿ããªããã®ããããã§ããã
ãã¼ã¨ãä¾ãã°ã¡ã¼ã«ã¢ãã¬ã¹ã®éè¤ç»é²ãé¿ãããããªå ´åãå ¥åãããã¢ãã¬ã¹ããã§ã«ãã¼ã¿ãã¼ã¹ã®ä¸ã«ãããã©ããããã§ãã¯ããããããªãã§ããã
//UsersTable.php
class UsersTable extends Table {
public function validationDefault(Validator $validator) {
$validator->add('email', 'exist', [
'rule' => [$this, 'exist'],
'message' => 'ãã§ã«ç»é²ããã¦ãã¾ã',
]);
return $validator;
}
public function exist($value, $context) {
$table = $context['providers']['table'];
$query = $table->find();
$query->where([$context['field'] => $value]);
$count = $query->count();
return (bool) $count == 0;
}
}
ä¾ãã°ãããªãã¨ãã§ãããã§ããããã£ã¦ããã¨ã¯ãå ¥åãããã¡ã¼ã«ã¢ãã¬ã¹ãDBã®ä¸ã«ãããã©ããå¤å®ãã¦ã1件以ä¸ãã£ãããéè¤ã¨ã©ã¼ãåºãã¨ãããã®ãããããã¨èª¬æã¯çç¥ãã¡ããã¾ãããã$context[‘providers’][‘table’]ãã«ãã¼ãã«ãªãã¸ã§ã¯ããå ¥ã£ã¦ãã®ã§ããã使ãã°ããã¼ã¿ãã¼ã¹ã«ã¢ã¯ã»ã¹ãããã§ãã¾ããCustomValidation.phpã®æ¹ã«æ¸ãã¦ãåãããã«åãã¾ãã
æ°è¦ç»é²æã¯ããã§è¯ãã¨æãã¾ããã§ããã¼ã¿æ´æ°æã¯ãèªèº«ã®ãã¼ã¿ãåç §ãã¦ãã¾ãã¨ãå¿ ã$countã1以ä¸ã«ãªã£ã¡ããã¾ããã ããèªåã®IDã ãã¯é¤ããããªå¦çãå¿ è¦ã«ãªãã¨æããã§ããã
//UsersTable.php
class UsersTable extends Table {
public function validationDefault(Validator $validator) {
$validator->add('email', 'exist', [
'rule' => [$this, 'exist'],
'message' => 'ãã§ã«ç»é²ããã¦ãã¾ã',
]);
return $validator;
}
public function exist($value, $context) {
$table = $context['providers']['table'];
$query = $table->find();
$query->where([$context['field'] => $value]);
//èªèº«ã®IDãé¤å¤
if(!empty($context['data'][$table->_primaryKey])) {
$query->where([$table->_primaryKey.' !=' => $context['data'][$table->_primaryKey]]);
}
$count = $query->count();
return (bool) $count == 0;
}
}
ä¾ãã°ãããªé¢¨ã«æ¸ããã¨ãã¾ããã$table->_primaryKeyãã«ã¯ããã©ã¤ããªã¼ãã¼ãå ¥ã£ã¦ã¾ãããã©ã¤ããªã¼ãã¼ã絶対ã«ãidãã§åºå®ã¨ããªãè¯ããã§ããã©ãid以å¤ã«ãããããããªãäºæ ãæ³å®ãã¦ãããã§ã¯ãã¼ãã«ãªãã¸ã§ã¯ããã¼ã¿ã®ä¸ããåç §ã§ããããã«æ¸ãã¦ãã¾ãã
ãã®æããã¼ãã«ã«ç´æ¥æ¸ããå ´åã¯ã¨ã©ã¼ã«ãªããªããã§ããã©ãCustomValidation.phpã®æ¹ã«æ¸ãã¨ããã©ã¤ããªã¼ãã¼ãåãã¾ãããã¨ã©ã¼ã«ãªãã¾ãã
ã ããæ¸ãæ¹æ¬¡ç¬¬ã§ã¯ããã£ããCustomValidation.phpãä½ã£ã¦ãç¬èªã¡ã½ãããããã§ä¸æ¬ç®¡çãããã¨æã£ã¦ããã®ã«ããã¼ãã«ã®æ¹ã«ãæ¸ããªãããããªããªã£ãããããã¨ãããã£ãããªãã£ããï¼
ããããè¨è¨ããããªãã£ã¦è¨ããã¡ããã°ããã¾ã§ãªãã§ããã©â¦â¦ããããæã¯ã©ããããè¯ããã§ããããï¼
ã³ã³ããã¼ã©ã¼ããå¼ã³åºã
CakePHP3ã®ã¿ã®è©±ã«ãªãã¾ãããããªãã¼ã·ã§ã³ã¯ãã³ã³ããã¼ã©ã¼ããç´æ¥ä½¿ããã¨ãã§ããããã£ãã®draqueã¡ã½ããã使ã£ãã¨ã©ã¼ãã§ãã¯ããã³ã³ããã¼ã©ã¼ãã使ã£ã¦ã¿ã¾ãããã
//UsersController.php
class UsersController extends AppController {
public function add() {
if($this->request->is('post')) {
$user = $this->Users->newEntity($this->request->data);
//ã¨ã©ã¼ãã§ãã¯ã¡ã½ããã®å¼ã³åºã
$errors = $this->Users->validateDraque($user);
if(!$errors) {
//ã¨ã©ã¼ãªã
} else {
//ã¨ã©ã¼ãã
}
}
}
}
//UsersTable.php
use Cake\Validation\Validator;
class UsersTable extends Table {
public function validateDraque($user) {
$validator = new Validator();
$validator->add('name', 'draque', [
'rule' => function draque($value) {
return (bool) preg_match('/^[ã-ã]+$/u', $value);
},
'message' => 'ã²ãããªã§å
¥åãã¦ãã ãã',
]);
//ã¨ã©ã¼ãã§ãã¯
$errors = $validator->errors($user->toArray());
return $errors;
}
}
ã¨ã³ãã£ãã£ãä½æå¾ãã³ã³ããã¼ã©ã¼ã§ç´æ¥ããªãã¼ã·ã§ã³ã¡ã½ãããå¼ã³åºãã¦ãã¾ãããã£ã¦ããã¨ã¯ã»ã¨ãã©ä¸ç·ã§ãã$validatorã®ã¤ã³ã¹ã¿ã³ã¹ãæ°è¦ã«ä½æãã¦ãã«ã¼ã«ã®æ¸ãæ¹ã¯ä»ã¾ã§ã¨ä¸ç·ãæå¾ã«erros()ã¨ããã¡ã½ããã§ã¨ã©ã¼ãã§ãã¯ãè¡ã£ã¦ãã¾ããerrors()ã«ã¯ãã¨ã³ãã£ãã£ã®ãªãã¸ã§ã¯ããã¼ã¿ã§ã¯ãªããå ¥åããããã¼ã¿ã®é åã渡ãå¿ è¦ãããã®ã§ãtoArray()ã¡ã½ããã使ã£ã¦ããã¼ã¿ã ãã渡ãããã«ãã¦ãã¾ãã
ã¨ã©ã¼ãããã¨$errosã«ã¨ã©ã¼å 容ãå ¥ã£ã¦ããã®ã§ãããã使ã£ã¦ã¨ã©ã¼ã¡ãã»ã¼ã¸ã表示ããããç»é²å¦çãè¡ã£ããã§ãã¾ãã
ãã«ããã¼ãã®3
ããããæå¾ã®ãã«ããã¼ãå ¥åããããã¼ã¿ããã¨ã³ãã£ãã£ã使ã£ã¦å å·¥ãããã¨ããããããªãã§ãããã¨ã³ãã£ãã£ã«ã¡ã½ãããä½ã£ã¦ã
ããªãã¼ã·ã§ã³ã®ã¨ãã«ãã®ã¨ã³ãã£ãã£ã§ä½ã£ãã¡ã½ããã使ããããªã¼ã£ã¦æã£ããã¨ããããã§ããã©ãvalidationDefault()ã¨ãã®ä¸ã§ããã使ããªããã§ãããï¼
ä½ãè¨ã£ã¦ããã£ã¦ã¼ã¨ãä¾ãã°â¦â¦ãã¼ã¿ãã¼ã¹ã§ã¯ãååã¯nameã¨ããä¸ã¤ã®ãã£ã¼ã«ãã§æã£ã¦ããã ãã©ãå ¥åã¯å§ã¨åãåãã¦å ¥åãããããªå ´åãèãã¦ã¿ã¾ããããã§å ¥åããéã«å§ã¨åããã£ã¤ãã¦ããã«ãã¼ã ã«ãã¦ããã¨ã©ã¼ãã§ãã¯ãè¡ããã¿ãããªã
//ãã³ãã¬ã¼ã
<?= $this->Form->text('sei') ?>
<?= $this->Form->text('mei') ?>
//ã³ã³ããã¼ã©ã¼
$this->Users->newEntity($this->request->data);
//ãã¼ãã«
public function validationDefault(Validator $validator) {
$validator->add('name', 'draque', [
'rule' => function draque($value) {
return (bool) preg_match('/^[ã-ã]+$/u', $value);
},
'message' => 'ã²ãããªã§å
¥åãã¦ãã ãã',
]);
return $validator;
}
//ã¨ã³ãã£ãã£
public function fullName() {
$this->name = $this->sei.$this->mei;
}
ããããã¨ãã£ãã¼çç¥ãã¦æ¸ãã¡ããã¾ãããããããããããªæãã®ãã¨ãããããã¨ããã
ããã¯ãã¨ã³ãã£ãã£ã®fullName()ã£ã¦ããã¡ã½ããã使ã£ã¦ãã¨ã©ã¼ãã§ãã¯ãè¡ãåã«å§ã¨åãåãããå¤ãnameã«å ¥ãããããªããããªãã¨ãã§ãããè¯ããªã¨ããã®ä¸ã§ãnameãã£ã¼ã«ãã«é¢ããã¨ã©ã¼ãã§ãã¯ãè¡ããããªã¨ããããªç¨åº¦ã®ãã¨ã ã£ãããã¨ã©ã¼ãã§ãã¯ãè¡ã£ãå¾ã«å§ã¨åããã£ã¤ããã°è¯ããã§ããã©ããã£ã¨è¤éãªãã¼ã¿ãé£ãã§ããæãªã©ã«ãã¨ã©ã¼ãã§ãã¯åã«ã¨ã³ãã£ãã£ã§ãã¡ããã¡ããããã便å©ããªã¼ã£ã¦ã
ãããããã¨ã£ã¦ããã£ã±ãç¡çãªãã§ããããã
åã®ä¸ã§ã¯ãä»ãã¨ãç¡çã£ã¦ããçµè«ãåºã¦ãã®ã§ããããããªé¢¨ã«ã¨ã³ãã£ãã£ã®ã¡ã½ããã使ããããªæã¯ãç´æ¥ã³ã³ããã¼ã©ã¼ããããªãã¼ã·ã§ã³ã¡ã½ãããå¼ã³åºãããæ¹ã使ã£ã¦ã¾ããã¾ãnewEntityã§ã¨ã³ãã£ãã£ãä½æãã¦ããã®ãã¼ã¿ã使ã£ã¦ã¨ã©ã¼ãã§ãã¯ãããã¿ãããªã
newEntity()ã¨ãpatchEntity()ã£ã¦ãã¨ã³ãã£ãã£ãä½æããã¡ã½ããã ãããã©ã£ãã§ã¨ã³ãã£ãã£ãªãã¸ã§ã¯ãã¨ããåã渡ããããªå¦çãå ¥ã£ã¦ã¦ããã£ã¡ã§ããããåãã°ãããã使ããã¨ããã£ã¦ãè¯ããããªãã§ããã©ããåã調ã¹ãéãã§ã¯ãè¦å½ããã¾ããã§ããã
ã¨ãããã¨ã§ãããªãã¼ã·ã§ã³ã«é¢ããã話ã§ããã¼ãããã¾ã§èªãã§ãããæ¹ãæ¬å½ã«ãç²ããã¾ã§ããã§ãããããããé·ããªãã ããã¨ããäºæ³ã¯æåãããã£ãã®ã§ãç¹ã«åçã¯ãã¦ãã¾ããããã¿ã¾ãããã¦ã¸ãºãã§ããããã
åãããã£ãæãã§ã¯ããã«ããã¼ã®é ç®ã§æ¸ãã¦ãããã«ããã¼ã¿ãã¼ã¹ã¸ã®æ¥ç¶ã使ã£ãã¨ã©ã¼ãã§ãã¯ã¨ããã¨ã³ãã£ãã£ã¡ã½ããã使ã£ãã¨ã©ã¼ãã§ãã¯ã¨ããããããªãã¿ã¼ã³ãå¿ è¦ã«ãªãã¾ããããããããã«ã¹ã¿ã ããªãã¼ã·ã§ã³ãä½ã£ããããã¼ãã«ã«ç´æ¥æ¸ããããã³ã³ããã¼ã©ã¼ããç´æ¥å¼ã³åºããããªã¡ã½ããæ¸ãããã¨ãçµ±ä¸æã®ãªãã³ã¼ãã«ãªã£ã¦ãã¾ãã¾ããã
CustomValidation.phpã«ãã£ããã¾ã¨ããããã°ãCakePHP3ãã¸ã¹ã²ã¼ã£ã¦ãªã£ã¦ããã§ããã©ããæ®å¿µãªããããã¯ãããã§ãããã¶ã£ã¡ããããªãã¼ã·ã§ã³ã«é¢ãã¦ã¯ãBehaviorä¸ã¤ã«ã¾ã¨ãããã¨ãã§ããCakePHP2ã®æ¹ããåå人ã¨ãã¦ã¯ä½¿ãåæãè¯ãã£ããããªâ¦â¦ï¼
ãããã¡ããCakePHP3ã®ä»æ§ãæªãããããªãã¦ãåã®èããä»æ§ããè¨è¨ãæªãå¯è½æ§ãããããã«ãããã§ããã©ãããããã風ã«ãªããªãããã«è¨è¨ãèããã¹ããªãã ã£ã¦è¨ããã¦ãã¾ãã°ãããã®é³ãããããåºãªãã§ããã©ãããã±ãã®é³ãåºãããªãã¡ããã®é³(ry
ã§ãå®éã®ã¨ãããã¨ã³ãã£ãã£ã使ã£ãã¨ã©ã¼ãã§ãã¯ã¨ãè¡ããã¿ã¼ã³ã£ã¦ãååããå¾ãã¨æããã ãã©ãªãâ¦â¦ã©ãã£ãããï¼
ãã®ä»ã®CakePHP3ã触ã£ã¦ã¿ã¾ããã®è¨äºã¯ãã¡ã
ã¾ã¨ãã¨ããåã®ç®¸ä¼ã