user3442612
user3442612

Reputation: 1822

How to exclude a rule from a rule set

I have a question about picking validation rules.

I stick my validation rules in the model like so

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]'
    )
  )
);

Then call it in the controller like so

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

That was for the create method, but I have an update method which doesn't require the select_chapter set of rules.

Is there a simple way to call this rule set (sentence_fields), but exclude select_chapter for my update method?

Thanks.

Upvotes: 0

Views: 125

Answers (2)

killstreet
killstreet

Reputation: 1332

As the above method works, I would recommend making it a function to allow better readability and ease of use. To do so have a look at the following

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;
}

This way you can do your validation as follow:

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

$this->form_validation->set_rules($validate);

Upvotes: 1

Hikmat Sijapati
Hikmat Sijapati

Reputation: 6994

If you want to exclude select_chapter for my update method.Just use array's unset() method like this..

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

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

Upvotes: 1

Related Questions