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

Use disabled attributes for radio / checkbox form control when is reaondly #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions modules/adminui/lib/Form/ChoiceControlTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* @author Raphaël MARTIN
* @copyright 2024 3liz.com
*
* @see 3liz.com
*
* @license MIT
*/

namespace Jelix\AdminUI\Form;

trait ChoiceControlTrait
{
/**
* use parent method, and if readonly, add proper attributes
* Note that value won't be sent by submit but it is not a problem as it is readonly
* this method is usefull for radio and checkbox(es) controls
*/
protected function getControlAttributes()
{
$attr = parent::getControlAttributes();
if ($this->ctrl->isReadOnly()) {
$attr['disabled'] = 'disabled';
} else {
unset($attr['disabled']);
}
return $attr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
class checkbox_adminlteFormWidget extends checkbox_htmlFormWidget
{
use \Jelix\AdminUI\Form\WidgetTrait;
use \Jelix\AdminUI\Form\ChoiceControlTrait;

protected function outputJs()
{
Expand Down Expand Up @@ -51,11 +52,6 @@ public function outputControl()
$attr['value'] = $this->ctrl->valueOnCheck;
$attr['id'] = $attrid.'_'.$this->ctrl->valueOnCheck;

// attribute readonly is not enough to make checkboxes readonly. Note that value won't be sent by submit but it is not a problem as it is readonly
if (array_key_exists('readonly', $attr)) {
$attr['disabled'] = 'disabled';
}

echo '<input';
$this->_outputAttr($attr);
echo '/>';
Expand Down Expand Up @@ -87,10 +83,7 @@ public function outputControl()
}
$attr['value'] = $this->ctrl->valueOnCheck;
$attr['type'] = 'checkbox';
// attribute readonly is not enough to make checkboxes readonly. Note that value won't be sent by submit but it is not a problem as it is readonly
if (array_key_exists('readonly', $attr)) {
$attr['disabled'] = 'disabled';
}

echo '<input';
$this->_outputAttr($attr);
echo '/>';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
class checkboxes_adminlteFormWidget extends checkboxes_htmlFormWidget
{
use \Jelix\AdminUI\Form\WidgetTrait;
use \Jelix\AdminUI\Form\ChoiceControlTrait;

protected function getCSSClass()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class radiobuttons_adminlteFormWidget extends radiobuttons_htmlFormWidget
{
use \Jelix\AdminUI\Form\WidgetTrait;
use \Jelix\AdminUI\Form\ChoiceControlTrait;

protected function getCSSClass()
{
Expand Down
20 changes: 20 additions & 0 deletions test/modules/test/forms/formallwidgets.form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,26 @@
<item value="assiette">a plate</item>
</checkboxes>

<checkboxes ref="outerspace_house" readonly='true'>
<label>You own an house on </label>
<item value="mars">Mars</item>
<item value="moon">Moon</item>
<item value="pluto">Pluto</item>
</checkboxes>

<radiobuttons ref="superhero" readonly='true'>
<label>You are </label>
<item value="bat">batman</item>
<item value="spider">spiderman</item>
<item value="iron">ironman</item>
</radiobuttons>

<checkbox ref="no_awnser_possible" readonly='true'>
<label>Question ?</label>
<oncheckvalue label="yes" />
<onuncheckvalue label="no" />
</checkbox>

<date ref="birthdaydate">
<label>Your birthday</label>
</date>
Expand Down