Ticket #2486 (new enhancement)

Opened 2 months ago

Last modified 2 months ago

automatically add a class to the fields in a form that are required

Reported by: nicolaas Assigned to: sminnee
Type: enhancement Priority: medium
Milestone: Component: Sapphire Framework
Version: 2.2.2-rc2 Severity: medium effort / impact
Keywords: Cc:
Due date: Harvest Task: (Unknown)
Invoice sent to client: 0 Hours:

Description

most of the time, when you have fields in a form, you want to highlight which ones are required. Would it be possible to add a class to the holder of each required field?

I tried to do this:

foreach($requiredFieldsArr as $fieldName) {

$newField = $fields->dataFieldByName($fieldName); $newField->setRightTitle("*"); $fields->replaceField($fieldName, $newField);

}

dataFieldByName as well as fieldByName do not actually return the right fields as stated in the documentation.

Attachments

Change History

Changed 2 months ago by nicolaas

Note, that the above method shown is pretty clumsy. It would be much better to have a class than an asterix with setRightTitle

Changed 2 months ago by sharvey

You can do this, though (not sure if that's what you mean):

function Form() {
	$reqFields = array(
		'a',
		'b'
	);
	$form = new Form($this, 'Form',
		new FieldSet(
			new TextField('a'),
			new TextField('b')
		),
		new FieldSet(
			new FormAction('submit', 'Submit')
		),
		new RequiredFields($reqFields)
	);
	foreach($reqFields as $field => $value) {
		$fieldObj = $form->dataFieldByName($value);
		$fieldObj->addExtraClass('required');
	}
	return $form;
}

Changed 2 months ago by nicolaas

Hmmm, this does not seem to work ( I am trying to add it to the ecommerce application), but the principles is sound - great

Changed 2 months ago by nicolaas

Note: See TracTickets for help on using tickets.