Gowthamsettu
Gowthamsettu

Reputation: 1

Yii framework to create action for new admin page

How can I add a button and write the own action code for it in yii framework while I'm creating an admin page ? The code I have in my view file is:

<?php echo CHtml::beginForm('Protected/GreetingsController/actionSubmit','get');?>
<?php echo CHtml::submitButton('submit',array('SiteController'=>'actionIndex'));?>
<?php echo CHtml::endForm(); ?>

Upvotes: 0

Views: 2918

Answers (2)

When you work on yii framework, the controller is the responsible to show view page. Now you have a form in view and you want to add action to that form. You go to the that specific controller and call the that form view here.

For Example. You have a Registration form here.

<h2>Registration Form</h2>
<div class="form">
<?php echo CHtml::beginForm(); ?>

    <?php echo CHtml::errorSummary($model); ?>
<div class="row">
        <?php echo CHtml::activeLabel($model,'User Name'); ?>
        <?php echo CHtml::activeTextField($model,'username') ?>
    </div>

    <div class="row">
        <?php echo CHtml::activeLabel($model,'email'); ?>
        <?php echo CHtml::activeTextField($model,'email') ?>
    </div>

    <div class="row">
        <?php echo CHtml::activeLabel($model,'password'); ?>
        <?php echo CHtml::activePasswordField($model,'password') ?>
    </div>

    <div class="row">
        <?php echo CHtml::activeLabel($model,'Retype Password'); ?>
        <?php echo CHtml::activePasswordField($model,'retypepassword') ?>
    </div>

    <div class="row submit">
        <?php echo CHtml::submitButton('Register'); ?>
    </div>

    <?php echo CHtml::endForm(); ?>
</div><!-- form -->

Now i did not insert any action in view registration form page.

Now i can access this form to any controller where i can want.

<?php
class yourController extends Controller
{
    public  function actionIndex() {

        $model = new RegisterForm ;
        $this->render('register',array('model'=>$model));
    }
}

Now you may do all the things as you want ...

You should need to add model because if you did not add model class then i will give error.

You can see the complete documentation given by yii so you can understand easily.

Link. http://www.yiiframework.com/doc/guide/1.1/en/form.model

Thanks

Upvotes: 0

Nimir
Nimir

Reputation: 5839

CHtml::beginForm() will only put <form> tag to your HTML. You need to understand that CHtml functions like: button(), link(), ajaxButton() ...etc are to generate some HTML using the attributes (e.g $htmlOptions array) as options to suit your need.

For this case, where you want to point to an exact action in some controller you can use a method called beginWidget like this:

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'login-form',
    'action' => "/controller-name/action-name",
  )
);?>

<?php echo CHtml::submitButton('Save'); ?>

<?php $this->endWidget(); ?>

The button() when fired will send to the specified action.

Upvotes: 2

Related Questions