MAR Validation

I never tried validating checkboxes, of which I have 2, and I’m
noticing some odd behavior. Even if the box is enabled it will still
throw a submission error when using:

$this->validate_existence(‘approval’,‘Check the approval box’);

If I comment out the above line the form submits and a value of “Yes”
is passed to the db as it should. I assume checkboxes require
something different?

Todd


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Assuming that your checkbox is named approval, has a value of some
sort (1 will do), and that there is a field in the database called
approval, and you have used populate() or another method to set the
value of approval to the value of $_POST[‘approval’], then this should
just work. I use validate_existence() for this all day long. If you
don’t have such a field in the database, then you can set the value
anyway and test for it, but it may not work.

Walter

On Aug 18, 2009, at 2:18 PM, Todd wrote:

I never tried validating checkboxes, of which I have 2, and I’m
noticing some odd behavior. Even if the box is enabled it will
still throw a submission error when using:

$this->validate_existence(‘approval’,‘Check the approval box’);

If I comment out the above line the form submits and a value of
“Yes” is passed to the db as it should. I assume checkboxes require
something different?

Todd


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

The approval checkbox now validates. On the admin side of things I
have a pre-populated duplicate of the client-side form so I can edit
entries. I was expecting the approval checkbox to be “checked” but it
remains unchecked despite a positive value being passed to the db. Is
it possible to have it already enabled? Perhaps my value setting is
incorrect? The only way I can get a value to display is by using a
text-field, not a checkbox.

Todd

On Aug 18, 2009, at 2:00 PM, Walter Lee Davis wrote:

Assuming that your checkbox is named approval, has a value of some
sort (1 will do), and that there is a field in the database called
approval, and you have used populate() or another method to set the
value of approval to the value of $_POST[‘approval’], then this
should just work.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

There’s no special magic in the view that pre-checks a checkbox. (That
would be nice!) Here’s what I usually add to my controller:

$foo->bar_checked = ($foo->bar > 0) ? ' checked="checked"' : '';

(add that line before the call to render_partial)

And then in the view, I simply add that variable into the checkbox
like this:

<input type="checkbox" name="bar" id="bar" value="1"<?=$bar_checked?> />

One other thing you need to take care of with checkboxes is the un-
check behavior. I usually add this inside the ‘edit’ $verb vase, after
the populate(clean($_POST)) line:

if($foo->bar > 0 && !isset($_POST['bar'])) $foo->bar = 0;

Otherwise, you will only be able to check the checkbox, not un-check
it later.

Walter

On Aug 20, 2009, at 6:11 PM, Todd wrote:

The approval checkbox now validates. On the admin side of things I
have a pre-populated duplicate of the client-side form so I can edit
entries. I was expecting the approval checkbox to be “checked” but
it remains unchecked despite a positive value being passed to the
db. Is it possible to have it already enabled? Perhaps my value
setting is incorrect? The only way I can get a value to display is
by using a text-field, not a checkbox.

Todd

On Aug 18, 2009, at 2:00 PM, Walter Lee Davis wrote:

Assuming that your checkbox is named approval, has a value of some
sort (1 will do), and that there is a field in the database called
approval, and you have used populate() or another method to set the
value of approval to the value of $_POST[‘approval’], then this
should just work.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Well, isn’t that too cool. Pretty much what I wanted, a “checked”
checkbox, excellent. Thanks.

I’ve been playing around with the value of the 'box. I wanted to use
“Yes” or “No” instead of “1” and “0” and I can get most of the way
there except that the checkbox no longer displays as checked in ‘edit’
though the value (in ‘view’) is correct. A slip-O-the-syntax I suspect.

Todd


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

I have multiple cases (4) and each corresponding “page” has different
form fields with different validation requirements. Currently I have
one model file that checks for all fields on all forms. The validation
works but it will throw an error for a field that may not even exist
depending on the case being submitted. Is there a way I can designate
the model (or have multiple models) to only check a specific case
based on which one is submitted?

Todd


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Could you refactor these cases to reflect different models? It sounds
like you may have too much in one model and could stand to break
things up a bit. What are the different pages you are showing, and
what are the validation rules? What does your object model look like?

If you can’t refactor, another way to approach this is to move the
validation out of the model and into the controller. If you have a one-
off validation need, you could use an uglier construction like this:

$foo = ActiveRecord::Create('foos');
if(isset($_POST['save')){
	$foo->populate($_POST);
	$foo->validate_existence('bar','You&rsquo;re missing a bar!');
	$foo->save();
	$foo->manage_result('_flash','flash','Foo created',$foo- 
 >link_for('edit','bar','link'));
}

Then leave whatever your absolute basic requirements are inside the
model where they belong, and these one-off requirements can be tested
piece-meal in the controller.

Walter

On Aug 26, 2009, at 5:31 PM, Todd wrote:

I have multiple cases (4) and each corresponding “page” has
different form fields with different validation requirements.
Currently I have one model file that checks for all fields on all
forms. The validation works but it will throw an error for a field
that may not even exist depending on the case being submitted. Is
there a way I can designate the model (or have multiple models) to
only check a specific case based on which one is submitted?

Todd


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options