1

In the admin side I have a XML form with 2 fields as below. Both the fields are marked as required=true

<field name="field1"
  type="text"
  label="Field1"
  required="true"/>

<field name="field2"
  type="text"
  label="Field2"
  required="true"/>

In the admin form (default.php) the fields are sown as below. field2 is required only on certain condition.

echo $this->form->renderField('field1');
if (condition){
  echo $this->form->renderField('field2');
}

When this form is getting submitted the server side validation validates both the fields irrespective on what is displayed on the form. I want to setup in such a way that validation only validates what's in the form.

1 Answer 1

2

I managed to achieve this via setFieldAttribute. In the XML form I removed the field2 required property and set that dynamically in the form as below.

echo $this->form->renderField('field1');
if (condition){
  $this->form->setFieldAttribute('field2','required','required');
  echo $this->form->renderField('field2');
}

Not sure whether this is the right approach however it works great...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.