Zend framework disable radio button in multioption element - php

Hi I am trying to disable one radio button value at multioption element
$elements[] = $em = $this->createElement("radio", "tshirt-$member",array(
"separator" => " ",
"belongsTo" => "tshirt",
"class" => "flow",
"parentClass" => 'renew-donation-holder',
"label_class" => 'span6',
"multioptions" => $options['options'],
'value' => ct_get($this->memSession, "tshirt.tshirt$member", $entry->athlete->tshirt_size),
));
try to disable it with radio button value
$em->setAttrib('disable', array( 'L'));
or to add
"disable" => array("L")
Is there any solution for this, another option is to add data atribute as some kind of flag?

I believe you are on the right track. Keep in mind that using this method you should specify the key of the associative array, not the value, depending on what your $options['options'] are.
Example:
$element = $this->createElement('radio', "tshirt-{$member}", [
'multiOptions' => [
's' => 'S',
'm' => 'M',
'l' => 'L',
]
]);
Disabling one (or more) items should now be:
// Single
$element->setAttrib('disable', ['s']);
// Multiple
$element->setAttrib('disable', ['s', 'l']);

Related

CakePHP link on Paginator ellipsis

I want to put a link on an ellipsis in the Paginator. When the pagination has 2 ellipsis's the ellipsis must have different link.
My code is:
echo $this->paginator->numbers(array(
'tag' => 'li',
'separator' => '',
'currentTag' => 'a',
'currentClass' => 'active',
'modulus' => 2,
'first' => 1,
'last' => 1,
'ellipsis' => "<li><a href='#' class='hellip'>...</a></li>"
));
So the result I want to create is:
1 ...(link) 6 7 8 ...(link) 12
In short, the Paginator doesn't support what you want it to do and so your only option is to modify the CakePHP source code. Specifically PaginatorHelper.php
First thing you'll need to do is modify the $defaults variable on line 720, and add leftEllipsis and rightEllipsis fields. This means that we can maintain consistent behavior when we don't set these fields in the $options variable.
$defaults = array('tag' => 'span', 'before' => null, 'after' => null,
'model' => $this->defaultModel(), 'class' => null,'modulus' => '8',
'separator' => ' | ', 'first' => null, 'last' => null, 'ellipsis' => '...',
'currentClass' => 'current', 'currentTag' => null, 'leftEllipsis' => null,
'rightEllipsis' => null);
Probably should unset our two new fields too (lines 735 - 738):
unset($options['tag'], $options['before'], $options['after'], $options['model'],
$options['modulus'], $options['separator'], $options['first'], $options['last'],
$options['ellipsis'], $options['class'], $options['currentClass'], $options['currentTag'],
$options['leftEllipsis'], $options['rightEllipsis']
);
The next bit is a tad tricky because it would be nice to be able to specify one of the ellipsis without the other, and in the absence of either, fallback onto whatever the original ellipsis field was set to. But the developers have used the magical extract and compact functions coupled with the first(...) and last(...) functions depending on certain fields being set in the $options parameter.
After line 756 insert the following code to default the $leftEllipsis to whatever $ellipsis is set to:
if(isempty($leftEllipsis)) {
$leftEllipsis = $ellipsis;
}
Next we need to modify what gets passed as the $options parameter to the first(...) function on lines 758 - 761.
if ($offset < $start - 1) {
$out .= $this->first($offset, compact('tag', 'separator', 'class') + array('ellipsis' => $leftEllipsis));
} else {
$out .= $this->first($offset, compact('tag', 'separator', 'class') + array('after' => $separator, 'ellipsis' => $leftEllipsis));
}
You can use this pattern to attack the right ellipsis too.
The proper way to do this is to fork the project on GitHub, make the changes to your version of the code base and create a pull request so you give the developers a chance to integrate your feature into the mainline. This way everyone can benefit from your work!
Good luck!

Dynamically apply a key/value pair to an array in one go (within first definition)

