循环中的setFieldAttribute

So i'm just trying to set a field attribute dynamically. Joomla Docs are pretty non saying so how should this work if i want to set all fields to "required"?

 // works
$this->form->setFieldAttribute('phone','required','required');

// not working
$fields = $this->form->getFieldset('contact');
foreach ($fields as $field): 
$this->form->setFieldAttribute($field->name,'required','required');  // this cannot be done directly on a field?
endforeach

It seems $field->name returns the name used on the input field in the rendered form (like 'form[phone]' instead of just 'phone'). You should be able to get the name by using $field->getAttribute('name'):

// This should work :)
$fields = $this->form->getFieldset();
foreach ($fields as $field): 
  $this->form->setFieldAttribute($field->getAttribute('name'),'required','required');  
endforeach;

I agree this is not the most obvious way, you'd expect a method setAttribute(key, val) on the field object...