Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

3.1.12 FieldGroup / setID [SOLVED]


Go to End


9 Posts   1827 Views

Avatar
timo

Community Member, 47 Posts

9 May 2015 at 6:22pm

How do you set an ID for FieldGroup???
This doesnt work: (and neigther FieldGroup::create)

$CFGroup = new FieldGroup(
						new SliderField('BN', 'Brightness', -100, 100),
						new SliderField('R', 'R', 0, 255),
						new SliderField('G', 'G', 0, 255),
						new SliderField('B', 'B', 0, 255)
		);
		$CFGroup->setID('CFGroup');
		$CFGroup->setTitle('Custom Colorize');

thank.timo

Avatar
Pyromanik

Community Member, 419 Posts

11 May 2015 at 11:09pm

Edited: 11/05/2015 11:11pm

If you're referring to the HTML ID attribute, then setAttribute('ID', 'CFGroup') is probably what you're after.

::create() isn't anything special over new (in terms of output) it simply returns a newly constructed object. The advantage is in dependency injection and that you can from chain it.

ie. new FormField()->setID() will give issues.
FormField::create()->setID() will be fine.

It's syntactic sugar for most people, until you start wanting to get a bit advanced with DI. It's recommended because without it there's no chance of DI in the future.

Avatar
timo

Community Member, 47 Posts

12 May 2015 at 12:09am

Edited: 12/05/2015 12:21am

Hi Pyromanik
ive tested your suggestion but this also doesnt set an ID.
it would be handy to have an id on that field to use it in jquery.

class MyImageDecorator extends DataExtension :

function getCustomFields(){
		
		Requirements::javascript('mysite/javascript/ImageDecorator.js');
		Requirements::css("mysite/css/ImageDecorator.css");
		
		$previewSrc = $this->owner->getMyCMSFilter() ? $this->owner->GetMyCMSFilter()->SetHeight(100)->Link() : $this->owner->SetHeight(100)->Link();
		$size = $this->owner->getMyCMSFilter() ? $this->format_size(filesize($this->owner->GetMyCMSFilter()->getFullPath())) : $this->format_size(filesize($this->owner->getFullPath()));
		//$size = ceil(filesize($this->owner->GetMyCMSFilter()->getFullPath()) / 1024).' KB';
		$urlField = new FieldGroup( 
			new LiteralField('', '<div style="height: 100px;">
									<img id="loader" src="'.PROJECT_DIR.'/img/loading.gif"/>
									<img id="preview" style="display:none;" src="'.$previewSrc.'"/>
									</div>'
			),
			new LiteralField('',$size)
		);
		
		$urlField->setTitle('Preview');
		
		
		$selectionGroup = new OptionsetField(
					   $name = "Rotate",
					   $title = "Rotate",
					   $source = array(
						  "1" => "90º CW",
						  "2" => "90º CCW",
						  "3" => "180º"
					   ),
					   $value = ""
					);
		
		$filter = new OptionsetField(
					$name = "Filter",
					$title = "Filter",
					$source = array(
						"Colorize,100,10,10" => "Colorize Red",
						"Greyscale,0,0,255" => "Greyscale",
						"Colorize,10,100,10" => "Colorize Green",
						"Sepia" => "Sepia",
						"Colorize,10,10,100" => "Colorize Blue",
						"Custom" => "Custom Colorize",
						"" => "REMOVE FILTER"
					),
					$value = ""
		);
					
		$CFGroup = FieldGroup::create(
			new SliderField('BN', 'Brightness', -100, 100),
			new SliderField('R', 'Red', 0, 255),
			new SliderField('G', 'Green', 0, 255),
			new SliderField('B', 'Blue', 0, 255)
		)->setAttribute('ID', 'CFGroup');
		//$CFGroup->setID('CFGroup');
		//$CFGroup->setAttribute('ID', 'CFGroup');
		
					
		$fields = new FieldList();
		if($this->owner->getMyCMSFilter() || isset($_POST['Rotate'])){ 
			$fields->push($urlField);
		}
		$fields->push($selectionGroup);
		$fields->push($filter);
		$fields->push($CFGroup);
		
		
		return $fields;
    }

(function($) {
	$.entwine('ss',function($) {
		
		$('input[name=Filter]').entwine({  
		
			onmatch: function() {
				
				$active = $('input[name=Filter]:checked').val();
				$('#Filter').next().css({'display' : 'none'});  // ID ???? !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
				if($active == 'Custom'){
					$('#Filter').next().css( {'display' : 'block'} );
				}
			},
			onchange: function() {
				var radiovalue = $(this).val();
				if(radiovalue == 'Custom'){
					$('#Filter').next().css( {'display' : 'block'} );
				}else{
					$('#Filter').next().css( {'display' : 'none'} );
				}
			}
		});
	});
})(jQuery);

timo.

Avatar
timo

Community Member, 47 Posts

12 May 2015 at 1:13am

Edited: 12/05/2015 1:16am

OK. Solved.

$CFGroup->setName('CFGroup');

sets an id.
is this documented anywhere? or am i too dumb?

timo.

Avatar
Pyromanik

Community Member, 419 Posts

12 May 2015 at 9:06pm

The HTML id attribute is generated from the Field Name, yes.
Or more specifically a combination of the Form name (function that creates it/it submits to) and the Field name (FormField::create($name, $title, $value)).

Avatar
timo

Community Member, 47 Posts

12 May 2015 at 9:12pm

So whatfor is : ->setID() ?
if it doesnt do what i exspect it does?

Avatar
Pyromanik

Community Member, 419 Posts

12 May 2015 at 9:33pm

Edited: 12/05/2015 9:42pm

Nothing, looks like code rot from 2.4 days, when forms weren't nearly as flexible as they are now.
https://github.com/silverstripe/silverstripe-framework/blob/2.4/forms/CompositeField.php#L83
vs
https://github.com/silverstripe/silverstripe-framework/blob/3.1/templates/forms/CompositeField_holder.ss (furthermore this is why setAttribute() did nothing for you - CompositeField's template doesn't make use of attributes at all it seems, only $extraClass. setAttribute() is what you're after with most FormFields)

Avatar
timo

Community Member, 47 Posts

12 May 2015 at 9:46pm

Edited: 12/05/2015 9:47pm

->setAttribute('ID', 'CFGroup');

also doesnt work
i really have hard times to read and undestand the api docs.

Go to Top