? ? Success

User tests: Successful: Unsuccessful:

avatar itbra
itbra
19 Feb 2014

Fixing issue described in bug #31493, related to JForm (libraries/joomla/form/form.php)

Presently JForm::setFieldAttributes sets passed attribute values without taking care for existing ones which may lead to loss. To prevent this and add new attribute values preserving existing ones one had to call:

$form->setFieldAttribute(
   'fieldname',
   'attribute',
   $form->getFieldAttribute('fieldname', 'attribute', '', 'group') . ' newAttr1 newAttr2 newAttr3 ...',
   'group'
);

This is not very comfortable and might lead to very long statements. Also, it does not take care for value duplication. The PR adds an additional function parameter as flag for merging or replacing (default) along with an extended processing statement. Adding attribute value statements will then look like:

$form->setFieldAttribute(
   'fieldname',
   'attribute',
   'newAttr1 newAttr2 newAttr3 ...',   // a space separeted attributes list
   'group',
   false   // flag indicating whether to merge or replace
);

When the last paramter is passed and set to 'false' the form first reads all values of the related form fields attribute, dumps them into an array, merges the passed attribute values and finally filters for duplicates. This way all attribute values are set without having dupes.

avatar clubnite clubnite - open - 19 Feb 2014
avatar itbra itbra - reference | - 21 Feb 14
avatar brianteeman brianteeman - change - 21 Aug 2014
Status New Pending
avatar nicksavov nicksavov - change - 21 Aug 2014
Labels Removed: ?
avatar brianteeman brianteeman - change - 2 Sep 2014
Category Libraries
avatar infograf768 infograf768 - change - 15 Oct 2014
Labels Added: ?
avatar brianteeman brianteeman - change - 17 Oct 2014
Title
[#31493] fix JForm::setFieldAttribute()
fix JForm::setFieldAttribute()
avatar jissues-bot jissues-bot - change - 17 Oct 2014
Title
[#31493] fix JForm::setFieldAttribute()
fix JForm::setFieldAttribute()
avatar nueckman
nueckman - comment - 14 Mar 2015

It's just an opinion, but i'm against it. It's not an expected behavior. If a setX(getX() + Y) is inconvenient, i would recommend to add something like "append".


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/3143.
avatar nueckman nueckman - test_item - 14 Mar 2015 - Not tested
avatar itbra
itbra - comment - 14 Mar 2015

What do you mean with expected behavior? My expected behavior is that setting additional data does not replace existing data carelessly, which is the main issue with this method - especially when it comes to ADD more values to an element's class attribute. The fix not only catches this issue but also checks for duplicates. The latter would potentially get created if you append new values to this attribute.
To prevent this duplication either there are two scenarios:

a) the developer must read the attribute first, prepare the value (remove any dupes) and then set the new value
b) the field class method makes this job, which makes the developer's life easier

I suppose you haven't had to add new class attributes to form fields on that often yet, have you? :-)

avatar nueckman
nueckman - comment - 15 Mar 2015

The expected behavior of set(x, y) is setting x to y. I'm not a native speaker, but set implies a single task no matter the past. I'm not against a feature for accessing the attributes in a more convenient way, but the current solution seems not right to my. But of course, that's only an opinion. In a real OOP world you would need something like Field->getAttribute()->add('foobar').

avatar Fedik
Fedik - comment - 15 Mar 2015

I agree with @nueckman

And I am sure someone already use setFieldAttribute() to replace existing attribute, I seen somewhere in CMS it used for override existing attribute in the form field(s).

this pull will make b/c issue from my point of view :smile:

avatar nueckman
nueckman - comment - 15 Mar 2015

@Fedik it's a new paramater, so there will be no b/c issues in a coding way (at least if you haven't override the method). But i'm still against it ;)

avatar roland-d roland-d - change - 3 May 2015
Milestone Added:
avatar roland-d
roland-d - comment - 3 May 2015

I tend to agree with @nueckman that the setMethod is for setting a complete new value. Look at other classes we do the same there. I would be more inclined to have an addFieldAttribute() method instead, doing the same thing but with a clear task.

avatar wilsonge wilsonge - change - 6 Nov 2015
Milestone Removed:
avatar brianteeman brianteeman - change - 10 Mar 2016
Category Libraries Fields Libraries
avatar JoomliC
JoomliC - comment - 13 Mar 2016

Don't know where this PR is...

Just a note:
In library, setField already have a $replace option.
public function setField(SimpleXMLElement $element, $group = null, $replace = true)

So could make sense to have the same for setFieldAttribute ?
public function setFieldAttribute($name, $attribute, $value, $group = null)

This option could be really useful in some cases (i see one currently in my own extension!) and by looking at code, no B/C issue.
I will test this PR soon... :+1:


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/3143.

avatar nrueckmann
nrueckmann - comment - 13 Mar 2016

Just for the record: I'm still not convinced ;) The $replace argument in setField is more like a $force. But this PR wants to add the ability to append something to the current value.

avatar bertmert
bertmert - comment - 13 Mar 2016

And what about values that are comma separated, semicolon separated and so on?
You don't know how values look like. Space separated is one special case of lots of.
Should be solved in another way than this PR does (if feature really needed).

avatar roland-d roland-d - close - 7 May 2016
avatar joomla-cms-bot joomla-cms-bot - close - 7 May 2016
avatar roland-d roland-d - change - 7 May 2016
Status Pending Closed
Closed_Date 0000-00-00 00:00:00 2016-05-07 16:58:19
Closed_By roland-d
avatar roland-d
roland-d - comment - 7 May 2016

The setFieldAttribute should only set values, if we want to append values there should be an aptly named function for that. A pull request with that feature would be needed. Closing this pull request for that reason. Thank you all for your contribution.


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/3143.

avatar roland-d roland-d - change - 7 May 2016
Title
fix JForm::setFieldAttribute()
[#31493] fix JForm::setFieldAttribute()

Add a Comment

Login with GitHub to post a comment