Welcome Guest, Not a member yet? Register   Sign In
One Set of Fields or Another Required
#1

[eluser]GlenW[/eluser]
Hello all, first post here.

I have a project that I'm working on and am quite stumped by the best way to go about it.

I have three fields in a form that needs to be submitted. timeStart, timeStop and hours.

I would like a form_validation rule that requires timeStart AND timeStop to be filled in, or hours, or both.

For testing I tried just having it redirect to other pages to let me know where it was getting:

Code:
$this->form_validation->set_rules('timeStart', 'Time Start', 'required');
        $this->form_validation->set_rules('timeStop', 'Time Stop', 'required');

        if ( $this->form_validation->run === TRUE )
        {
            redirect("http://seatingisbelieving.com");
        }
        else
        {
            $this->form_validation->set_rules('hours', 'Hours', 'required');
            if ( $this->form_validation->run === TRUE )
            {
                redirect("http://google.ca");
            }
            else
            {
                redirect("http://outlook.com");
            }
        }

I'm assuming that the call to the run function under form_validation does not clear previous rules, which is part of my problem.

Any help would be greatly appreciated.
#2

[eluser]alexwenzel[/eluser]
You can achieve this behaviour by coding your own validation function.

http://ellislab.com/codeigniter/user-gui...#callbacks

At least this would be the way i would go after a quick thought.
#3

[eluser]GlenW[/eluser]
[quote author="alexwenzel" date="1349963231"]You can achieve this behaviour by coding your own validation function.

http://ellislab.com/codeigniter/user-gui...#callbacks

At least this would be the way i would go after a quick thought.[/quote]

The problem with the callback functions is that you seem to only be able to pass a single field to it, unless you use an array.

Would it be possible that I could set an array, called fields, and run the callback like this:

Code:
$this->form_validation->set_rules(fields, 'Fields', 'callback_required_fields');
#4

[eluser]alexwenzel[/eluser]
In my validation callback i would simply check if some of the other input fields are filled.

Code:
function my_callback_field1($xy) {

if ( ! $this->input->post('field2')) {
// not passed
}

// passed
}

function my_callback_field2($xy) {

if ( ! $this->input->post('field1')) {
// not passed
}

// passed
}

Without knowing if this works, just as an quick idea.
#5

[eluser]GlenW[/eluser]
That should work. What I'll try is having one callback that checks the post data, and call that callback for each of the fields. It's a bit of inefficient way of doing it, but all three calls to the function will either return true or false, so it should be fine.

I'll post back with my results and code once I get around to it.
#6

[eluser]GlenW[/eluser]
I got it working. I decided to only call it once and call it based on the Submit button's POST data (bad practise? Probably. But effective).

Code:
public function create($page = 'index')
{
        $this->form_validation->set_rules('btnSubmit', 'Submit', 'callback_required_fields');
        
        if ($this->form_validation->run() === TRUE)
        {
            $this->event_model->set_event();
            redirect(site_url());
        }
        echo "Nailed";
}

public function required_fields($str)
{
        //If timeStart AND timeStop, OR hours are not nothing, then return true.
        if ( ( $this->input->post('timeStart') != "" && $this->input->post('timeStop') != "" ) || ( $this->input->post('hours') != "" ) )
        {
            return true;
        }
        else
        {
            return false;
        }
}
#7

[eluser]alexwenzel[/eluser]
There is no best practise for this case i think Smile
#8

[eluser]Aken[/eluser]
That's one way to go about it. Alex is right; there's really no standardized way to do this.

Other options:

1) Create rules based on POST values.

Code:
if ( ! $this->input->post('hours'))
{
    // Require your start/stop times.
}
else
{
    // Require hours
}

2) Don't use form validation at all, and check the values / create error messages manually.
#9

[eluser]GlenW[/eluser]
[quote author="Aken" date="1350013352"]1) Create rules based on POST values.

Code:
if ( ! $this->input->post('hours'))
{
    // Require your start/stop times.
}
else
{
    // Require hours
}
[/quote]

I was originally going to go with that method, but the problem was that I wanted the user to be able to fill in timeStart, timeStop and hours, or hours, or timeStart and timeStop. If I had the write the code for that it then it would have been over complicated for the little bit I wanted.

Quote:2) Don't use form validation at all, and check the values / create error messages manually.

I considered that as well, but the reason I chose to do this project in CodeIgniter is because I have a much larger project coming up that is required to be done in CI, and thus I would like to try to understand CI's features to the best of my ability before I start the coding process on the other.
#10

[eluser]Aken[/eluser]
What's stopping you from doing that going by the first method? Your goal is to require one or the other - it doesn't say that if you require start/stop time, then hours will do absolutely nothing. This is just for setting your rules. You could do what you want with a IF/ELSE IF/ELSE set.

I get wanting to learn and understand CI. But if a CI feature doesn't work exactly for what you want, there's no requirement to use it.

And again, these are just other options. Use whichever one you like the most, they all do the same thing in the end.




Theme © iAndrew 2016 - Forum software by © MyBB