• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Validation: one of two field required

#1
Hello.

I have a form in which two fields are related.
I want at least one of the two fields to be filled out. It is also conceivable that both are filled in, but not that neither is filled out.
Is there a validation rule for this? Or can someone help me with a solution?

Greetings,

Kigh ...
Reply

#2
https://codeigniter.com/userguide3/libra...on-methods

I think you can create your own validation rule for this case. Or you can follow the code below. I think you can do that on CI4 too.


PHP Code:
if(!isset($_POST['field_one']) and !isset($_POST['field_two'])){

$this->form_validation->set_message('required''At least one field must be filled!');

$this->form_validation->set_rules('field_one''field one''required');
$this->form_validation->set_rules('field_two''field two''required');


Reply

#3
Hmmm... okay, today afternoon I#ll try it...
Reply

#4
For CI 4:

PHP Code:
        if($this->request->getPost('field_one') == '' && $this->request->getPost('field_two')){
            $error_msg 'You need to fill field_one or field_two.';
        }else{
            $validation Services::validation();
            if($this->request->getPost('field_one')){
                $validation->setRule('field_one''label 1''YOUR RULES HERE');
            }
            if($this->request->getPost('field_two')){
                $validation->setRule('field_two''label 2''YOUR RULES HERE');
            }
            if($validation->withRequest($this->request)->run() == false){
                $error_msg $validation->listErrors();
            }else{
                /* Here your success code */
            }
        


Where $error_msg contains your error.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.