SYMFONY FORM isClicked() issue (depending Browser) when mixed with JavaScript - JQuery: event.preventDefault() and event.target.submit() -
the goal:
i want on symfony form have two submit buttons. 1st 1 used validate form, 2nd submit button used away form.
by default fields of form use required
check before submitting, means prior use 2nd submit, required
attributes need turned off on dom <input>
. using jquery, event.preventdefault()
, turn off required
on each <input>
, event.target.submit()
.
then on symfony side expect catch button had been clicked using $form->has([button name])->isclicked()
function.
the issue:
i have noticed depending on browser, $form->has([button name])->isclicked() doesn't work if javascript event.preventdefault()
, event.target.submit()
had been used.
on chrome (version 51.0.2704.103 m) (isclicked = 1 or true):
on firefox (version 47.0) or microsoft edge 25.* (isclicked = false):
the code:
the full code @ following github.
emphasis on code:
in /src/appbundle/form/formtype.php you'll find, code manages javascript hold on submit
, turnoff required
, resume submit
:
public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('field1',emailtype::class, array('label'=>'form field 1')) ->add('submit',submittype::class,array('label'=>'submit form')) ->add('get_away_from_form',submittype::class,array( 'label'=>'get away form', 'attr'=>array( 'onclick'=>'{ //if user click not_submit button required field disabled before submit happens // here part done (see github code details): ////`event.preventdefault()`, //// turn off `required` on each `<input>` //// , `event.target.submit()`. }' ) )); }
in /src/appbundle/controller/defaultcontroller.php you'll find, check on $form->get('get_away_from_form')->isclicked():
if($form->issubmitted() && $form->has('get_away_from_form')){ if($form->get('get_away_from_form')->isclicked() == 1){ $isclicked = 'it works expected: $form->get(get_away_from_form)->isclicked() = '.$form->get('get_away_from_form')->isclicked(); } else { $isclicked = 'it doesn\'t work expected: $form->get(get_away_from_form)->isclicked() = '.$form->get('get_away_from_form')->isclicked(); } }
anyone has idea?
why using submit button away form, instead of using link page ? avoid need bypass form validation
Comments
Post a Comment