Imagine this situation:
$component = array(
'type' => 'chimney',
'material' => 'stone'
);
What i would like to do is to add a key/value pair to this array, if a certain condition is met.
$hasMetrics = true;
$component = array(
'type' => 'chimney',
'material' => 'stone',
'metrics' => ($hasMetrics ? array('width' => 60, 'height' => 2000) : false)
);
While this could be used, it will always cause a key called 'metrics' in my array.
Of course, if i don't want that, i could use array_merge() to merge a second array with the first (the second being either an empty array or the desired key/value pair, depending on the condition).
But what i am longing to find out is if there is any way to define this array like above, while taking care of $hasMetrics, without the use of any other means (such as array_merge()) but purely in the actual (first and only) definition of this array.
Like this: (non-applicable, demonstrative example)
$component = array(
'type' => 'chimney',
'material' => 'stone',
($hasMetrics ? array('metrics' => array(
'width' => 60,
'height' => 2000
)) : false)
);
(This, as i understand it, would generate two keys (type and material and then create one keyless value that is, itself, an array containing a key (metrics) and another array as value.)
Can anyone show me some proper approach? Perhaps there is some kind of PHP function available, with special properties (such as list() which is capable of cross-assignment).
EDIT
Perhaps some more clarification is needed, as many answers point out ways to go such as:
Using a followup assignment to a certain key
Filtering the generated array after defining it
While these are perfectly valid ways to extend the array, but i am explicitly looking for a way to do this in one go within the one array definition.
Not with the array defenition itself. I would add it to the array if necessary:
if($hasMetrics) {
$component['metrics'] = array('width' => 60, 'height' => 2000);
}
$hasMetrics = true;
$component = array(
'type' => 'chimney',
'material' => 'stone',
);
if($hasMetrics){
$component['metrics'] = array('width' => 60, 'height' => 2000);
}
Try
$component = array(
'type' => 'chimney',
'material' => 'stone',
'metrics' => $hasMetrics ? array('width' => 60, 'height' => 2000) : ''
);
And after that
$component = array_filter( $component ); // remove if it has '' value
OR
$component = array(
'type' => 'chimney',
'material' => 'stone',
);
if($hasMetrics) {
$component['metrics'] = array('width' => 60, 'height' => 2000);
}

CakePHP custom values on select input

I am trying to create a select input field. However I want to set the values of each individual option manually.
in an attempt I tried the following:
echo $this->Form->input('field', array(
'options' => array('Active', 'Blocked', 'Pending', 'Unknown'),
'values' => array(1,2,0,99),
'empty' => '(choose one)'
));
However this did not help (i.e 'Active' was 0, 'Blocked' was 1 etc...)
Does anyone know if it is possible to manually set the values?
values is not the right key, you need to leverage the options array for it, as well:
'options' => array(1 => 'Active', 2 => 'Blocked', 0 => 'Pending', 99 => 'Unknown'),
but that is basic PHP (since non-defined keys are numerically indexed starting off at 0).
You’ll need to use an associative array to set the keys as well:
$options = array(
'1' => 'Active',
'2' => 'Blocked',
'0' => 'Pending',
'99' => 'Unknown'
);
echo $this->Form->input('field', array('options' => $options));
However, I’d advise storing options like this in a separate database table rather than hard-coding them, to keep your views DRY and allowing them to be easily modified in the future.

Cakephp adding option field with array values

I am trying to create a drop down menu (option) and to fill this drop down i have sent an array list to the view:
$country = $this->country_list;
$this->set(compact('country'));
Now my question is does cake have a buildin method for me to set an input field using ($this->Form->input()) with the data of the array list?
take this example
$sizes = array(
's' => 'Small',
'm' => 'Medium',
'l' => 'Large'
);
echo $this->Form->input('size', array('options' => $sizes, 'default' => 'm'));
In the controller, set the value
$this->set('countries', $this->Country->find('list', array('fields' => 'Country.name')));
To show the dropdown box in the view
$this->Form->input('country_id');

$addToSet and $inc for updating a row

I'm working on a rating system. When a user rates, $inc increments the field, and addToSet adds the user_id to make sure the user only clicks rate once. I am checking if the user_id is already in the x field before updating, but that is another query which I'd rather avoid. Can I reach this purpose without having to write another query? I mean, $addToSet only adds if there is no value like that; can I instead get affected rows? Can you suggest other queries?
Thank you!
..->update(
array("_id" => $idob),
array(
'$inc' => array($type => (int) 1),
'$addToSet' => array("x" => (int) $user_id)
)
);
Ok I see the problem.
..->update(
array("_id" => $idob),
array(
'$inc' => array($type => (int) 1),
'$addToSet' => array("x" => (int) $user_id)
)
);
The problem is that you need a conditional $inc there so that it only $incs if it does add to set.
This is not possible with a unique index since unique indexes work from the root of the document atm. Also you probably want to use the $inc as a form of pre-aggregation or what not.
One method could be:
update(
array('_id' => $idob, 'x' => array('$nin' => array($user_id))),
array(
'$inc' => array($type => 1),
'$push' => array('x' => (int)$user_id)
)
)
This will only do the update if that user_id does not already exist in x.

Categories