I have a form with a field that is required in some instances and not in others so I have changed the required attribute on the fly in the edit view file with the setFieldAttribute function:
if (_condition_) {
$this->form->setFieldAttribute('transprice', 'required', 'false');
}
this works and is reflected in the html output.
However when I try to save the form I'm still getting:
Warning
Field required: Transfer Price
Is there something else I need to consider to get this to work?
Without seeing all your code it's hard to answer this question but our best option would be to use javascript to set and unset the required part of this field. If you're using a form that uses Validation.js then you can dynamically set the requrired
class on the page via javascript.
So long answer short: 1) Leave it un-required in the XML. Because if you're using JForm, JForm is going to rely on that no matter what you set.
2) Dynamically set the required class so validation.js can refer to it and see if the form should be required or not. This is only a client side validation.
3) For server side validation you will need to override the data model and the controller to determine (again) if the field should be required based on your other dynamic data.
Read more about that here: http://docs.joomla.org/Form_field#Client-side_validation