YII ajax validation: The error message doesnt appear afer ajax validation - ajax

I have 2 fields in the form (num1 and num2), sum of which forms variable, entered to the databases (sum), which is required. There are some other required fields .
array('summ ', 'required'),
In other words in order to get this variable I need two of these fields (num1 and num2). Without ajaxom validation I solve this problem by:
if($_POST['num1'] && $_POST['num2']) {
$model->sum = $_POST['num1'] + $_POST['num2'];
} else {
$model->sum='';
}
Here is code of form’s part:
<?php echo $form->labelEx($model, 'bithday', array('class' => 'label_register')); ?>
<input name="num1" type="text" value="">
<input name="num2" type="text" value="">
<?php echo $form->error($model, 'bithday'); ?>
After forwarding ajax request, the response containing error messages. But the error doesn’t appear. Though errors of other required fields appear correctly.
Response comes in form of json, with key and it’s value. (Data about this error is contained here)
How to solve this problem? .

Use the Yii textfield like so:
<?php echo $form->labelEx($model, 'num1', array('class' => 'label_register'));
<?php echo $form->textField($model,'num1'); ?>
<?php echo $form->error($model, 'num1'); ?>
<?php echo $form->labelEx($model, 'num2', array('class' => 'label_register'));
<?php echo $form->textField($model,'num2'); ?>
<?php echo $form->error($model, 'num2'); ?>
And add the properties $num1 and $num2 to your model.

Related

(Codeigniter) Ion Auth CSRF Error:This form post did not pass our security checks (when loading views)

I'm using Ion Auth authentication library in Codeigniter. When I load my footer view, I get an CSRF Error(This form post did not pass our security checks). When I remove the footer view, it works fine though! Is there anything I'm doing wrong here? Thanks!
function edit_user($id) {
//I'm only posting the last part of the code of edit_user function in the auth controller
$this->load->view('layout/header');
$this->_render_page('auth/edit_user', $this->data);
$this->load->view('layout/footer'); // I'm getting an error when I load this footer view.
}
This is the code in my views.
<h1><?php echo lang('edit_user_heading');?></h1>
<p><?php echo lang('edit_user_subheading');?></p>
<div id="infoMessage"><?php echo $message;?></div>
<?php echo form_open(uri_string());?>
<p>
<?php echo lang('edit_user_fname_label', 'first_name');?> <br />
<?php echo form_input($first_name);?>
</p>
<p>
<?php echo lang('edit_user_lname_label', 'last_name');?> <br />
<?php echo form_input($last_name);?>
</p>
<p>
<?php echo lang('edit_user_company_label', 'company');?> <br />
<?php echo form_input($company);?>
</p>
<p>
<?php echo lang('edit_user_phone_label', 'phone');?> <br />
<?php echo form_input($phone);?>
</p>
<p>
<?php echo lang('edit_user_password_label', 'password');?> <br />
<?php echo form_input($password);?>
</p>
<p>
<?php echo lang('edit_user_password_confirm_label', 'password_confirm');?><br />
<?php echo form_input($password_confirm);?>
</p>
<h3><?php echo lang('edit_user_groups_heading');?></h3>
<?php foreach ($groups as $group):?>
<label class="checkbox">
<?php
$gID=$group['id'];
$checked = null;
$item = null;
foreach($currentGroups as $grp) {
if ($gID == $grp->id) {
$checked= ' checked="checked"';
break;
}
}
?>
<input type="checkbox" name="groups[]" value="<?php echo $group['id'];?>"<?php echo $checked;?>>
<?php echo $group['name'];?>
</label>
<?php endforeach?>
<?php echo form_hidden('id', $user->id);?>
<?php echo form_hidden($csrf); ?>
<p><?php echo form_submit('submit', lang('edit_user_submit_btn'));?></p>
<?php echo form_close();?>
Ion auth csrf protection is older. CI-2 already have
This is provide to security when posting form, ex: POST is from local or server?
ion auth controller file, you see like codes below:
// do we have a valid request?
if ($this->_valid_csrf_nonce() === FALSE)
{
show_error($this->lang->line('error_csrf'));
}
If you remove these validation, you will not get csrf error
You can enable security with CI core lib
http://ellislab.com/codeigniter/user-guide/libraries/security.html
$config['csrf_protection'] = TRUE;
You have to use form_open() tag to triggger csrf protection.
Same problem happened to me when i was trying to add common/header and common/footer to the auth/reset_password page.
Issue was that I was using relative paths for the JS included in the footer part. After a lot of experiment using the base_url() fixed the issue.
I have the sale pb but a replace flashdata by userdata, it's just a little less secure but it's a good solution.

