Crud: Only show field when another field value is true

Created on 8 Mar 2017  路  8Comments  路  Source: Laravel-Backpack/CRUD

When adding a new item, i'd like to only show input field 'X' when 'Y' has been selected.

Is this currently possible via the addField() method? Or would the only way be with my own custom javascript injected in?

Thanks!

Most helpful comment

There is a "toggle" field just for that, developed by @OwenMelbz in https://github.com/Laravel-Backpack/CRUD/pull/165 , but is not merged yet

I just copied the file proposed (as a custom template) in my own backpack space at resources/views/vendor/backpack/crud/fields/toggle.blade.php and started using it as per Owen instructions, worked great.

Usage: (angry is a boolean, why and what_can_we_do are varchar in my example)

````php
$this->crud->addField(
[
'label' => 'Are you angry?',
'name' => 'angry',
'type' => 'toggle',
'inline' => true,
'options' => [
0 => 'No',
1 => 'Si'
],
'hide_when' => [
0 => ['why', 'what_can_we_do'],
],
'default' => 0
]);

$this->crud->addField([ 'label' => "Why?", 'type' => 'text', 'name' => 'why', 'both');

$this->crud->addField([ 'label' => "What can we do?", 'type' => 'text', 'name' => 'what_can_we_do', 'both');

````

All 8 comments

There is a "toggle" field just for that, developed by @OwenMelbz in https://github.com/Laravel-Backpack/CRUD/pull/165 , but is not merged yet

I just copied the file proposed (as a custom template) in my own backpack space at resources/views/vendor/backpack/crud/fields/toggle.blade.php and started using it as per Owen instructions, worked great.

Usage: (angry is a boolean, why and what_can_we_do are varchar in my example)

````php
$this->crud->addField(
[
'label' => 'Are you angry?',
'name' => 'angry',
'type' => 'toggle',
'inline' => true,
'options' => [
0 => 'No',
1 => 'Si'
],
'hide_when' => [
0 => ['why', 'what_can_we_do'],
],
'default' => 0
]);

$this->crud->addField([ 'label' => "Why?", 'type' => 'text', 'name' => 'why', 'both');

$this->crud->addField([ 'label' => "What can we do?", 'type' => 'text', 'name' => 'what_can_we_do', 'both');

````

That's perfect - thanks for your help

+1 Worked here too, just keep in mind that if you're using iCheck you need to add some js in your document.ready (as @MarcosBL mentioned in #165, here) Thanks for sharing!

The 'default' setting not working. can someone verify?

@conandor been working for a while, so most likely a usage problem, can you share the code your using and show us the html its generated (not the whole document, just around the problem area)

The CRUD:

screen shot 2018-01-30 at 5 53 54 pm

The HTML:

screen shot 2018-01-30 at 5 52 16 pm

@conandor that didnt work :)

What method can I use to set the default choice.

Was this page helpful?
0 / 5 - 0 ratings