In my yii application i have a simple form that contains two fields like this:
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'message-form',
'enableClientValidation' => true,
//'enableAjaxValidation' => true,
'clientOptions' => array(
'validateOnSubmit' => true,
'validateOnChange' => false,
'validateOnType' => false,
'errorCssClass' => 'has-error',
'successCssClass' => 'has-success',
)));
?>
<div class="form-group">
<div class="row">
<div class="col-md-6">
<?php echo $form->labelEx($model, 'subject'); ?>
<?php echo $form->textField($model, 'subject', array('class' => 'form-control')); ?>
<?php echo $form->error($model, 'subject', array('class' => 'alert alert-danger')); ?>
</div>
<div class="col-md-5"></div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-6">
<?php echo $form->labelEx($model, 'message'); ?>
<?php echo $form->textField($model, 'message', array('class' => 'form-control', 'id'=>'message')); ?>
<?php echo $form->error($model, 'message', array('class' => 'alert alert-danger')); ?>
</div>
</div>
</div>
......
//remaining of code
"subject" and "message" are two fields of "Notification" model. in Notification model, i have defined this rule:
array('subject, message', 'required')
my problem is validation for "subject" works but validation for "message" not works!
after submitting the form, error message of "subject" shows up but "message" does not have any error. can anyone help me solve this problem?
I use Yii 1.1.15
in http://www.yiiframework.com/doc/api/1.1/CActiveForm u can see example
<?php $form = $this->beginWidget('CActiveForm', array(
'id'=>'user-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'focus'=>array($model,'firstName'),
)); ?>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'firstName'); ?>
<?php echo $form->textField($model,'firstName'); ?>
<?php echo $form->error($model,'firstName'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'lastName'); ?>
<?php echo $form->textField($model,'lastName'); ?>
<?php echo $form->error($model,'lastName'); ?>
</div>
<?php $this->endWidget(); ?>
i think u set enableAjaxValidation = true
Is there a reason for HTML attribute "id" => "message" on your message field? Yii CActiveForm generate a specific HTML id : modelName_fieldName for each field.
You don't need to set id manually (like your subject field; it don't have id). Yii needs those format of ids for validations, get/post data and more things (as client/js functions).
Remove "id" => "message" on your message field and test again.
Related
Problem: I have three forms in one page, that all have the same bug: I need to click the submit button twice in order to submit the form.
I have discovered that this is a common bug and usually triggered by naming the button "submit", however this is not the case.
Here's my code:
(First form: Change Password)
<div id="change-password-form">
<?php $changePasswordForm = ActiveForm::begin([
'id' => 'change-password-form',
"options" => ["class" => "animated-label"],
"fieldConfig" => ["template" => "{input}\n{label}\n{hint}\n{error}"],
]); ?>
<div class="row">
<div class="col-md-6">
<?= $changePasswordForm->field($changePassword, 'newPassword')->passwordInput() ?>
</div>
<div class="col-md-6">
<?= $changePasswordForm->field($changePassword, 'repeatNewPassword')->passwordInput() ?>
</div>
</div>
<div class="row">
<div class="col-md-6">
<?= $changePasswordForm->field($changePassword, 'oldPassword')->passwordInput() ?>
</div>
<div class="col-md-6">
<?= Html::submitButton(
'Change Password',
[
'class' => 'btn btn-primary',
"style" => "width: 100%",
'name' => 'submit-change-password',
'id' => 'submit-change-password',
]
) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
</div>
(Second form: Change Email)
<div id="change-email-form">
<?php $changeEmailForm = ActiveForm::begin([
'id' => 'change-email-form',
"options" => ["class" => "animated-label"],
"fieldConfig" => ["template" => "{input}\n{label}\n{hint}\n{error}"],
]); ?>
<div class="row">
<div class="col-md-6">
<?= $changeEmailForm->field($changeEmail, 'password')->passwordInput() ?>
</div>
<div class="col-md-6">
<?= $changeEmailForm->field($changeEmail, 'email')->textInput() ?>
</div>
</div>
<div class="row">
<div class="col-md-6 col-md-offset-6">
<?= Html::submitButton(
'Change Email',
[
'class' => 'btn btn-primary',
"style" => "width: 100%",
'name' => 'submit-change-email',
'id' => 'submit-change-email',
]
) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
</div>
(Third form: Change Username)
<div id="change-username-form">
<?php $changeUsernameForm = ActiveForm::begin([
'id' => 'change-username-form',
"options" => ["class" => "animated-label"],
"fieldConfig" => ["template" => "{input}\n{label}\n{hint}\n{error}"],
]); ?>
<div class="row">
<div class="col-md-6">
<?= $changeUsernameForm->field($changeUsername, 'password')->passwordInput() ?>
</div>
<div class="col-md-6">
<?= $changeUsernameForm->field($changeUsername, 'username')->textInput() ?>
</div>
</div>
<div class="row">
<div class="col-md-6 col-md-offset-6">
<?= Html::submitButton(
'Change Username',
[
'class' => 'btn btn-primary',
"style" => "width: 100%",
'name' => 'submit-change-username',
'id' => 'submit-change-username',
]
) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
</div>
As you can see, the submit buttons have both unique IDs and names, they are correctly resolved to the desired HTML code.
E.g. the submit button of "change password" looks like this:
<button type="submit" id="submit-change-password" class="btn btn-primary" name="submit-change-password" style="width: 100%">Change Password</button>
Are there any solutions or workarounds for this case? I have only found the solution about renaming the submit buttons.
for anyone looking for an answer to this particular situation, in my case, I solve it by removing the extra attribute on the active form begin part.
Example: Before
ActiveForm::begin(['id' => 'registration-form'])
After
ActiveForm::begin()
Now I am not really sure, how this was causing the problem. I have other forms in which I have the extra attributes and it works completely fine on those forms. I check the console for errors but could not find any. My guess is on my particular form with this issue, I was using external js file required for plugin and somehow it was causing some kind of conflict with yii validation or bootstrap js files.
I am using XMultiSelects Widget to post large data in form.(Yii Framework)
<div class="row">
<?php echo $form->labelEx($model, 'list'); ?>
<div style="display: inline-block">
<?php
$this->widget('multiselects.XMultiSelects', array(
'id' => 'hd-programs', //since there are multiple selector pairs on this page we need to set identifier
'leftTitle' => 'Available',
'leftName' => 'availableList',
'leftList' => $availableList,
'rightTitle' => 'Selected',
'rightName' => get_class($model) . '[list][]',
'rightList' => $list,
'size' => 15,
'width' => '350px',
));
?>
<?php echo $form->error($model, 'list'); ?>
</div>
where $avaliableList is very large data set.
Now when i print value of form using print_r($_Post['MyFormName']) $list is not posted.
Any suggestion how could i handle large data with multiselect widget.
Hello guys I am doing a form in codeigniter to display error message. My CSS framework is Bootstrap 3.
My codes so far:
View.php
<div class="form-group">
<?php
$label_attributes = array('class' => 'col-sm-2 control-label');
echo form_label('Load Balance', 'load_balance', $label_attributes);
?>
<div class="col-xs-3">
<?php
$load_balance_attrs = array('class' => 'form-control',
'id' => 'load_balance',
'placeholder' => 'Load Balance',
'name' => 'load_balance',
'type' => 'number',
'min' => '0',
'step' => '1', );
echo form_input($load_balance_attrs);
?>
<?php echo form_error('load_balance',
'<span class="glyphicon glyphicon-remove form-control-feedback"></span>
<p class="help-block">', '</p>'); ?>
</div>
</div>
I am trying to get this example but I have no idea how to append .has-error to my .form-group to display the error message. Do you have any ideas how to do that? Your help will be greatly appreciated. Thanks.
You may try something like this:
<div class="form-group <?php if(validation_errors()) echo 'has-error'; ?>">
Instead of:
<div class="form-group">
I'm new to Yii framework, I need to create a confirm dialog to pop up before I submit a form. Below is the code of the form used to approve and reject. I need a pop up to appear before I submit to confirm whether approved or rejected.
<div class="row">
<?php echo $form->labelEx($model,'Approved'); ?>
<?php
echo $form->radioButtonList($model, 'Approved',
array( 1 => 'Approved',
0 => 'Rejected',
),
array(
'labelOptions'=>array('style'=>'display:inline'), // add this code
'separator'=>' ',
) );
?>
<?php echo $form->error($model,'Approved'); ?>
</div>
Edit
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
How can I achieve this
You can add htmlOptions into your submitButton like:
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save',
array('confirm'=> 'Are you Sure')); ?>
It will show a confirm dialog when you click submitButton.
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', [
'class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary',
'data' => [
'confirm' => 'Are you sure want to Create/Update this message?'
]
]) ?>
Use data attribute.
I have a login form in my header that talks to my users_controller but the form itself isn't in a view being generated by the users controller so I get two problems
1.) The password field doesn't get treat like a password field and is just a normal text field
2.) When submitting the form it just redirects to the login action
Here is the code and it's used in BOTH the login view and in my header (so I know it works):
<?php echo $this->Form->create(null, array('id' => 'loginform', 'type' => 'post',
'url' => array('controller' => 'users', 'action' => 'login'))); ?>
<fieldset id="login">
<ul class="clearfix">
<li id="li-username">
<?php echo $this->Form->input('username', array('label'=>false,'placeholder'=>'Username or email address')); ?>
</li>
<li id="li-password">
<?php echo $this->Form->input('password', array('label'=>false,'placeholder'=>'Password')); ?>
<span id="iforgot"><?php echo $this->Html->link('?',
array('controller' => 'users', 'action' => 'forgotpassword'), array('title' => 'Forgot your password?')); ?></span>
</li>
<li id="li-submit">
<button type="submit" title="Log in">Log in ►</button>
</li>
</ul>
</fieldset>
<?php echo $this->Form->end(); ?>
Why not changing
Form->create(null,...
to
Form->create('User',...
otherwise change
Form->input('username',...
to
Form->input('User.username',...
You can use type = 'password'
echo $this->Form->input('password', array('label'=>false, 'type' => 'password', 'placeholder'=>'Password'));
You have specified array('controller' => 'users', 'action' => 'login') in your $this->Form->create() statement. So the form is submitted to "/users/login"
If you don't want to submit the form to '/users/login', you can use AJAX to perform login.
Hope these will answer your queries.