Codeigniter form_error with Bootstrap 3 - php

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">

Related

yii2 disable validation on hidden fields

I have created a dev that is changed according to the selected radio button.
the dev has input fields:
<div id='div-cash' class="toHide" hidden="true">
<div class="row" >
<div class="col-md-4">
<?php echo $form->field($cash[0], 'form_get',[
'template' => '{label}<div class="input-group">{input}
<span class="input-group-addon">pt</span></div>{error}{hint}',
'options' => ['class' => 'form-inline toHide cashtoHide', 'style' => 'margin-top:2rem'],
'labelOptions' => ['style' => 'margin-right:2rem'],
])->textInput();?>
</div>
</div>
</div>
when I submit the form the inputs of the other options are validated and I want to disable it
I used Conditional Validation to solve this problem
'whenClient' => "function (attribute, value) {
return $('input[type=radio]:checked').val() == 'cash';
}"

Yii2: Need to click submit button twice, name and id is not "submit"

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.

Codeigniter 3.x / form_input() / preappend or append / bootstrap 3.x

How is is possible to include a prepend icon or an append icon using form_input();
<?php
$email = array(
'name' => '',
'value' => '',
'id' => null,
'placeholder' => 'Email',
'class' => 'mail form-control'
);
echo form_input($email);
?>
What I would like to do is add, for example, the following bootstrap 3 styling to the email input box:
<span class="input-group-addon" id="basic-addon2">#example.com</span>
I am unsure how that would fit into the array. I have looked at the manual for CI3, however, it does not mention anything in particular. Any suggestions, etc, appreciated.
This is what I am aiming for:
Unfortunately, you can't. form_input() is used to build the input element only. If you take a closer look at the html structure. <span> comes right after <input> and not within <input>. So, you have to add it after the input element
....
<?php echo form_input($email); ?>
<span class="input-group-addon" id="basic-addon2">#example.com</span>
...
Taking code from bootstrap website:
<form class="form-horizontal">
<div class="form-group has-success has-feedback">
<label class="control-label col-sm-3" for="inputSuccess3">Input with success</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="inputSuccess3" aria-describedby="inputSuccess3Status">
<span class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true"></span>
<span id="inputSuccess3Status" class="sr-only">(success)</span>
</div>
</div>
<div class="form-group has-success has-feedback">
<label class="control-label col-sm-3" for="inputGroupSuccess2">Input group with success</label>
<div class="col-sm-9">
<div class="input-group">
<span class="input-group-addon">#</span>
<?php
$email = array(
'type' => 'text',
'class' => 'form-control'
'id' => 'inputGroupSuccess2',
'aria-describedby' => 'inputGroupSuccess2Status',
'name' => 'email',//name field is mandatory to be able to control it in form_validation library
'placeholder' => 'Email',
);
echo form_input($email);
?>
</div>
<span class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true"></span>
<span id="inputGroupSuccess2Status" class="sr-only">(success)</span>
</div>
</div>
</form>
you can see that span tag and input tag doesn't collide. So you would just use:
<?php
$email = array(
'name' => '',
'value' => '',
'id' => null,
'placeholder' => 'Email',
'class' => 'mail form-control'
);
echo form_input($email);
?>
instead of bootstrap input field. Everything else can stay the same in view file. If you would like to make input field like second one from example above, you should code it like:
<?php
$email = array(
'type' => 'text',
'class' => 'form-control'
'id' => 'inputGroupSuccess2',
'aria-describedby' => 'inputGroupSuccess2Status',
'name' => 'email',//name field is mandatory to be able to control it in form_validation library
'placeholder' => 'Email',
);
echo form_input($email);
?>
This could be example like "Optional icons in horizontal and inline forms" on Forms page.

Yii get pagesize in view of ClistView

How would i get the current pagesize in my View? I want to be able to add a banner when half the pagesize is reached.
If the page size is 10, insert a banner at position 5
i tried adding this in my view.php
if ($index == ($dataProvider->pagination->pageSize / 2)) { ... }
and this my index.php
$this->widget ( 'site.common.extensions.EListView.EListView', array (
'dataProvider' => $dataProvider,
'itemView' => '_view_gallery',
'pagerCssClass' => 'pagination',
'template' => '
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-md-12">
<div class="pull-right">{sorter}</div>
<div class="pull-left"></div>
</div>
</div>
<div class="row">
<div class="col-md-12">{items}</div>
</div>
<div class="row padding-top-20px">
<div class="col-md-12">
{pager}
</div>
</div>
</div></div>',
'pager' => array (
'id' => '_pagination',
'class' => 'LinkPager',
'header' => '',
'htmlOptions' => array (
'class' => 'pagination'
)
),
'emptyText' => '<div class="alert alert-info">No result found.</div>'
)
);
but nothing happens.
I'm currently using this extension to create a perpage page size.
http://www.yiiframework.com/extension/elistview
also tried without using this extension, but still no luck. I'm using Yii 1.1.16.
any ideas?
In your _view_gallery.php you can use special variables available for CListView and CGridView (http://www.yiiframework.com/wiki/252/special-variables-in-cgridview-and-clistview/) in order to get the pageSize.
That could look like this:
if($widget->dataProvider->pagination->pageSize == 10){
//do something here
}
Accessing $dataProvider directly won't result in anything because that variable means nothing to the view.

yii CActiveForm client validation not working on some field

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.

Categories