Resetting dropdown values after failed validation in CodeIgniter - codeigniter

I have a very simple voting form. It contains a single text field for entry ID, then five dropdowns. The only validation being done is on the text field, but if it fails, I want all of the dropdowns to keep their values.
For example, a user enters 12345 as the entry, then selects option A from all five dropdowns. (Option B is default.) If 12345 fails validation, the dropdowns should still be set to option A when the validation error is displayed. That’s not happening. I’m using the form helper’s form_dropdown to create the dropdowns. Any ideas? As you can see, I tried using set_value on the dropdown called q1, but it doesn’t work. The validation itself does work.
Here’s the code for the form:
<?php
$hidden = array('dot_judge_id' => $this->session->userdata('dot_judge_id'));
echo form_open('vote/create');
$entry_code_data = array(
'name' => 'entry_code',
'id' => 'entry_code',
'value' => set_value('entry_code')
);
echo form_hidden($hidden);
$score_options = array('1'=>'1 (lowest)', '2'=>'2','3'=>'3', '4'=>'4','5'=>'5 (highest)');
?>
<p><label for="entry_code">Entry code: </label><?php echo form_input($entry_code_data); ?></p>
<p><label for="q1">Question 1: </label><?php echo form_dropdown('q1', $score_options, set_value('q1')); ?></p>
<p><label for="q2">Question 2: </label><?php echo form_dropdown('q2', $score_options, ''); ?></p>
<p><label for="q3">Question 3: </label><?php echo form_dropdown('q3', $score_options, ''); ?></p>
<p><label for="q4">Question 4: </label><?php echo form_dropdown('q4', $score_options, ''); ?></p>
<p><label for="q5">Question 5: </label><?php echo form_dropdown('q5', $score_options, ''); ?></p>
<p><?php echo form_submit('submit', 'Submit vote'); ?></p>
<?php echo form_close(); ?>
<?php echo validation_errors('<p class="error">', '</p>');?>
I've had someone on the CodeIgniter forum tell me to use set_select, but I don’t think I can use set_select, because I’m not writing out the individual values of the select box, nor am I trying to provide a hard-coded default value. I need to reset the dropdowns to the values the user selected prior to running the form validation. I’m using the form_dropdown function, so how do I get the actual selected value to pass into the second param of set_select? According to the set_select documentation,
The first parameter must contain the name of the select menu, the
second parameter must contain the value of each item
There was also a comment on this post that said to use set_value -echo form_dropdown('events', $options, set_value('events'), $firstItem);. The comment got three votes, so I'm assuming that it works for some people, but doesn't do anything for me. That question was a year old; perhaps that worked in an older version of CI but not the newest one. In any case, there HAS to be a way to have "sticky" dropdown values when creating dropdowns using form_dropdown, doesn't there? I can't imagine that I'd have to do it the long way and create the dropdown by looping through an array of values and echoing out each option line separately; that kind of defeats the purpose of using something like CI in the first place.

Remember that set_value() requires a second parameter that is the value of each item, so you can get this to work by adding $this->input->post('q1') as the second parameter.
Instead of:
<?php echo form_dropdown('q1', $score_options, set_value('q1')); ?>
You would write:
<?php echo form_dropdown('q1', $score_options, set_value('q1', $this->input->post('q1'))); ?>
Hope that helps.

much easier,
<select name="add_fields_type" id="add_fields_type">
<option value="">Please Select</option>
<option value="Input">Input</option>
<option value="Text Area">Text Area</option>
<option value="Radio Button">Radio Button</option>
<option value="Check Box">Check Box</option>
</select>
<script>
document.getElementById('select_status').value="<?php echo set_value('status'); ?>";
</script>

Related

Shipping methods are not displayed accordingly in Magento 1.8

How can I display the list of my shipping methods according to the order numbers that I have set in their input fields of Sort Order?
I have set each shipping method's Sort Order in my backoffice, but they are not displayed accordingly.
For instance, in my backoffice, I have these shipping methods,
Method Name: bestway
Sort Order: 1
Method Name: Free
Sort Order: 2
Method Name: Chronopost
Sort Order: 3
But they are not displayed in this order in frontend\argenties\default\template\checkout/cart/shipping.phtml
<?php
$methods = Mage::getSingleton('shipping/config')->getActiveCarriers();
$shipping = array();
foreach($methods as $_ccode => $_carrier):
if($_methods = $_carrier->getAllowedMethods()):
if(!$_title = Mage::getStoreConfig("carriers/$_ccode/title"))
$_title = $_ccode;
foreach($_methods as $_mcode => $_method):
$_code = $_ccode . '_' . $_mcode;
$shipping[$_code] = array('title' => $_method,'carrier' => $_title);
?>
<li><input type="radio" name="estimate_method" value="<?php echo $_code;?>" id="s_method_<?php echo $_code ?>"<?php if($_code === $this->getAddressShippingMethod()) echo ' checked="checked"' ?>> <span class="item-text-shipping-method"><?php echo $_title;?></span></li>
<?php endforeach;?>
<?php endif;?>
<?php endforeach;?>
Result,
Method Name: Free
Method Name: bestway
Method Name: Chronopost
Method Name: bestway should be on the top in the list, but it is always dropped to the second. Why? Any idea how I can fix it?
Please be aware of the following facts:
Custom Shipping Methods (also known as offline shipping) will always display before the Real-Time Shipping methods.
Shipping methods within each carrier will display in order of price (lowest to highest.)
<?php
foreach ($_shippingRateGroups as $code => $_rates):?>
<?php foreach ($_rates as $_rate): ?>
<li>
<input name="estimate_method" type="radio" value="<?php echo $this->escapeHtml($_rate->getCode()) ?>" id="s_method_<?php echo $_rate->getCode() ?>"<?php if($_rate->getCode()===$this->getAddressShippingMethod()) echo ' checked="checked"' ?> class="radio" />
<span class="item-text-shipping-method">
<?php echo $this->getCarrierName($code);?>
</span>
</li>
<?php endforeach; ?>
Try this code, i alerted this code form base/default/checkout/cart/shipping.phtml file
The shipping methods are sorted based upon price, and when they are all the same price, it doesn't look at other fields determine which one should come first.
I had a similar issue, where I had my delivery methods on one set price, so I added additional sorting criteria to Mage/Shipping/Model/Rate/Results.php file particularly the sortRatesByPrice() function.
If you want to make changes to core files, be sure to copy them to your local pool.

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

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.

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).

Magento Custom options in grouped products page. Price not reflecting on selection

I had a customized grouped product page in a magento store. I can able to put custom option values in that grouped product page by simply editing grouped.phtml file with this code.
foreach($product->getOptions() as $o):
$values = $o->getValues();
I can able to publish custom option title and its value using this code;
echo $v->getData('title');
echo ('+').$v->getData('price');
But the problem is i can't able to reflect the price on custom option selection.
When i go through the view.phtml file, i can able to see that a function is calling on custom option fields so that price is reflecting.
So i rewrite my custom options select box code like this
<select name="sel_<?php echo $_item->getId()?>" id="sel_<?php echo $_item->getId()?>" class=" product-custom-option" title="" onchange="opConfig.reloadPrice()">
<option value="">-- Please Select --</option>
<?php foreach ($values as $v):?>
<option value="<?php echo $v->getData('title');echo ('+').$v->getData('price');?>">
<?php echo $v->getData('title');echo ('+').$v->getData('price');?>
</option>
<?php endforeach;?>
</select>
Here I had called an onchange function opConfig.reloadPrice() in select box. But still price is not changing. What will be the possible issues?

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