Custom 'submit' Messes Up Standard Submit

I have a standard from for submission. Apart from standard submit button, there is another submitButton with custom submit parameter (submission URL).

If a user presses the custom submit button and form do not pass client validation, AND if the user then presses standard submit button, the form is posted to the custom URL.

How to fix this?

Apparently custom submit URL should work with custom button only.

Standard submit button should always post the form to default URL.

Thanks in advance.

Hi Stan,

Please share your view code and the controller code, otherwise I guess no one can answer your question. :)

Hi, softark,

Controller is not involved here. The problem is somewhere in the client.

For the simplicity I made a test using standard ‘test’ project which comes with Yii. All you should do is to add a couple of lines into views/site/login.php. Here is the view after modification:


<?php

/* @var $this SiteController */

/* @var $model LoginForm */

/* @var $form CActiveForm  */


$this->pageTitle=Yii::app()->name . ' - Login';

$this->breadcrumbs=array(

	'Login',

);

?>


<h1>Login</h1>


<p>Please fill out the following form with your login credentials:</p>


<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'login-form',

	'enableClientValidation'=>true,

	'clientOptions'=>array(

		'validateOnSubmit'=>true,

	),

)); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


	<div class="row">

		<?php echo $form->labelEx($model,'username'); ?>

		<?php echo $form->textField($model,'username'); ?>

		<?php echo $form->error($model,'username'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'password'); ?>

		<?php echo $form->passwordField($model,'password'); ?>

		<?php echo $form->error($model,'password'); ?>

		<p class="hint">

			Hint: You may login with <kbd>demo</kbd>/<kbd>demo</kbd> or <kbd>admin</kbd>/<kbd>admin</kbd>.

		</p>

	</div>


	<div class="row buttons">

	<?php              // NEW LINES GO HERE

      echo CHtml::submitButton(Yii::t('t', 'New button'), array('id' => 'newid', 'submit' => CController::createUrl('site/new')));

    ?>

	</div>


	<div class="row rememberMe">

		<?php echo $form->checkBox($model,'rememberMe'); ?>

		<?php echo $form->label($model,'rememberMe'); ?>

		<?php echo $form->error($model,'rememberMe'); ?>

	</div>


	<div class="row buttons">

		<?php echo CHtml::submitButton('Login'); ?>

	</div>


<?php $this->endWidget(); ?>

</div><!-- form -->



When you open the form and enter username/password as demo/demo, then press ‘Login’ button - the client posts your data to site/login and you logged in successfully.

If you open the form, leave the fields empty and press ‘New button’, you’ll get validation errors - that’s ok. But if you then fill in username/password with demo/demo again and press ‘Login’, the troubles begin. This time the form is posted to site/new, despite the fact that you pressed ‘Login’.

Should I add some settings to fix this? Or is it a bug?

Hi Stan,

I could reproduce the problem by following your instruction.

I’m not sure if we should call it a bug or not. But from the description of ‘submit’ option described in the reference of CHtml::clientChange, it should be so-called ‘by design’.

http://www.yiiframework.com/doc/api/1.1/CHtml#clientChange-detail

One thing you can do might be adding ‘submit’ option also to the normal submit button.




<div class="row buttons">

    <?php echo CHtml::submitButton('Login', array('submit' => CController::createUrl('site/login'))); ?>

</div>