Yii Framework Forum: Is There Ajaxsubmitbutton In Yii2? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Is There Ajaxsubmitbutton In Yii2?

#1 User is offline   yurkinx 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 30-May 12

Posted 30 October 2013 - 08:13 PM

In Yii 1.x there is CHtml::ajaxSubmitButton that is very useful. If I'm not wrong, I see there is none in Yii2. So, is there any other work around for this?
0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,338
  • Joined: 17-January 09
  • Location:Russia

Posted 31 October 2013 - 05:18 AM

No. Workaround is to write a line of JS yourself.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#3 User is offline   yurkinx 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 30-May 12

Posted 31 October 2013 - 11:22 AM

It's really not a problem, maybe I will rephrase... The problem actually is that I can't validate the form using validateOnSubmit in this case. I want to check that everything is OK and only then submit the form via ajax. Can I fire validation before sending the form in ajax?


For instance, is it possible to make the validation before submitting in this example:
<?php
$this->registerJs("
		  $('.main-form-btn').click(function(){
			$.ajax({
                                type: 'POST',
                                        ...
                                data: $('.comment-form form').serializeArray(),
                                success: function (data) {
                                      ....
                                },
                                error: function (XMLHttpRequest, textStatus, errorThrown) {
                                      ...
                                }
			});
                       return false;
		  });
		",
  yii\base\View::POS_END, 'comment-form');
?>

<div class="comment-form">
<?php $form = ActiveForm::begin(['enableAjaxValidation'=>true,
                                 'validateOnSubmit'=>true]);
 ?>
                <?= $form->field($model, 'content')->textarea(['rows' => 6]) ?>
		<div class="form-group">
			<?= Html::submitButton('Send', ['class' => 'btn main-form-btn']) ?>
		</div>
<?php ActiveForm::end(); ?>
</div>

0

#4 User is offline   nterms 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 26-April 12

Posted 11 March 2014 - 02:06 AM

Hey check this out. This works well for me. No need of extra steps.


<?php $form = ActiveForm::begin([
	'action' => ['create'],
	'enableAjaxValidation' = false,
	'enableClientValidation' = true,
	'beforeSubmit' => "function(form) {
	
		if($(form).find('.has-error').length) {
			return false;
		}
		
		$.ajax({
			url: form.attr('action'),
			type: 'post',
			data: form.serialize(),
			success: function(data) {
				// do something ...
			}
		});
		
		return false;
	}",
]); ?>



0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users