4

カスタムアプローチなしで、入力が0値より大きくなければならないというルールをYiiモデルに適用する方法を知っている人はいますか..

お気に入り :

public function rules()
{
    return array( 
        ....
        ....

            array('SalePrice', 'required', "on"=>"sale"),

        ....
        ....
    );
}

どうもありがとう ..

4

6 に答える 6

8

より簡単な方法 array('SalePrice', 'numerical', 'min'=>1)

カスタムバリデータメソッドで

array('SalePrice', 'greaterThanZero')

 public function greaterThanZero($attribute,$params)
   {

      if ($this->$attribute<=0)
         $this->addError($attribute, 'Saleprice has to be greater than 0');

 }
于 2012-03-29T07:46:15.893 に答える
2

私はそれが価格であると思うので、あなたは次のように最小値として0.01(ペニー)を使うことができます:

array('SalesPrice', 'numerical', 'min'=>0.01),

このソリューションは、入力された数値が価格であることを検証せず、0.01を超えることだけを検証することに注意してください。

于 2012-03-29T07:43:57.737 に答える
0

私はこれには遅すぎることを知っています。ただし、将来の参考のために、このクラスも使用できます

<?php
class greaterThanZero extends CValidator 
{
/**
 * Validates the attribute of the object.
 * If there is any error, the error message is added to the object.
 * @param CModel $object the object being validated
 * @param string $attribute the attribute being validated
*/
 protected function validateAttribute($object,$attribute)
  {
    $value=$object->$attribute;
     if($value <= 0)
    {
    $this->addError($object,$attribute,'your password is too weak!');
}
 }


  /**
  * Returns the JavaScript needed for performing client-side validation.
  * @param CModel $object the data object being validated
  * @param string $attribute the name of the attribute to be validated.
   * @return string the client-side validation script.
  * @see CActiveForm::enableClientValidation
*/
 public function clientValidateAttribute($object,$attribute)
 {

    $condition="value<=0";
     return "
   if(".$condition.") {  messages.push(".CJSON::encode($object->getAttributeLabel($attribute).' should be greater than 0').");
 }";
}

 }

?>

使用する前に、このクラスがインポートされていることを確認してください。

于 2013-12-20T07:16:06.393 に答える
0

誰もドキュメントをチェックしませんでしたか?

組み込みのバリデータCCompareValidatorがあります。

['SalePrice', 'compare', 'operator'=>'>', 'compareValue'=>0]
于 2015-05-07T14:41:12.883 に答える
-2

私はこれを正規表現で処理しました。それも役立つかもしれません..

array('SalePrice', 'match', 'not' => false, 'pattern' => '/[^a-zA-Z0]/', 'message' => 'Please enter a Leader Name', "on"=>"sale"),

あなたの助けと時間をありがとう@sdjuan&@Ors..

于 2012-03-30T05:55:33.350 に答える