2017-01-24 4 views
0

妥当性検査ルールの選択に関する質問があります。ルールセットからルールを除外する方法

は、私はそのように

public $validate = array(
    'sentence_fields'=> array(
    'select_chapter' => array(
     'field'=>'select_chapter', 
     'label'=>'Select chapter', 
     'rules'=>'required|integer' 
    ), 
    'source_sentence' => array(
     'field'=>'source_sentence', 
     'label'=>'Source', 
     'rules'=>'trim|required|max_length[500]' 
    ), 
    'translated_sentence' => array(
     'field'=>'translated_sentence', 
     'label'=>'Translation', 
     'rules'=>'trim|required|max_length[500]' 
    ), 
    'translated_translation' => array(
     'field'=>'translated_translation[]', 
     'label'=>'Select another translation', 
     'rules'=>'trim|max_length[500]' 
    ) 
) 
); 

のようなモデルで私の検証ルールを固執作成方法のためだったので

$validate = $this->sentence_model->validate['sentence_fields']; 
$this->form_validation->set_rules($validate); 

のようなコントローラでそれを呼び出すが、私はupdateメソッドを持っていますselect_chapterのルールセットは必要ありません。

このルールセット(sentence_fields)を呼び出す簡単な方法はありますか?私の更新方法はselect_chapterを除外しますか?

ありがとうございました。

答えて

1

あなたはこのような私の更新method.Just使用アレーのunset()メソッドのselect_chapterを除外したい場合は...

$validate = $this->sentence_model->validate['sentence_fields']; 

unset($validate['sentence_fields']['select_chapter ']);//unsets your array 
$this->form_validation->set_rules($validate); 
+0

こんにちは、私はこれがどのように動作するかを理解しますが、何らかの理由で、私はまだ取得しています次の出力の章配列を選択してください。 '$ validate = $ this-> sentence_model-> validate ['sentence_fields']; unset($ validate ['sentence_fields'] ['select_chapter']); print_r($ validate); ' 明らかに間違っているものはありますか?ありがとう。 – user3442612

+0

はい、 '$ validate ['sentence_fields'] ['select_chapter']'の各キーを解除する必要があります。 –

+0

'unset($ validate ['select_chapter']);' '$ validate 'に' sentence_fields'を割り当てたように見えます – user3442612

1

上記の方法が動作するように、私は読みやすくかつ使いやすさを可能にし、それ機能することをお勧めします使用の。そのためには、以下の

public $validate = array(
    'sentence_fields'=> array(
    'select_chapter' => array(
     'field'=>'select_chapter', 
     'label'=>'Select chapter', 
     'rules'=>'required|integer' 
    ), 
    'source_sentence' => array(
     'field'=>'source_sentence', 
     'label'=>'Source', 
     'rules'=>'trim|required|max_length[500]' 
    ), 
    'translated_sentence' => array(
     'field'=>'translated_sentence', 
     'label'=>'Translation', 
     'rules'=>'trim|required|max_length[500]' 
    ), 
    'translated_translation' => array(
     'field'=>'translated_translation[]', 
     'label'=>'Select another translation', 
     'rules'=>'trim|max_length[500]' 
    ) 
) 
); 

public function formValidationRules($validation, $unset = array()) { 
    if($unset) { 
     return $this->unsetValidation($unset); 
    } else { 
     return $this->validate[$validation]; 
    } 
} 

private function ($unset) { 
    $validations = $this->validate[$validation]; 
    foreach($unset as $key) 
    { 
    unset($validations[$key]); 
    } 
    return $validations; 
} 

であなたは次のようにあなたの検証を行うことができるこの方法を見てみましょう:

$validate = $this->sentence_model->formValidationRules('sentence_fields', ['select_chapter']); 

$this->form_validation->set_rules($validate); 
関連する問題