Form field not be be required for edit

Bill Lions
Member | 48
+
0
-

I have a form with a file upload, and all works well for submitting records.
The form field is a required field for submitting.
However, when using the same form to edit the field, the file field should not be required

$form->addUpload('hero', 'Hero Image:')
    ->setHtmlAttribute('class', 'form-control' )
    ->setRequired('Please select a Hero Image.')
    ->addRule(Form::IMAGE, 'File must be JPEG, PNG or GIF')
    ->addRule(Form::MAX_FILE_SIZE, 'Maximum file size is 1Mb.', 1024 * 1024);

The form used the same createComponent, which is reused for submit and edit.
How do i make this field required for submit, and not-required for edit.

Ondřej Kubíček
Member | 463
+
0
-
$upload = $form->addUpload(...)->...

if (!$isEdit) {
   $upload->setRequired(...);
}
Bill Lions
Member | 48
+
0
-

Ondřej Kubíček wrote:

$upload = $form->addUpload(...)->...

if (!$isEdit) {
   $upload->setRequired(...);
}

Yes, but how does the createComponentMyForm() method know if this is edit or a new post?
Is there something with that can be passed to this method from {control MyForm} ?

Ondřej Kubíček
Member | 463
+
0
-

look at the CD Collection

in DashboardPresenter there are renderAdd and renderEdit methods

dakur
Member | 87
+
0
-

@BillLions There are more ways to achieve that. I would set up a property of presenter, let's say boolean $formEditMode – default to false. Then, in actionEdit() just set it to true and test for $this->formEditMode in createComponent..() method.

Last edited by dakur (2020-09-30 09:37)