Jump to content

Options field and check


Peter Knight
 Share

Recommended Posts

I have a checkbox with only 1 possible value called "A Project". I'm using the relatively new Options field type.

I'm trying to echo some html if that checkbox is checked.

I tried this 

if ($page->portfolio_type ) {

but it always outputs my HTML even if the checkbox isn't checked. 

I then tried the following from the forum suggestion

if ($page->portfolio_type == 1) // 1 is checked, 0 is unchecked

This has no effect either so I'm wondering if I need a different approach based on the fact that I'm using the Options fieldtype rather than a standard page based options?

Link to comment
Share on other sites

Hi,

if I'm not mistaking, you can check if a specific option is selected using the has() method.

So in your case one of these options should work:

if ($page->portfolio_type->has(1)) { ... }  //assuming the option is defined as: "1=A Project"
if ($page->portfolio_type->has("title='A Project'")) { ... }

Hope this helps.

Edit: Background should be that the option field returns a WireArray. Which has the has() method for checking its content.

  • Like 4
Link to comment
Share on other sites

@Peter

one more suggestion, have you tried the Tracy debugger yet?

Once you have it installed you could simply bd($page->portfolio_type) and then see what the system is outputting in either case, instead of guessing at it...

  • Like 4
Link to comment
Share on other sites

@Peter

one more suggestion, have you tried the Tracy debugger yet?

Once you have it installed you could simply bd($page->portfolio_type) and then see what the system is outputting in either case, instead of guessing at it...

Just tried it and discovered a missing include file (unrelated issue). Looks very useful.

The setup options are a little daunting but I'll definitely try this as a first port of call in future.

Link to comment
Share on other sites

Just tried it and discovered a missing include file (unrelated issue). Looks very useful.

Hey Pete - be sure to post about this in the Tracy support thread so I can take a look.

The setup options are a little daunting but I'll definitely try this as a first port of call in future.

It works out of the box with the settings as they are. For more info though, be sure to check out the blog post: https://processwire.com/blog/posts/introducing-tracy-debugger/ 

  • Like 1
Link to comment
Share on other sites

Hey Pete - be sure to post about this in the Tracy support thread so I can take a look.

Hey Adrian

I just meant that TracyDebugger had uncovered a call to one of my own missing include files. I didn't mean there was an issue with TD as such :)

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...