2

私は現在、FuelPHPへの最初のベンチャーを行っており、モデル内の特定のキー(ユーザー名など)にマークを付ける方法と、オイルを使用してマークを付けることができるかどうかを考えていました。特にモデルでの検証に関しては、次のようになります。

protected static $_properties = array(
  'id',
  'name' => array(
     'data_type' => 'string',
     'label' => 'Name',
     'validation' => array('required', 'max_length'=>array(100), 'min_length'=>array(10)) //validation rules
  ),
  'username' => array(
     'data_type' => 'string',
     'label' => 'Username',
     'validation' => array('required', 'unique') // does this work?
  )
)
4

2 に答える 2

4

「ユニーク」と呼ばれるフレームワーク定義の検証ルールがあれば、それは機能します。しかし、ありません。

ドキュメント (http://docs.fuelphp.com/classes/validation/validation.html#/extending_validation) には、カスタム ルールを追加する方法を説明する例があり、例として「一意」を使用しています。

于 2012-09-03T12:04:30.630 に答える
0

私が更新している現在のレコードを除いて、これに問題がありました。これを解決するために、少し「ハッキング」しました。

これは Model_Crud で動作しますが、少し調整すれば他のものでも動作するはずです。

ルール (model/mymodel.php 内):

protected static $_rules = array(
  'uniqueField' => 'unique[Model_Product,code]', //unique[Model,fieldToCheck]
);

メソッド (myvalidation.php 内):

public static function _validation_unique($val, $model, $field)
{
  if (empty($val))
    return true;

  $findOpts = array(
    'where' => array(
      array($field, '=', $val)
    )
  );

  $input = \Validation::active()->input();

  if (! empty($input['id']))
    $findOpts['where'][] = array('id', '<>', $input['id']);

  \Validation::active()->set_message('unique', 'The field :label must be unique, but :value has already been used');

  //Model_Crud provides a find method which I use to find any of the duplicates
  $obj = call_user_func(array($model, 'find'), $findOpts);

  return ! count($obj) > 0;
}
于 2013-02-19T20:20:49.520 に答える