共通の\ components名前空間に "EndTimeValidator"という名前のカスタムバリデータークラスを作成しました。モデルカスタムクライアント側の検証
コード:
namespace common\components;
use yii\validators\Validator;
class EndTimeValidator extends Validator
{
public function init()
{
parent::init();
$this->message = 'Event EndTime must be greater than StartTime.';
}
public function validateAttribute($model, $attribute)
{
$startDate = $model->StartDate;
$endDate = $model->EndDate;
$startTime = $model->StartTime;
$endTime = $model->EndTime;
if(($startDate == $endDate) && $startTime != "" && $endTime != "" )
{
if(strtotime($endTime) < strtotime($startTime))
{
$this->addError($attribute, $this->message);
}
}
}
public function clientValidateAttribute($model, $attribute, $view)
{
$startDate = $model->StartDate;
$endDate = $model->EndDate;
$startTime = $model->StartTime;
$endTime = $model->EndTime;
if(($startDate == $endDate) && $startTime != "" && $endTime != "" )
{
if(strtotime($endTime) < strtotime($startTime))
{
return <<<JS
messages.push($this->message);
JS;
}
}
}
}
とモデルに私が最初にそのパスを含めると、以下のように検証を定義した:
use common\components\EndTimeValidator;
['EndTime', EndTimeValidator::class],
更新質問:
しかし、それは与えています私の例外以下:
私はこの linkが参照されているか、非オブジェクトにメンバ関数getAttributeLabel(210
コール)。私は間違って何かしていますか?私を案内してください。ありがとう!
感謝を!しかしその後、それはまだ私に別のエラーを与えています。私は質問を更新しました。もう一度助けてもらえますか? – Chinmay
完全なエラーを表示して、それが指し示すファイルを表示してください – Yupik