Ajax validation in Yii form

In my form I want to make an ajax validation for the radio buttons of gender.
I have an attribute gender in the model users, at the rules of model I put gender as a required field, but when I submit the form without selecting any gender the validation doesn't work to tell me that the gender cannot be blank, here is my code:
<div class="row">
<?php echo $form->labelEx($model,'gender'); ?>
<?php echo $form->radioButton($model,'gender',array('value'=>'0')) . 'Male<br>'; ?>
<?php echo $form->radioButton($model,'gender',array('value'=>'1')) . 'Female'; ?>
<?php echo $form->error($model,'gender'); ?>
</div>
You should be using radioButtonList() instead of radioButton().
<?php echo $form->labelEx($model,'gender'); ?>
<?php echo $form->radioButtonList($model,'gender',array('1'=>'Male','2'=>'Female')); ?>
<?php echo $form->error($model,'gender'); ?>

yii ajax validation errors show up

I have the following form:
$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'new-agenda-entry',
'enableAjaxValidation'=>true,
'action'=>'')); ?>
<div class="four columns">
<div class="right top5 sufix10">
<?php echo $form->labelEx($model,'eventTime'); ?>
<?php $this->widget('zii.widgets.jui.CJuiDatePicker', array(
'name'=>'UserAgenda[eventTime]',
'model'=>$model,
'options'=>array(
'dateFormat'=>'yy-mm-dd',
'minDate'=>'new Date()',
) // jquery plugin options
));?>
<?php echo $form->error($model, 'eventTime'); ?>
</div>
<div class="right top5 sufix10">
<?php echo CHtml::dropdownList('eventTime_hour', false, $this->getHourArray()); ?>
<?php echo CHtml::dropdownList('eventTime_min', false, array('00'=>'00', '30'=>'30')); ?>
</div>
<?php echo $form->labelEx($model,'note'); ?>
<?php echo $form->textField($model, 'note');?>
<?php echo $form->error($model, 'note'); ?>
The model is successfully validated on the server as I can see the response in Firebug:
{"UserAgenda_eventTime":["Event time cannot be blank."],"UserAgenda_note":["Note cannot be blank."]}
The message seems fine, so does the html but the hidden inputs generated by yii remain hidden and empty.
in the action maybe (create or update) you have to uncomment // $this->performAjaxValidation($model); like this you have an ajax validation, if the problem persist I think posting it to yiibootstrap discussion in yii forums i better, I see that you use yiibootstrap, try it with default yii form widget.

Yii validation: some errors are not displayed

