Save Form in Yii Framework:
Create a create.php in view part & add this :
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'space-create-form',
'enableAjaxValidation' => false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
));
?>
<div class="form-group">
<label><?php echo Yii::t('SpaceModule.views_create_create', 'Project Name'); ?></label>
<?php print $form->textField($model, 'name', array('class' => 'form-control', 'placeholder' => Yii::t('SpaceModule.views_create_create', 'project titan'))); ?>
<?php echo $form->error($model, 'name'); ?>
</div>
<div class="form-group">
<label><?php echo Yii::t('SpaceModule.views_create_create', 'Business Founded'); ?></label>
<?php print $form->textField($model, 'business', array('class' => 'form-control', 'placeholder' => Yii::t('SpaceModule.views_create_create', 'business founded'))); ?>
</div>
<?php
echo HHtml::ajaxButton(Yii::t('SpaceModule.views_create_create', 'Create'), array('//space/create/create'), array(
'type' => 'POST',
'beforeSend' => 'function(){ setModalLoader(); }',
'success' => 'function(html){ $("#globalModal").html(html); }',
), array('class' => 'btn btn-primary', 'id' => 'space-create-submit-button'));
?>
In your Create Controller:
public function actionCreate()
{
if (!Yii::app()->user->canCreateSpace()) {
throw new CHttpException(400, 'You are not allowed to create spaces!');
}
$model = new Space('edit');
// Ajax Validation
if (isset($_POST['ajax']) && $_POST['ajax'] === 'space-create-form') {
echo CActiveForm::validate($model);
Yii::app()->end();
}
if (isset($_POST['Space'])) {
$_POST['Space'] = Yii::app()->input->stripClean($_POST['Space']);
$model->attributes = $_POST['Space'];
if ($model->validate() && $model->save()) {
// Save in this user variable, that the workspace was new created
Yii::app()->user->setState('ws', 'created');
// Redirect to the new created Space
$this->htmlRedirect($model->getUrl());
}
}
$this->renderPartial('create', array('model' => $model), false, true);
}
In your space modal:
Firstly add your attributes in attributeLabels() function
public function attributeLabels()
{
return array(
'name' => Yii::t('SpaceModule.models_Space', 'Name'),
'business' => Yii::t('SpaceModule.models_Space', 'Business Founded'),
)};
Also, add in your rules();
public function rules()
{
$rules = array();
if ($this->scenario == 'edit') {
$rules = array(
array('name,business', 'safe'),
);
}
return $rules;
}
*You must have to add in your rules(), Otherwise it will show xss errors.
Thanks
Create a create.php in view part & add this :
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'space-create-form',
'enableAjaxValidation' => false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
));
?>
<div class="form-group">
<label><?php echo Yii::t('SpaceModule.views_create_create', 'Project Name'); ?></label>
<?php print $form->textField($model, 'name', array('class' => 'form-control', 'placeholder' => Yii::t('SpaceModule.views_create_create', 'project titan'))); ?>
<?php echo $form->error($model, 'name'); ?>
</div>
<div class="form-group">
<label><?php echo Yii::t('SpaceModule.views_create_create', 'Business Founded'); ?></label>
<?php print $form->textField($model, 'business', array('class' => 'form-control', 'placeholder' => Yii::t('SpaceModule.views_create_create', 'business founded'))); ?>
</div>
<?php
echo HHtml::ajaxButton(Yii::t('SpaceModule.views_create_create', 'Create'), array('//space/create/create'), array(
'type' => 'POST',
'beforeSend' => 'function(){ setModalLoader(); }',
'success' => 'function(html){ $("#globalModal").html(html); }',
), array('class' => 'btn btn-primary', 'id' => 'space-create-submit-button'));
?>
In your Create Controller:
public function actionCreate()
{
if (!Yii::app()->user->canCreateSpace()) {
throw new CHttpException(400, 'You are not allowed to create spaces!');
}
$model = new Space('edit');
// Ajax Validation
if (isset($_POST['ajax']) && $_POST['ajax'] === 'space-create-form') {
echo CActiveForm::validate($model);
Yii::app()->end();
}
if (isset($_POST['Space'])) {
$_POST['Space'] = Yii::app()->input->stripClean($_POST['Space']);
$model->attributes = $_POST['Space'];
if ($model->validate() && $model->save()) {
// Save in this user variable, that the workspace was new created
Yii::app()->user->setState('ws', 'created');
// Redirect to the new created Space
$this->htmlRedirect($model->getUrl());
}
}
$this->renderPartial('create', array('model' => $model), false, true);
}
In your space modal:
Firstly add your attributes in attributeLabels() function
public function attributeLabels()
{
return array(
'name' => Yii::t('SpaceModule.models_Space', 'Name'),
'business' => Yii::t('SpaceModule.models_Space', 'Business Founded'),
)};
Also, add in your rules();
public function rules()
{
$rules = array();
if ($this->scenario == 'edit') {
$rules = array(
array('name,business', 'safe'),
);
}
return $rules;
}
*You must have to add in your rules(), Otherwise it will show xss errors.
Thanks
No comments:
Post a Comment