Skip to content

Commit

Permalink
dispatch submit event from div.ui.form to native form
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed May 21, 2023
1 parent ff7f4be commit d7342c2
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ protected function init(): void
parent::init();

$this->formElement = View::addTo($this, ['element' => 'form', 'shortName' => 'form'], ['FormElementOnly']);
$this->on('submit', new JsExpression('if (event.target === this) { []; }', [$this->js(false, null, $this->formElement)->form('submit')]));

// Initialize layout, so when you call addControl / setModel next time, form will know
// where to add your fields.
Expand Down Expand Up @@ -167,14 +168,11 @@ protected function initLayout(): void
->addMoreInfo('layout', $this->layout);
}

// allow to submit by pressing an enter key when child control is focused
$jsSubmit = $this->js()->form('submit');
$this->on('submit', new JsExpression('if (event.target === this) { []; }', [$jsSubmit]));

// Add save button in layout
// add save button in layout
if ($this->buttonSave) {
$this->buttonSave = $this->layout->addButton($this->buttonSave);
$this->buttonSave->setAttr('tabindex', 0);
$jsSubmit = $this->js()->form('submit');
$this->buttonSave->on('click', $jsSubmit);
$this->buttonSave->on('keypress', new JsExpression('if (event.keyCode === 13) { []; }', [$jsSubmit]));
}
Expand Down Expand Up @@ -509,7 +507,9 @@ protected function renderTemplateToHtml(): string

public function fixOwningFormAttrInRenderedHtml(string $html): string
{
return preg_replace('~<(button|fieldset|input|output|select|textarea)(?!\w| form=")~i', '$0 form="' . $this->formElement->name . '"', $html);
return preg_replace_callback('~<(?:button|fieldset|input|output|select|textarea)(?!\w| form=")~i', function ($matches) {
return $matches[0] . ' form="' . $this->getApp()->encodeHtml($this->formElement->name) . '"';
}, $html);
}

/**
Expand Down

0 comments on commit d7342c2

Please sign in to comment.