Here is my rules method:
public function rules() {
$newRules = array(
array('password_verification, valid_from, valid_until', 'required'),
array('password_verification', 'length', 'min'=>6, 'max'=>32),
array('password_verification', 'compare', 'compareAttribute'=>'password'),
array('username, email', 'length', 'min'=>3,'max'=>255),
array('password', 'length','min'=>6, 'max'=>32), array('username, email', 'unique'),
array('email', 'email'), array('id, type, username, password, email, valid_from, valid_until', 'safe'),
);
return array_merge($newRules,parent::rules());
}
And here is my view (fields which give a hard time):
<div class="row">
<?php echo $form->labelEx($user,'password_verification',array('label'=>'Verification mot de passe','class'=>'forsys-label')); ?>
<?php echo $form->passwordField($user,'password_verification',array('size'=>30,'maxlength'=>32)); ?>
<?php echo $form->error($user,'password_verification') ?>
</div>
<div class="row">
<?php echo $form->labelEx($user,'valid_from',array('label'=>'Valide depuis le','class'=>'forsys-label')); ?>
<?php echo $form->dateField($user,'valid_from'); ?>
<?php echo $form->error($user,'valid_from'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($user,'valid_until',array('label'=>"Valide jusqu'au",'class'=>'forsys-label')); ?>
<?php echo $form->dateField($user,'valid_until'); ?>
<?php echo $form->error($user,'valid_until'); ?>
</div>
Some informations:
enableClientValidation and enableAjaxValidation are both at true
datefield is a costum method. It's not where the problem come from (a colleague use it in the same way I use it without this problem).
My Problem:
For "valid_until" and "valid_from" errors are not displayed on the HTML.
If I let "password_verification" empty, when I change the focus an error appears (because of ajaxValidation onChange), but none errors if I let "valid_until" or "valid_from" empty.
But these errors are noticed by Yii, I can see them in firebug if I check the ajax request response.
So if all fields are empty, nothing will be created in DB because of 3 errors, but only one (password_validation) will be displayed.
If any body have an idea, you're welcome :)
Sorry for my approximate english
Thank you for read me, have a good day.
Michaël
You have these attributes marked as "safe" in the last rule. Probably that's the reason. Remove that rule or add a scenario for it (usually attributes are declared as safe on 'search' scenario - e.g. add 'on'=>'search' element to that rule).

populate dropdown in codeigniter form using set_value

i have a form that uses a drop down and im using codeigniter , form helper and form validation
so when i get a validation error in my form , all the correctly entered fields are populated using set_value of codeigniter , however this doesnt work for dropdown
im doing :
<?php echo form_dropdown('mid', $id_map, set_value('mid'), 'id="mid"') ?>
when get error in form , always the first value of dropdown is selected and not previously set
Any ideas , what am i doing wrong?
The correct method to use set_select() is
<select name="myselect">
<option value="one" <?php echo set_select('myselect', 'one', TRUE); ?> >One</option>
<option value="two" <?php echo set_select('myselect', 'two'); ?> >Two</option>
<option value="three" <?php echo set_select('myselect', 'three'); ?> >Three</option>
</select>
So Andras Ratz method/answer won't work.
Your method is correct.
<?php echo form_dropdown('mid', $id_map, set_value('mid'), 'id="mid"') ?>
However remember that set_value() is only going to return the value if you have setup a validation rule for it, in your case for mid. You could try as below -
<?php echo form_dropdown('mid', $id_map, (isset($_POST['mid']) ? $_POST['mid'] : ''), 'id="mid"') ?>
or just set a validation rule for mid too
It's because you have to use the set_select() function , not the set_value();
You can check that here:
http://codeigniter.com/user_guide/helpers/form_helper.html
And the right syntax:
EDIT :
<?php echo form_dropdown('mid', $id_map, set_select('mid'), 'id="mid"'); ?>
If you have a default value, that should go in the 2. parameter for set_select.
I hope this will work.
<?php // Change the values in this array to populate your dropdown as required
$options = array(
'active' => 'Active',
'inactive' => 'Inactive',
);
echo form_dropdown('mid', $options,$id_map, ' id="mid"');
?>
What about if the the form select content is selected from a foreach loop like this
<?php foreach ($roles as $role) {
echo "<option value=" . $role->id . ">" . $role->role_name . "</option>";
}
?>
If you do not have a validation rule on the actual dropdown field, then
echo form_dropdown('mid',$id_map);
is sufficient.

Resources