gans
(Ganesh 29oct)
January 3, 2012, 12:00pm
1
Hello All,
I am working on Multiple Addresses functionality in which there should be multiple zips, multiple cities. I have created one address array and i am storing all the values into this array. But the problem is for validating zip codes, cities i am facing problem because zip field, city field is array so yii validation is not working . Is there way to solve this problem? how can i validate those array fields using yii?
My sample code for city :
<div class="row">
<?php echo $form->labelEx($model, 'city'); ?>
<?php echo CHtml::textField(get_class($model).'[city][]', $model->city); ?>
<?php echo $form->error($model, 'city'); ?>
</div>
Thanks in advance,
Ganesh
jayant
(Codesutras)
January 3, 2012, 12:06pm
2
gans:
Hello All,
I am working on Multiple Addresses functionality in which there should be multiple zips, multiple cities. I have created one address array and i am storing all the values into this array. But the problem is for validating zip codes, cities i am facing problem because zip field, city field is array so yii validation is not working . Is there way to solve this problem? how can i validate those array fields using yii?
My sample code for city :
<div class="row">
<?php echo $form->labelEx($model, 'city'); ?>
<?php echo CHtml::textField(get_class($model).'[city][]', $model->city); ?>
<?php echo $form->error($model, 'city'); ?>
</div>
Thanks in advance,
Ganesh
Can you show me your complete code of view and controller file so that i can understand exactly what you are trying to do.!!!
Since if you are talking about Dynamic form then it is a different case.So,share your code here.so, people can answer your very well.
gans
(Ganesh 29oct)
January 3, 2012, 12:45pm
3
This is my view file code.
<div class="MainAddress">
<?php
/**
* Loop for multiple addresses
*/
foreach($this->fetchAddresses() as $key => $address) {
$model->address = $address['address'];
$model->city = $address['city'];
$model->zip = $address['zip'];
?>
<div class="divAddress">
<div class="row">
<?php echo $form->labelEx($model, 'address'); ?>
<?php echo CHtml::textArea(get_class($model).'[address][]', $model->address, array('class'=>'addressField'));?>
<?php echo $form->error($model, 'address'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'city'); ?>
<?php echo CHtml::textField(get_class($model).'[city][]', $model->city); ?>
<?php echo $form->error($model, 'city'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'zip'); ?>
<?php echo CHtml::textField(get_class($model).'[zip][]', $model->zip); ?>
<?php echo $form->error($model, 'zip'); ?>
</div>
</div>
<?php
}
?>
</div>
<div class="column">
<div class="address-button right" >
<input type="button" class="addmore" id="addmore" value="Add Multiple Address"/>
</div>
</div>
I have write down the fetchAddresss() method from which i have fetched all the address.
// Code to fetch Addresses
$model = $this->getFormModel();
$arrAddresses = array(
array(
'address' => '',
'city' => '',
'zip' => ''
)
);
/**
* fetch addresses
*/
$arrAddress = CJSON::decode($model->address);
if (!empty($arrAddress) && is_array($arrAddress)) {
/**
* Get city and zipcode information
*/
$arrCity = (is_array($model->city))?$model->city:\CJSON::decode($model->city);
$arrZipcodes = (is_array($model->zip))?$model->zip:\CJSON::decode($model->zip);
/**
* Add city and zipcode
*/
array_walk($arrAddress, function($value, $key)use(&$arrAddress, &$arrCity, &$arrCountry, &$arrState, &$arrZipcodes) {
$arrAddress[$key] = array(
'address' => $arrAddress[$key],
'city' => $arrCity[$key],
'country' => $arrCountry[$key],
'state' => $arrState[$key],
'zip' => $arrZipcodes[$key]
);
});
$arrAddresses = $arrAddress;
}
/**
* Return addresses
*/
return $arrAddresses;
Then using magic method(get,set) i have saved the addresses. Now i want to use required rules for city and zip. but its not working.