I have done some digging around on the forum and found several cases of problems with checkboxes. Nothing that really solves my problem though.

Basically I am adding form validation to a form. Part of the form is a checkbox, 1 checkbox with either a true or false value.

The view looked kinda like this:
<p><label for="inschrijving_open">Inschrijvingen open</label>&lt;?php echo form_checkbox('inschrijving_open', TRUE, $inschrijving_open); ?&gt;</p>

This worked just fine. This was for the edit view, the add view would have had a FALSE at the place of $inschrijving_open.

Now I tried to add validation. I can do textareas or dates or whatever, and callbacks even. I just can't get the checkbox to work. What I understand from the user_guide it should look sort of like this:
<p><label for="inschrijving_open">Inschrijvingen open</label>&lt;?php echo form_checkbox('inschrijving_open', set_checkbox('inschrijving_open', 'true')); ?&gt;</p>

What I want it to do is just repopulate if other fields in the form are required and then not filled out. I also want it to display the value in the database first time it is loaded. But here is the killer I also need it to remember if it was changed since it was loaded from the database.

I can better illustrate it using a text example. Let's say the database has 'Example 1'. That value is set to a field. Then someone comes along and changes it to 'Example 2'. He then submits the form, but forgot to fill out a required field. The form reloads and would get the old value from the database ('Example 1'), but it should remember to repopulate using 'Example 2'.

This has got me quite stumped.

I use
$this->validation->some_field ? $some_field=$this->validation->some_field :$some_field=$row->some_field;
And then populate/repopulate
<code>form_checkbox('some_field', TRUE, $some_field);</code>

Oh, its an 1.5.4 example:-)

I get the concept behind it, just not the syntax for the 'new' formvalidation class, instead of the old and deprecated validation class.

I checked one of our 1.7.1 projects:
set_value('gender')? $gender=set_value('gender'):$gender=$row->gender;


Hmmm, yes I get that concept for a set_value, my main gripe here is that I just can't seem to get that set_checkbox is the checkbox version. I am probably missing something here.

Controller for a normal field
$this->form_validation->set_rules('subtitel', 'Subtitel');
$data['subtitel'] = $row->subtitel;

View for a normal field
<p><label for="subtitel">Subtitel</label>&lt;?php echo form_input('subtitel', set_value('subtitel')); ?&gt;</p>

But if I fill it with database value or the filled in value that wasnt correct...? Nope lost me. $row->gender is a controller thing, but set_value is a view thing. I must be confused here.

I normally parse the entire resultset to a view and create a foreach there.
So that's why I get $row->gender in a view.

I never understood why I would have to put that kind of data in an $data array. Seems double the work and execution time is increased.

But that's off topic

As for your code
&lt;?php echo form_checkbox('inschrijving_open','1', set_checkbox('inschrijving_open', '1')); ?&gt;
Should do the trick. Is does not parse a value but rather the state it was is before the commit.

Aha! Go figure, I had it earlier, but that last one made me realise what I did wrong. ``s compared to ''s, of course that failed.

Anyways this did help a bunch, it is now working! Thanks a slew, sometimes these forms can just really ruin a persons mood. Smile

Hm, well I am going to bring this up again.

With the entire situation there is one (major) problem.

It will populate if it has been posted, but it will not populate if a field has been made empty. It will then use the database value. Which means that let's say someone wants to remove the data in an optional field, and forget to fill in a required field, it will then reset both fields to the database value. This could make people loose huge amounts of data.

How would I work around that?

EDIT: Better way to describe the problem:

In this situation:
set_value('locatie')? $data['locatie']=set_value('locatie'):$data['locatie'] = $row->locatie;
You either have a empty value or field and it uses the database value, or you don't and it uses the posted value. You kind of need a third option, the empty one made empty and supposed to be empty by the user.

[eluser]Trae R.[/eluser]
How about something like this, using a nested ternary operator to check the post value and effectively add the tertiary condition you mention?

(Note this example is untested, aside from a syntax check.)
$data['locatie'] = ($this->input->post('locatie') ? $this->input->post('locatie') : (set_value('locatie') ? set_value('locatie') : $row->locatie));

I just ran some tests with that. It doesn't appear to work I am afraid. also kind of advices you against nesting them.

I just can't seem to detect the difference between an empty field on purpose and an empty field by mistake. Don't other people catch that in their apps?

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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