Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional form inputs based on checkbox or select #200

Closed
7ammer opened this issue Jul 13, 2016 · 10 comments
Closed

Conditional form inputs based on checkbox or select #200

7ammer opened this issue Jul 13, 2016 · 10 comments
Milestone

Comments

@7ammer
Copy link

7ammer commented Jul 13, 2016

It would be great to have conditional input fields based on a checkbox's value or a select's value.
Form inputs could be simply shown/hidden with a bit of css (display:none / display:block).

@ghost
Copy link

ghost commented Mar 23, 2017

@7ammer can you make screen how do you see it

@ghost ghost closed this as completed Mar 23, 2017
@ghost ghost reopened this Mar 23, 2017
@7ammer
Copy link
Author

7ammer commented Mar 24, 2017

Hi @Aios. Here you can see a conditional form. The user clicks the button and a form shows.
The picture below shows yes / no buttons. Other inputs could be used: select dropdowns, radio buttons or checkboxes.

image

Here are some syntax ideas:

AdminFormElement::conditional( 
   AdminFormElement::checkbox('publish', 'Publish')->required()
)->equalTo('true',[
   AdminFormElement:: date('from_date', 'From Date')->required(), // only required if condition is true
   AdminFormElement:: date('to_date', 'To Date')->required() // only required if condition is true
]);

AdminFormElement::conditional(
   AdminFormElement::select('status', 'Status', ['Unknown', 'Single', 'Married'])
   )->equalTo('Single', [
      AdminFormElement::text('name', 'Your Name')   
   ])
   ->equalTo('Married', [
      AdminFormElement::text('name', 'Your Name'),
      AdminFormElement::text('name', 'Partners Name'),
      AdminFormElement::file('file', 'Attach proof')
   ]);

Alternatives:

AdminFormElement::conditional( 
   AdminFormElement::checkbox('publish', 'Publish')->required(), [
      'true' => [
         AdminFormElement:: date('from_date', 'From Date')->required(), // only required if condition is true
         AdminFormElement:: date('to_date', 'To Date')->required() // only required if condition is true
      ]
);

AdminFormElement::conditional(
   AdminFormElement::select('status', 'Status', [
      'Unknown',
      'Single'
      'Married'
   ]),
   [
      'Single' => [
         AdminFormElement::text('name', 'Your Name')
      ],
      'Married' => [
         AdminFormElement::text('name', 'Your Name'),
         AdminFormElement::text('name', 'Partners Name'),
         AdminFormElement::file('file', 'Attach proof')
      ]
   ]
);
AdminFormElement::checkbox('publish', 'Publish')->conditionalEquals(true, [
   AdminFormElement:: date('from_date', 'From Date')->required(), // only required if condition is true
   AdminFormElement:: date('to_date', 'To Date')->required() // only required if condition is true
]);

AdminFormElement::select('status', 'Status', [
   'Unknown',
   'Single'
   'Married'
])->conditionalEquals('Single', [
   AdminFormElement::text('name', 'Your Name')
])->conditionalEquals('Married', [
   AdminFormElement::text('name', 'Your Name'),
   AdminFormElement::text('name', 'Partners Name'),
   AdminFormElement::file('file', 'Attach proof')
])

@ghost
Copy link

ghost commented Mar 24, 2017

@7ammer Looks great!. What you think about survey.js?

@7ammer
Copy link
Author

7ammer commented Mar 24, 2017

survey.js looks great and has a lot of features.
What about jquery-visibly.js? It's very simple and efficient :)
conditionize.js is another alternative.

Example using jquery-visibly.js:

<form>
  <select id="status">
    <option value="Unknown">Unknown</option>
    <option value="Single">Single</option>
    <option value="Married">Married</option>
  </select>

  <div visibly="status:Single">
      <input type="text" name="name">
  </div>

  <div visibly="status:Married">
      <input type="text" name="name">
      <input type="text" name="partner">
      ...
  </div>
</form>

<script>
   $(document).ready(function() {
	$('[visibly]').Visibly();
   })
</script>

@ghost
Copy link

ghost commented Mar 27, 2017

We need make some with advanced features but very simple & flexible - still looking for survey))
@7ammer

@7ammer
Copy link
Author

7ammer commented Jun 18, 2017

Just found this: http://www.celljs.org. Might be useful.

@ghost
Copy link

ghost commented Jun 18, 2017

@7ammer no - i think it will be as survey.js

@7ammer
Copy link
Author

7ammer commented Jun 18, 2017

@Aios cool 👍

@ghost ghost added this to the 5.0 milestone Aug 10, 2017
@daaner
Copy link
Contributor

daaner commented Aug 13, 2019

I think we’ll do it in a new branch. Though old Issue

@daaner
Copy link
Contributor

daaner commented Sep 11, 2020

Such a functional will be in the next big update

@daaner daaner closed this as completed Sep 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants