Buenas tardes a todos. Investigué un poco más, ya que ahora me mandaron a usar javascript para facilitarme el trabajo
el cual no manejo muy bien pero me aplique el fin de semana. Bueno en esta ocasión voy a mandar mi código completo. Ya elaboré mi función para la multiplicación pero aún no doy con la forma de insertar la operación en la vista y que se guarde en la base de datos. Bueno aquí voy:
form:
<?php
/* @var $this InmuebleController */
/* @var $model Inmueble */
/* @var $form CActiveForm */
?>
<div class="form">
<?php
///Activar Javascript////////////////////////////
Yii::app()->clientScript->registerCoreScript('jquery');
Yii::app()->clientScript->registerCoreScript( 'jquery.ui' );
//We need to pre-render the jquery.yiiactiveform.js on the view where we are going to place the AJAX functionality
Yii::app()->clientScript->registerCoreScript('yiiactiveform');
$cs = Yii::app()->getClientScript(); //////////////REGISTRO MI FUNCION JAVASCRIPT////////
$cs->registerScript(
function mult(){
c1 = document.getElementByName("areat").value;
c2 = document.getElementByName("valorc").value;
r = m1*m2;
document.getElementByName("valorter").value = r;
}
CClientScript::POS_END
);
//////Hasta acá///////////////////////////////////////
$form=$this->beginWidget('CActiveForm', array(
'id'=>'inmueble-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>false,
'clientOptions'=>array(
'validateOnSubmit'=>true,
)
)); ?>
<p class="note">Los campos con <span class="required">*</span> son obligatorios.</p>
<hr size=2 width=80% noshade=“noshade” align= right />
<?php echo $form->errorSummary($model); ?>
<div class="grupo">
<div class="row">
<?php echo $form->labelEx($model,'cc'); ?>
<?php $form->widget('CMaskedTextField', array(
'model' => $model,
'attribute' => 'cc',
'mask' => '99|99|99|U99|999|999|99|999|999',
'htmlOptions' => array('size' => 31)
)); //echo $form->textField($model,'cc',array('size'=>30,'maxlength'=>30)); ?>
<?php echo $form->error($model,'cc'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'operacion'); ?>
<?php echo $form->dropDownList($model,'operacion',array (''=>'Seleccione','Venta'=>'Venta','Donación'=>'Donación','Herencia'=>'Herencia','Permuta'=>'Permuta','R. Judicial'=>'R. Judicial','D. Enpago'=>'D. Enpago','Partición'=>'Partición','Ejec. Hipot.'=>'Ejec. Hipot.','Otro'=>'Otro')); ?>
<?php echo $form->error($model,'operacion'); ?>
</div>
</div>
<div class="grupo">
<div class="row">
<?php echo $form->labelEx($model,'propietario'); ?>
<?php echo $form->textField($model,'propietario',array('size'=>30,'maxlength'=>30)); ?>
<?php echo $form->error($model,'propietario'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'persona'); ?>
<?php echo $form->dropDownList($model,'persona',array(''=>'--','PN'=>'PN','PJ'=>'PJ')); ?>
<?php echo $form->error($model,'persona'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'razon'); ?>
<?php echo $form->dropDownList($model,'razon',array(''=>'-','V'=>'V','E'=>'E','J'=>'J')); ?><?php echo $form->textField($model,'ci',array('size'=>11,'maxlength'=>11)); ?><?php echo $form->error($model,'ci'); ?>
<?php echo $form->error($model,'razon'); ?>
</div>
<div class="row">
<?php //echo $form->labelEx($model,'ci'); ?>
<?php //echo $form->textField($model,'ci'); ?>
<?php //echo $form->error($model,'ci'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'tlf'); ?>
<?php echo $form->textField($model,'tlf',array('size'=>11,'maxlength'=>11)); ?>
<?php echo $form->error($model,'tlf'); ?>
</div>
</div>
<div class="grupo">
<div class="row">
<?php echo $form->labelEx($model,'ocupante'); ?>
<?php echo $form->textField($model,'ocupante',array('size'=>30,'maxlength'=>30)); ?>
<?php echo $form->error($model,'ocupante'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'personao'); ?>
<?php echo $form->dropDownList($model,'personao',array(''=>'--','PN'=>'PN','PJ'=>'PJ')); ?>
<?php echo $form->error($model,'personao'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'razono'); ?>
<?php echo $form->dropDownList($model,'razono',array(''=>'-','V'=>'V','E'=>'E','J'=>'J')); ?><?php echo $form->textField($model,'cio',array('size'=>11,'maxlength'=>11)); ?>
<?php echo $form->error($model,'cio'); ?>
<?php echo $form->error($model,'razono'); ?>
</div>
<div class="row">
<?php //echo $form->labelEx($model,'cio'); ?>
<?php //echo $form->textField($model,'cio'); ?>
<?php //echo $form->error($model,'cio'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'tlfo'); ?>
<?php echo $form->textField($model,'tlfo',array('size'=>11,'maxlength'=>11)); ?>
<?php echo $form->error($model,'tlfo'); ?>
</div>
</div>
<hr size=5 width=80% noshade=“noshade” align= center />
<div class="grupo">
<div class="row">
<?php echo $form->labelEx($model,'direccion'); ?>
<?php echo $form->textArea($model,'direccion',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,'direccion'); ?>
</div>
</div>
<div class="grupo">
<div class="row">
<?php echo $form->labelEx($model,'tipoinm_id'); ?>
<?php echo $form->dropDownList($model,'tipoinm_id',CHtml::listData(Tipoinm::model()->findAll(),'id','tipo'),array('empty'=>'-------Elija una opción-------')); ?>
<?php echo $form->error($model,'tipoinm_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'parroquia_id'); ?>
<?php echo $form->dropDownList($model,'parroquia_id',CHtml::listData(Parroquia::model()->findAll(),'id','parr'),array('empty'=>'-------Elija una opción-------')); ?>
<?php echo $form->error($model,'parroquia_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'zona_id'); ?>
<?php echo $form->dropDownList($model,'zona_id',CHtml::listData(Zona::model()->findAll(),'id','sectores'),array('empty'=>'---------Elija una opción---------')); ?>
<?php echo $form->error($model,'zona_id'); ?>
</div>
</div>
<hr size=5 width=80% noshade=“noshade” align= center />
<div class="grupo">
<div class="row" id="areat">
<?php echo $form->labelEx($model,'areat'); ?>
<?php echo $form->textField($model,'areat',array('size'=>10,'maxlength'=>10)); ?>
<?php echo $form->error($model,'areat'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'areac'); ?>
<?php echo $form->textField($model,'areac',array('size'=>10,'maxlength'=>10)); ?>
<?php echo $form->error($model,'areac'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'lindn'); ?>
<?php echo $form->textField($model,'lindn',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'lindn'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'linds'); ?>
<?php echo $form->textField($model,'linds',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'linds'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'linde'); ?>
<?php echo $form->textField($model,'linde',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'linde'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'lindo'); ?>
<?php echo $form->textField($model,'lindo',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'lindo'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'piso'); ?>
<?php echo $form->textField($model,'piso',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($model,'piso'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'techo'); ?>
<?php echo $form->textField($model,'techo',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($model,'techo'); ?>
</div>
</div>
<div class="grupo">
<div class="row" id="valorc">
<?php echo $form->labelEx($model,'valorc'); ?>
<?php echo $form->textField($model,'valorc',array('size'=>10,'maxlength'=>10)); ?>
<?php echo $form->error($model,'valorc'); ?>
</div>
<div class="row" id="valorter">
<?php echo $form->labelEx($model,'valorter'); ?>
<?php echo $form->textField($model,'valorter',array('name'=>'valorter','onChange'=>'mult();', 'size'=>10,'maxlength'=>10));?> ///Llamo a la función que multiplica
<?php echo $form->error($model,'valorter'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'valort'); ?>
<?php echo $form->textField($model,'valort',array('size'=>10,'maxlength'=>10)); ?>
<?php echo $form->error($model,'valort'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'control_id'); ?>
<?php echo $form->dropDownList($model,'control_id',CHtml::listData(Control::model()->findAll(),'id','director'),array('empty'=>'---------Elija una opción---------')); ?>
<?php echo $form->error($model,'control_id'); ?>
</div>
</div>
<div class="grupo">
<div class="row">
<?php echo $form->labelEx($model,'tomo'); ?>
<?php echo $form->textField($model,'tomo',array('size'=>11,'maxlength'=>11)); ?>
<?php echo $form->error($model,'tomo'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'numero'); ?>
<?php echo $form->textField($model,'numero',array('size'=>11,'maxlength'=>11)); ?>
<?php echo $form->error($model,'numero'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'folio'); ?>
<?php echo $form->textField($model,'folio',array('size'=>11,'maxlength'=>11)); ?>
<?php echo $form->error($model,'folio'); ?>
</div>
</div>
<hr size=5 width=80% noshade=“noshade” align= center />
<div class="contenedor">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
_view:
<?php
/* @var $this InmuebleController */
/* @var $data Inmueble */
?>
<div class="view">
<b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b>
<?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id)); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('cc')); ?>:</b>
<?php echo CHtml::encode($data->cc); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('propietario')); ?>:</b>
<?php echo CHtml::encode($data->propietario); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('tipoinm_id')); ?>:</b>
<?php echo CHtml::encode($data->tipoinm->tipo); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('parroquia_id')); ?>:</b>
<?php echo CHtml::encode($data->parroquia->parr); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('zona_id')); ?>:</b>
<?php echo CHtml::encode($data->zona->sectores); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('control_id')); ?>:</b>
<?php echo CHtml::encode($data->control->director); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('valorc')); ?>:</b>
<?php echo CHtml::encode($data->zona->ut); ?>
<br />
<?php /*
<b><?php echo CHtml::encode($data->getAttributeLabel('operacion')); ?>:</b>
<?php echo CHtml::encode($data->operacion); ?>
<br />
*/ ?>
</div>
model:
<?php
class Inmueble extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return Inmueble the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'inmueble';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('control_id, tipoinm_id, parroquia_id, zona_id, cc, propietario, persona, razon, ci, tlf, direccion, tomo, numero, folio, areat, lindn, linds, linde, lindo, valorter', 'required'),
array('ci, tlf, cio, tlfo', 'numerical', 'integerOnly'=>true),
array('tlf, tlfo', 'length', 'max'=>11, 'min'=>11),
array('control_id, tipoinm_id, parroquia_id, zona_id, areat, areac, valorter, valorc, valort', 'length', 'max'=>10),
array('cc, operacion, propietario', 'length', 'max'=>40),
array('cc', 'unique', 'attributeName'=>'cc','className'=>'Inmueble','allowEmpty'=>false),
array('areat', 'length', 'max'=>9, 'min'=>1),
array('areat', 'compare','operator'=>'<=','compareValue'=>999999999, 'message'=>'maximum is 999999999'),
array('areat', 'compare','operator'=>'>=','compareValue'=>1 ),
array('areac', 'length', 'max'=>9, 'min'=>1),
array('areac', 'match', 'pattern'=>'/^[0-9]{0,9}(\.[0-9]{0,9})?$/'),
array('areac','compare','compareAttribute'=>'areat','operator'=>'<=','message'=>'El área construida debe ser menor al área total'),
array('persona, razon, personao', 'length', 'max'=>2),
array('ocupante', 'length', 'max'=>30),
array('razono', 'length', 'max'=>1),
array('tomo, numero, folio', 'length', 'max'=>11),
array('tomo', 'match', 'pattern'=>'/^[0-9]{0,9}?$/'),
array('numero', 'match', 'pattern'=>'/^[0-9]{0,9}?$/'),
array('folio', 'match', 'pattern'=>'/^[0-9 -]{0,9}?$/'),
array('lindn, linds, linde, lindo', 'length', 'max'=>255),
array('piso, techo', 'length', 'max'=>50),
array('piso', 'match', 'pattern'=>'/^[a-zA-Z\s]+$/'),
array('techo', 'match', 'pattern'=>'/^[a-zA-Z\s]+$/'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, control_id, tipoinm_id, parroquia_id, zona_id, cc, operacion, propietario, persona, razon, ci, tlf, ocupante, personao, razono, cio, tlfo, direccion, tomo, numero, folio, areat, areac, lindn, linds, linde, lindo, piso, techo, valorter, valorc, valort', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'parroquia'=>array(self::BELONGS_TO,'Parroquia','parroquia_id'),
'zona'=>array(self::BELONGS_TO,'Zona','zona_id'),
'tipoinm'=>array(self::BELONGS_TO,'Tipoinm','tipoinm_id'),
'control'=>array(self::BELONGS_TO,'Control','control_id'),
'precioter'=>array(self::BELONGS_TO,'Zona','valorter'),
);
}
}
}
controller:
<?php
class InmuebleController extends Controller
{
/**
* @var string the default layout for the views. Defaults to '//layouts/column2', meaning
* using two-column layout. See 'protected/views/layouts/column2.php'.
*/
public $layout='//layouts/column2';
/**
* @return array action filters
*/
public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
'postOnly + delete', // we only allow deletion via POST request
);
}
/**
* Specifies the access control rules.
* This method is used by the 'accessControl' filter.
* @return array access control rules
*/
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update'),
'users'=>array('@'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete'),
'users'=>array('admin'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
/**
* Displays a particular model.
* @param integer $id the ID of the model to be displayed
*/
public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id),
));
}
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate()
{
$model=new Inmueble;
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
if(isset($_POST['Inmueble']))
{
$model->attributes=$_POST['Inmueble'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
/**
* Updates a particular model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id the ID of the model to be updated
*/
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
if(isset($_POST['Inmueble']))
{
$model->attributes=$_POST['Inmueble'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
));
}
/**
* Deletes a particular model.
* If deletion is successful, the browser will be redirected to the 'admin' page.
* @param integer $id the ID of the model to be deleted
*/
public function actionDelete($id)
{
$this->loadModel($id)->delete();
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
/**
* Lists all models.
*/
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('Inmueble');
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
/**
* Manages all models.
*/
public function actionAdmin()
{
$model=new Inmueble('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Inmueble']))
$model->attributes=$_GET['Inmueble'];
$this->render('admin',array(
'model'=>$model,
));
}
/**
* Returns the data model based on the primary key given in the GET variable.
* If the data model is not found, an HTTP exception will be raised.
* @param integer $id the ID of the model to be loaded
* @return Inmueble the loaded model
* @throws CHttpException
*/
public function loadModel($id)
{
$model=Inmueble::model()->findByPk($id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}
/**
* Performs the AJAX validation.
* @param Inmueble $model the model to be validated
*/
protected function performAjaxValidation($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='inmueble-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
}
En el form incluí mi función javascript y la aplico en la línea del formulario para la multiplicación. Seguí esta explicación pero no se si la hice bien Referencia Igualmente no se ve nada. Además use el document.getelementByName pero ya ustedes me corregirán si es factible usarlo acá. Cualquier duda, sugerencia o cualquier cosa será bien recibida. Saludos.
PD: Disculpen lo extenso del post 