###実現したいこと
PHPフレームワークのPhalcon3.2にて、各入力項目にValidationやエラー時の独自メッセージを設定したい。
その上で、まずはサンプルとして、Eメールの入力項目について、EmailのValidation処理を行い、Validationエラー時メッセージとして、「Please enter a correct email address」を設定する。
###発生している問題・エラーメッセージ
stack overflowの内容(リンク先)に基づきコードを書いてみたが以下のエラーが発生している状態であり、Validation・エラーメッセージの設定ができていない状態です。
エラーの原因の特定・対処法について、アドバイスあればご教示ください。
TypeError: Argument 2 passed to Phalcon\Validation::add() must implement interface Phalcon\Validation\ValidatorInterface, instance of Phalcon\Mvc\Model\Validator\Email given**
Phalconデバックコントローラーによるエラー発生箇所は、model/User.phpのエラーメッセージを設定している以下の行です。
PHP
1'message' => 'Please enter a correct email address',
###該当のソースコード
model/User.php
PHP
1 <?php 2 3 use Phalcon\Mvc\Model; 4 use Phalcon\Validation; 5 use Phalcon\Mvc\Model\Validator\Email as EmailValidator; 6 7 class User extends Model 8 { 9 10 /** 11 * 12 * @var string 13 * @Column(type="string", length=767, nullable=false) 14 */ 15 public $email; 16 17 public function validation() 18 { 19 $validator = new Validation(); 20 21 $validator->add( 22 'email', 23 new EmailValidator([ 24 'message' => 'Please enter a correct email address', 25 ]) 26 ); 27 28 return $this->validate($validator); 29 } 30 31 /** 32 * Initialize method for model. 33 */ 34 public function initialize() 35 { 36 $this->setSchema("lashca"); 37 $this->setSource("m_semiuser"); 38 } 39 40 /** 41 * Returns table name mapped in the model. 42 * 43 * @return string 44 */ 45 public function getSource() 46 { 47 return 'user'; 48 } 49 50 /** 51 * Allows to query a set of records that match the specified conditions 52 * 53 * @param mixed $parameters 54 * @return User[]|User|\Phalcon\Mvc\Model\ResultSetInterface 55 */ 56 public static function find($parameters = null) 57 { 58 return parent::find($parameters); 59 } 60 61 /** 62 * Allows to query the first record that match the specified conditions 63 * 64 * @param mixed $parameters 65 * @return User|\Phalcon\Mvc\Model\ResultInterface 66 */ 67 public static function findFirst($parameters = null) 68 { 69 return parent::findFirst($parameters); 70 } 71 72 }
controller/IndexController.php
PHP
1<?php 2 3use Phalcon\Http\Request; 4 5class IndexController extends ControllerBase 6{ 7 8 public function beforeExecuteRoute($dispatcher) 9 { 10 $this->data = $this->request->getPost(); 11 } 12 13 public function indexAction() 14 { 15 $debug = new \Phalcon\Debug(); 16 $debug->listen(); 17 18 $request = new Request(); 19 if ($request->isPost() == true) { 20 $user = new User(); 21 $user->email = $this->data[email]; 22 23 if ($user->save() === false) 24 { 25 $message = $user->getMessages(); 26 }else{ 27 $message = ""; 28 29 } 30 31 } 32 $this->view->setVar("message", $message); 33 } 34 35}
###環境情報
- CentOS 7.4(x64)
- Apache 2.4.6
- PHP 7.0.26
- Phalcon Framework 3.2.4
- MySQL Ver 14.14 Distrib 5.6.38
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。