Ajax validation in Yii form - ajax

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'); ?>

Related

Magento: How to show custom attribute on product page below description

I have a custom attribute for products in which I add a video URL.
I made this Embed video responsive using css.
Now I want to call the custom attribute on the product page, so it shows the video.
The file responsible for this is: description.phtml
I've tried the following:
?>
<?php $_description = $this->getProduct()->getDescription(); ?>
<?php if ($_description): ?>
<h2><?php echo $this->__('Details') ?></h2>
<div class="std">
<?php echo $this->helper('catalog/output')->productAttribute($this->getProduct(), $_description, 'description') ?>
</div>
<div class="std">
<?php echo $_product->getAttributeText('video') ?>
</div>
<?php endif; ?>
But I get the error:
Fatal error: Call to a member function getAttributeText() on a non-object in /data/web/public/app/design/frontend/base/default/template/catalog/product/view/description.phtml on line 40
The video does show however. I'm probably doing this all wrong. Can I fix this with a simple edit of the code, or do I have to use an entirely different approach?
Thanks.
<?php echo $this->getProduct()->getAttributeText('video'); ?>
Try this. Or on top of document add this
<?php $_product = $this->getProduct(); ?>

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.

Magento: textbox instead of multi select in layered navigation

Is there a possibility in Magento to have a multi-select attribute, for which I would use a textbox in layered navigation instead of showing all items from multi-select?
I have an attribute where I will have hundreds of options and need to use it in layered navigation.
When customer uses invalid value, an error should show up.
Edit: After the help from FlorinelChis I have folowing code in filter.phtml:
<ol>
<?php foreach ($this->getItems() as $_item): ?>
<?php
$attributeModel = $this->getAttributeModel();
$attribute_code = $attributeModel->getAttributeCode();
?>
<li>
<?php if ($attribute_code != 'available_zip'): ?>
<?php if ($_item->getCount() > 0): ?>
<?php echo $_item->getLabel() ?>
<?php else: echo $_item->getLabel() ?>
<?php endif; ?>
<?php if ($this->shouldDisplayProductCount()): ?>
(<?php echo $_item->getCount() ?>)
<?php endif; ?>
<?php endif; ?>
</li>
<?php endforeach ?>
</ol>
<?php
if ($attribute_code == 'available_zip'):
$cat = Mage::registry('current_category')->getUrlPath() ;
$url = Mage::getBaseUrl();
/*$sendUrl = $this->urlEscape($_item->getUrl()).'+'.$url.$cat.'?'.$attribute_code.'='.$_item->getValue();*/
echo '<form action="" method="get">';
echo '<input type="text" size="5" maxlength="5" name="'.$attribute_code.'" />';
echo '<button type="submit">OK</button>';
echo '</form>';
endif; ?>
I have one more thing now:
how to send the form with attribute id instead of value?
First, let's find out how Magento displays the filters in left hand navigation
1) Enable Template Path Hints:
and here is the result:
2) Let's take a look at app/design/frontend/base/default/template/catalog/layer/filter.phtml (you should copy this file in your theme folder structure)
3) The block ($this in the template file is an instance of Mage_Catalog_Block_Layer_Filter_Attribute which extends Mage_Catalog_Block_Layer_Filter_Abstract)
4) You can see that in Mage_Catalog_Block_Layer_Filter_Abstract a getName() method exists, so you could rely on this function to identify when you attribute is displayed. Don't! The label can change and your code won't be useful any more.
Back to our template file, you can get the attribute_code (which is reliable)
//$this is instance of Mage_Catalog_Block_Layer_Filter_Attribute
$attributeModel = $this->getAttributeModel();
$attribute_code = $attributeModel->getAttributeCode();
5) On your template you can have a check based on attribute code, so you display either the standard list or your custom html code with a textarea instead of a huge list.

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.

what's those line meaning in magento?

<?php foreach ($this->getChildGroup('detailed_info', 'getChildHtml') as $alias => $html):?>
<div class="box-collateral <?php echo "box-{$alias}"?>">
<?php if ($title = $this->getChildData($alias, 'title')):?>
<h2><span><?php echo $this->escapeHtml($title); ?></span></h2>
<?php endif;?>
<?php echo $html; ?>
</div>
<?php endforeach;?>
<?php echo $this->getChildHtml('product_additional_data') ?>
what's those line meaning in magento?
foreach over each child (that is grouped by name detailed_info with method getchildhtml) and output the data from those blocks
get the html of product_additional_data block
This is the snippet of code from the catalog/product/view.phtml that displays Description, Custom Options, and other detailed product information in the front end view.

Resources