I am kinda new to Yii and i am finding some things not funny. I have a form that i want to update and it keeps throwing errors each time, when i'm trying to update it.
View: _form.php
<?php
/* #var $this BaseStationController */
/* #var $model BaseStation */
/* #var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'base-station-form',
'enableAjaxValidation'=>true,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<?php echo $form->labelEx($model,'c_id'); ?>
<select name="BaseStation[c_id]">
<?php foreach($models as $m):?> //Here coded the loop below to display customer's id from the database
<option value='<?php echo $m->id;?>'><?php echo $m->firstname.' '.$m->lastname;?></option>
<?php endforeach;?>
</select>
</div>
<div class="row">
<?php echo $form->labelEx($model,'base_station_num'); ?>
<?php echo $form->textField($model,'base_station_num',array('size'=>15,'maxlength'=>15)); ?>
<?php echo $form->error($model,'base_station_num'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
Controller
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['BaseStation']))
{
$model->attributes=$_POST['BaseStation'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
));
}
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['BaseStation']))
{
$model->attributes=$_POST['BaseStation'];
$model->c_id = $model->c_id;
$model->base_station_num = $model->base_station_num;
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
));
}
try this
Related
this is my form in view
<div class="form">
<?php echo CHtml::errorSummary($model); ?>
<div class="row">
<?php echo CHtml::activeLabel($model,'Your Name'); ?>
<?php echo CHtml::activeTextField($model,'regname') ?>
</div>
<div class="row">
<?php echo CHtml::activeLabel($model,'Your Password'); ?>
<?php echo CHtml::activePasswordField($model,'regpass') ?>
</div>
<div class="row">
<?php echo CHtml::activeLabel($model,'Email Address'); ?>
<?php echo CHtml::activePasswordField($model,'regemail') ?>
</div>
<div class="row">
<?php echo CHtml::activeLabel($model,'Contact'); ?>
<?php echo CHtml::activePasswordField($model,'regcontact') ?>
</div>
<div class="row submit">
<?php echo CHtml::submitButton('Login'); ?>
</div>
please help me to save this data from my controller to model
this is my model
//-- set Table
public function tableName(){
return 'user';
}
public function attributeLabels()
{
return array(
'username'=>'Your username for the game',
'password'=>'Your password for the game',
'email'=>'Needed in the event of password resets',
);
}
and this is my controller
public function actionRegister2()
{
$model=new RegisterForm;
if(isset($_POST['RegisterForm']))
{
echo 'done';
}
$this->render('register2',array(
'model'=>$model,
));
i an new in yii framework so didn't get a good tutorial for it, please suggest me how i insert the data into database by submitting a form
you can add this code in your controller:
public function actionRegister2()
{
$model=new RegisterForm;
if(isset($_POST['RegisterForm']))
{
$model->attributes = $_POST['RegisterForm'];
if ($model->save()) {
Yii::app()->user->setFlash('success', 'You have successfully added.');
$this->redirect(array('index'));
}
// or if(!$model->save()){ print_r($model->getErrors())}
}
$this->render('register2',array(
'model'=>$model,
));
}
I'm newbie.I want to create update form in yii.I created a controller Adminprofile and call the model ProfileForm.In my view 'adminprofile.php' I want to load the value for the admin from the table 'superadmin' in name and password fields.Now It displays only field without values.
I want to use update as $update=update superadmin set username='$username',password='$password' where id='1'
SiteController.php
public function actionAdminprofile()
{
$model=new ProfileForm;
if(isset($_POST['ContactForm']))
{
$model->attributes=$_POST['ContactForm'];
if($model->validate())
{
$name='=?UTF-8?B?'.base64_encode($model->name).'?=';
$password='=?UTF-8?B?'.base64_encode($model->password).'?=';
$this->refresh();
}
}
$this->render('adminprofile',array('model'=>$model));
}
ProfileForm.php (MODEL)
class ProfileForm extends CFormModel
{
public $name;
public $password;
public function rules()
{
return array(
// name, email, subject and body are required
array('name, password', 'required'),
);
}
}
Adminprofile.php
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'contact-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'name'); ?>
<?php echo $form->textField($model,'name'); ?>
<?php echo $form->error($model,'name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->textField($model,'password'); ?>
<?php echo $form->error($model,'password'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php $this->endWidget(); ?>
</div>
<?php endif; ?>
I have the form
TblRegistration/_form.php
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'tbl-registration-form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.
'enableAjaxValidation'=>true,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary(array($model,$model1)); ?>
<div class="row">
<?php echo $form->labelEx($model,'director'); ?>
<?php echo $form->textField($model,'director',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($model,'director'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'experience'); ?>
<?php echo $form->textField($model,'experience'); ?>
<?php echo $form->error($model,'experience'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'language'); ?>
<?php echo $form->textField($model,'language',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($model,'language'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model1,'email'); ?>
<?php echo $form->textField($model1,'email'); ?>
<?php echo $form->error($model1,'email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model1,'password'); ?>
<?php echo $form->textField($model1,'password'); ?>
<?php echo $form->error($model1,'password'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
My actioncreate create contain
public function actionCreate()
{
$model=new TblRegistration;
$model1=new TblLogin();
// Uncomment the following line if AJAX validation is needed
//$this->performAjaxValidation($model1);
if(isset($_POST['TblRegistration']) && isset($_POST['TblLogin']))
{
$model->attributes=$_POST['TblRegistration'];
$model1->attributes=$_POST['TblLogin'];
$model->save();
if($model->save())
{
$model1->reg_id=$model->reg_id;
$model1->save();
}
if(($model->save() )&& ($model1->save()))
$this->redirect(array('view','id'=>$model->reg_id));
}
$this->render('create',array(
'model'=>$model,
));
}
my fields email and password is from the model TblLogin.I want to validate those fields before iam saving the data.how can i achive this?
After:
$model->attributes=$_POST['TblRegistration'];
$model1->attributes=$_POST['TblLogin'];
Add:
if($model->validate() && $model1->validate()){
$model->save();
...
I created app with yii and I need to load a view form in Fancy-box.
I did that but my problem is when I click on the submit button the form redirects me to the controller action without validating the form.
How to validate the form without the redirect inside Fancy-box?
My view:
<?php
$config = array(
);
$this->widget('application.extensions.fancybox.EFancyBox', array(
'target'=>'#getaction',
'config'=>$config,));
echo CHtml::link('Add Section',array('section/create'),array('id'=>'getaction'));
?>
FormView _from from call from another view
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'section-form',
'enableAjaxValidation'=>true,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'name'); ?>
<?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'name'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
Controller :
public function actionCreate()
{
$model=new Section;
if(isset($_POST['Section']))
{
$model->attributes=$_POST['Section'];
if($model->validate())
//// Do Som code here
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
the problem was fixed by my bro seenivasan on Yii Forum , so I will share the code here
_form.php
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'section-form',
'enableAjaxValidation'=>true,
//'enableClientValidation'=>true,
'clientOptions'=>array('validateOnSubmit'=>true), //This is very important
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'name'); ?>
<?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>64)); ?>
<?php echo $form->error($model,'name'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
In controller
public function actionCreate()
{
$model=new Section;
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);//You have enabled ajax validation. You have to uncomment this line.
if(isset($_POST['Section']))
{
$model->attributes=$_POST['Section'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
if(Yii::app()->request->getIsAjaxRequest())
echo $this->renderPartial('_form',array('model'=>$model),true,true);//This will bring out the view along with its script.
else $this->render('create',array(
'model'=>$model,
));
}
Reference link :
Yii Forum
Just add 'enableClientValidation'=>true, in the array inside the beginwidget.
I am a newbie to the Yii framework.I want a multimodel form so I just went through this link and made all things like this.I have two table, first is group and another is member.
Group
ID
name
Member
id
group_id
firstname
lastname
Now I have made models for both tables and CRUD as well.I made change to GroupController file like this
public function actionCreate()
{
$group = new Group;
$member = new Member;
if(isset($_POST['Group'],$_POST['Member'])) {
//Populate input data to $group and $member
$group->attributes = $_POST['Group'];
$member->attributes = $_POST['Member'];
//Validate both $group and $member
$validate = $group->validate();
$validate = $member->validate() && $valid;
if($valid){
$group->save(false);
$member->save(false);
}
}
$this->render('create',array(
'group'=> '$group',
'member'=> '$member',
));
$model=new Group;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Group']))
{
$model->attributes=$_POST['Group'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
and after changing the group >> View >> create.php file like this
<?php echo $this->renderPartial('_form', array('group'=>$group, 'member'=>$member)); ?>
The _form file is like this
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'group-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($group,$member); ?>
<div class="row">
<?php echo $form->labelEx($model,'name'); ?>
<?php echo $form->textField($model,'name'); ?>
<?php echo $form->error($model,'name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($member,'firstname'); ?>
<?php echo $form->textField($member,'firstname',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($member,'firstname'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
but after all I am getting error like this Undefined variable: group .
So can some one please tell me how to solve this issue. I have lost one day behind this.So any help and suggestions will be highly appreciable.
You are doing multiple mistakes here ->
when you call
$this->render('create',array(
'model'=>$model,
));
you are not passing $group or $member models which you created in the group create controller. Change it to -
$this->render('create',array(
'group'=>$group,
'member'=>$member,
));
and secondly, there is no variable named $valid... change this part
$validate = $member->validate() && $valid;
if($valid){
to
$validate = $member->validate() && $validate;
if($validate){
now things should work fine