###質問内容
Phalcon-devtoolsで生成したModelについて、テーブル定義上でNot Null制約が設定された項目は「PresenceOf」のValidationルールが自動で設定されますが、同時にPresenceOfエラー時のメッセージ「name is required」のように自動されてしまいます。
PresenceOfのValidationルールはそのままで良いのですが、このエラーメッセージを上書きする方法をご教示ください。
###概況
- Phalcon-devtoolsにより以下のコマンドで、Modelを生成。
phalcon model user
- Validationルールは特別に設定しなくても、Not Nullの入力項目「name」に対して入力しない状態でsaveすると、エラーが発生し、「name is required」のメッセージを取得可能。
- エラーメッセージを独自の文字列にするため、Userモデルに以下を追加。
$validator = new Validation(); $validator->add( 'name', new PresenceOf([ 'message' => "入力必須", ]) );
- エラーメッセージとして「入力必須」を取得する想定であったが、結果は変わらず「name is required」を取得してまう状態。
###ソースコード
PHP
1<?php 2 3use Phalcon\Mvc\Controller; 4use Phalcon\Validation; 5use Phalcon\Validation\Validator\PresenceOf; 6 7class User extends ModelBase 8{ 9 /** 10 * 11 * @var string 12 * @Column(type="string", length=767, nullable=false) 13 */ 14 public $name; 15 16 public function validation() 17 { 18 $validator = new Validation(); 19 20 $validator->add( 21 'name', 22 new PresenceOf([ 23 'message' => "入力必須", 24 ]) 25 ); 26 27 return $this->validate($validator); 28 } 29 30 /** 31 * Initialize method for model. 32 */ 33 public function initialize() 34 { 35 $this->setSchema("lashca"); 36 $this->setSource("user"); 37 } 38 39 /** 40 * Returns table name mapped in the model. 41 * 42 * @return string 43 */ 44 public function getSource() 45 { 46 return 'user'; 47 } 48 49 /** 50 * Allows to query a set of records that match the specified conditions 51 * 52 * @param mixed $parameters 53 * @return User[]|User|\Phalcon\Mvc\Model\ResultSetInterface 54 */ 55 public static function find($parameters = null) 56 { 57 return parent::find($parameters); 58 } 59 60 /** 61 * Allows to query the first record that match the specified conditions 62 * 63 * @param mixed $parameters 64 * @return User|\Phalcon\Mvc\Model\ResultInterface 65 */ 66 public static function findFirst($parameters = null) 67 { 68 return parent::findFirst($parameters); 69 } 70 71} 72
###環境
- 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
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。