Dan Mason
Dan Mason

Reputation: 2337

Create a delete page form in Yii Framework?

I know that usually you would just use the integrated CRUD delete button that is in admin however for my purposes I am requiring an actual page for delete that just has the id in a field and a submit button but so far it just produces the error view so any assistance is appreciated. I have tried to create it the same as my create and update pages are set up, please see the code below:

The link to the delete page:

<?php echo CHtml::link('Delete Article', array('delete', 'id'=>$pageid)); ?>

The link it produces:

http://local/..../Yii/news/index.php/delete?id=3

The controller:

public function actionDelete($id)
{
$model=$this->loadModel($id);


    if(isset($_POST['news_model']))
    {
        $model->attributes=$_POST['news_model'];
        if($model->save())
            $this->redirect('index');
    }

    $this->render(array('delete', array(
        'model'=>$model,
    ));
}

Delete.php:

<h2>Delete a news item</h2>

<?php echo $this->renderPartial('_form2', array('model'=>$model)); ?>

_form2.php

<?php echo $form->errorSummary($model); ?>

<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'news-model-form',
'enableAjaxValidation'=>false,
)); ?>

<div class="form">

<div class="row">
    <?php echo $form->labelEx($model,'id'); ?><br>
    <?php echo $form->textField($model,'id',array('size'=>50,'maxlength'=>128)); ?>
    <?php echo $form->error($model,'id'); ?>
</div><br>

<div class="row buttons">
    <?php echo CHtml::submitButton($model->isNewRecord ? 'Delete a news item'); ?>
</div>

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

    </div><!-- form -->

Thanks in advance for any help given.

Upvotes: 0

Views: 1096

Answers (1)

Snivs
Snivs

Reputation: 1135

You got an error in your php synax in _form2.php

<?php echo CHtml::submitButton($model->isNewRecord ? 'Delete a news item'); ?>

more like

<?php echo CHtml::submitButton($model->isNewRecord ? 'Delete a news item':'Delete an old item'); ?>

See the Ternary Operator in PHP: Comparison Operators

... yet I don't se the point in that sentence, to me it would seem a little bit more like:

<?php if (!$model->isNewRecord) echo CHtml::submitButton("Delete Record"); ?>

... but the record is guaranteed to not be new when it is loaded by $model=$this->loadModel($id);

Also, In Delete.php

<?php echo $this->renderPartial('_form2', array('model'=>$model)); ?>

would be more like

<?php echo $this->renderPartial('_form2', array('model'=>$model), true); ?>

or

<?php $this->renderPartial('_form2', array('model'=>$model)); ?>

Seel the documentation renderPartial(), specially pay attention to its return value its third argument. Turns out that you're actually echoing NULL. which explains why there is no display.

Upvotes: 3

Related Questions