public function zipcode($attribute, $value, $parameters, $validator) { $value = $this->filter->_getZipcode($value); return Import::DEFAULT_ZIPCODE === $value || NULL !== State::findByZipcode($value)->first(); }
/** * 判斷區碼是否合法 * * 條件: * * 1. 長度 * 2. 可否從 State 找到符合的區 * * 若合法,直接在這邊將取得的 State 存入屬性中, * 並且回傳 true * * 注意: * * 一定要確保 * * @param string $zipcode * @return boolean */ public function _isValidZipcode($zipcode, $address) { if (Import::MINLENGTH_ZIPCODE !== strlen($zipcode)) { return false; } $states = State::findByZipcode($zipcode)->get(); if (0 !== $states->count()) { $this->setCacheStateFromStates($states, $address); return true; } return false; }