CodeIgniter Forums
Form_validation confirm one of two items completed - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Form_validation confirm one of two items completed (/showthread.php?tid=29031)

Pages: 1 2


Form_validation confirm one of two items completed - El Forum - 03-28-2010

[eluser]gscharlemann[/eluser]
Hi

I'm not sure how to accomplish the following with form_validation. I have two fields, the first is a text field called new_type the second field is a drop down called existing_type.

here's the view:
Code:
...
<input type="text" name="new_type" value="" /><br/>
<select name="existing_type">
    <option value="new">New Type</option>
    <option value="type1">Type 1</option>
    ...
</select>

The rules are: if new_type is empty then something other than the option "new" must be selected in the "existing_type" dropdown (otherwise the form throws an error). And vice versa - If "new_type" is not empty and something other than "new" is selected in the "existing_type" dropdown, the form throws an error.

I don't need to check a DB to do this validation, just need to compare the two inputs. Is there a way to do this with form_validation class?

Thanks for any help.


Form_validation confirm one of two items completed - El Forum - 03-28-2010

[eluser]Tominator[/eluser]
I think this is absolutly simple:
Code:
if( !Empty($this->input->post('new_type')) && Empty($this->input->post('existing_type')) )
{
correct
}

elseif( Empty($this->input->post('new_type')) && !Empty($this->input->post('existing_type')) )
{
correct
}

else
{
incorrect
}



Form_validation confirm one of two items completed - El Forum - 03-28-2010

[eluser]gscharlemann[/eluser]
I can do it that way, but I was hoping to leverage the form_validation class:
Code:
$this->form_validation->set_rules('new_type', 'New Type', 'xss_clean');
$this->form_validation->set_rules('existing_type', 'Existing Type', 'if_empty_new_type_required|xss_clean');

Make sense?


Form_validation confirm one of two items completed - El Forum - 03-28-2010

[eluser]Tominator[/eluser]
Oh, I see. I'm very sorry. It's my mistake, because I am not very well on form_validation class and I was thinking, that you want "basic" answer.

I am going to learn it Smile


Form_validation confirm one of two items completed - El Forum - 03-28-2010

[eluser]flaky[/eluser]
create a callback function (read the user guide)
Code:
public function if_empty_new_type_required(){
    if( !Empty($this->input->post('new_type')) && Empty($this->input->post('existing_type')) )
    {
        return true;
    }

    elseif( Empty($this->input->post('new_type')) && !Empty($this->input->post('existing_type')) )
    {
        return true;
    }
    else
    {
        //set here the error message
        return false;
    }
}

then in the set_rules section
Code:
$this->form_validation->set_rules('new_type', 'New Type', 'xss_clean');
$this->form_validation->set_rules('existing_type', 'Existing Type', 'if_empty_new_type_required_callback|xss_clean');



Form_validation confirm one of two items completed - El Forum - 03-28-2010

[eluser]gscharlemann[/eluser]
Thanks flaky

Where does the callback function usually go? In the controller that is processing the form or elsewhere?


Form_validation confirm one of two items completed - El Forum - 03-28-2010

[eluser]theshiftexchange[/eluser]
[quote author="gscharlemann" date="1269836674"]Thanks flaky

Where does the callback function usually go? In the controller that is processing the form or elsewhere?[/quote]

In the controller where you are doing the form processing


Form_validation confirm one of two items completed - El Forum - 03-28-2010

[eluser]gscharlemann[/eluser]
I added a callback
Code:
$this->form_validation->set_rules('new_type', 'New Type', 'validate_existing_or_new_callback|xss_clean');
$this->form_validation->set_rules('existing_type', 'Existing Type', 'xss_clean');

Code:
public function validate_existing_or_new()
{
    if(!empty($this->input->post('new_type')) AND empty($this->input->post('existing_type')))
    {
        return TRUE;
    }
    else if(empty($this->input->post('new_type')) AND !empty($this->input->post('existing_type')))
    {
        return TRUE;
    }
    else
    {
        $this->form_validation->set_message('validate_existing_or_new', 'Choose new or existing, not both.');
        return FALSE;
    }
}

I'm getting the following error:
Can't use method return value in write context
and it refers to this line:
Code:
if(!empty($this->input->post('new_type')) AND empty($this->input->post('existing_type')))

Any ideas? Thanks


Form_validation confirm one of two items completed - El Forum - 03-28-2010

[eluser]suba[/eluser]
Hi,
$this->form_validation->set_rules('new_type', 'New Type', 'callback_check_old|xss_clean');
$this->form_validation->set_rules('existing_type', 'Existing Type', 'callback_check_new|xss_clean');

You must uses callback_ as a prefix of function name.

then
function check_old()
{

statement.........
}
function check_new()
{
statement..........
}
In function don't use callback_ name.
Those validation function , you can use in controller.


Form_validation confirm one of two items completed - El Forum - 03-29-2010

[eluser]flaky[/eluser]
sorry for my little typo Sad