yii2 icon indicating copy to clipboard operation
yii2 copied to clipboard

making validateMultiple work with updateMessages

Open fortm opened this issue 7 years ago • 1 comments

public function actionProfileValidate() {	
				
		$user = new User();
		$model1 = new Model1();
		$model2 = new Model2();
			
		if (Yii::$app->request->isAjax && $user->load(Yii::$app->request->post()) &&
				$model1->load(Yii::$app->request->post()) &&
				$model2->load(Yii::$app->request->post()) 
		) {
			Yii::$app->response->format = 'json';
			return \yii\widgets\ActiveForm::validateMultiple([$model1,$user,$model2]);
		}		
	}

Above validateMultiple does not update errors for form using snippet below in javascript where I have used updateMessages. I did not use yiiActiveForm "validate" method since it was causing redirects to "/' randomly cancelling ajax call. Also "updateMessages" works with ActiveForm::validate() but with ActiveForm::validateMultiple() it does not update error in form programatically from js.

$.ajax({
						url 		: encodeURI(baseUri + "site/profile-validate"),
						data        : $("#CreateProfile").serialize(),
						dataType	: 'json',
						type 		: 'POST',
						beforeSend : function(xhr, settings) {
							$this.attr("disabled","disabled");
                                                        Pace.stop();
							Pace.bar.render();
						},
						success		: function(data) {	
					        $('#CreateProfile').yiiActiveForm("updateMessages", data);
							if($("form#CreateProfile").find('.has-error').length == 0) {							                				
								$.ajax({
									url 		: encodeURI(baseUri + "site/profile"),
									data        : $("#CreateProfile").serialize(),
									dataType	: 'json',
									type 		: 'POST',
									beforeSend : function(xhr, settings) {
									},
									success		: function(data) {													
									},
									error		: function(data) {
									},
									complete	: function() {
										$this.removeAttr("disabled");
										Pace.stop();
									},
								 });								
							} else {
								$this.removeAttr("disabled");
								Pace.stop();
							}
						},
						error		: function(data) {
						},
						complete	: function() {							
						},
				 });

Above code shows how I am using disabled state and Pace loader with submit button while form itself looks as below -

<?php $form = ActiveForm::begin([
				"id"=>"CreateProfile",
				'enableClientValidation' => true,
				'enableAjaxValidation' => true,
				'validationUrl'=>['site/profile-validate'],
				"options" => ["enctype" =>"multipart/form-data"]
				]
); ?>

There is submit button here on click of while my js gets called as shown above. So rest of validation yii takes care of except on submit button press. The press of submit button is being done is js and not updating error messages for multiple model forms though it uses same URL for validation

fortm avatar Mar 13 '17 10:03 fortm

did you get $('#CreateProfile').yiiActiveForm("updateMessages", data); to work? doesn't seem to work for me.

srakl avatar May 26 '23 10:05 srakl