Create a delete page form in Yii Framework? - php

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.

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.

Related

Yii form submition with CActiveForm

i just need to do a registration form, i'm battling with completing this task with CActiveForm. Basically its just inserting a new db record on form submit. This is what i have,
MyView
<!--begin a form-->
<?php $form = $this->beginWidget('CActiveForm', array(
'id'=>'user-registration-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'focus'=>array($model,'firstName'),
)); ?>
<!--error handling-->
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'firstName'); ?>
<?php echo $form->textField($model,'firstName'); ?>
<?php echo $form->error($model,'firstName'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'lastName'); ?>
<?php echo $form->textField($model,'lastName'); ?>
<?php echo $form->error($model,'lastName'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'age'); ?>
<?php echo $form->textField($model,'age'); ?>
<?php echo $form->error($model,'age'); ?>
</div>
<?php $this->endWidget(); ?>
<!--end a form-->
My Controller that renders the above view, this where i'm stuck, I also created a model called User(haven't done any code in it, default)
class RegisterController extends Controller
{
public function actionIndex()
{
$model = User::
$this->render('index', array('model'=>$model));
}
}
From my research i found there is something like, jst dnt know how to use it
link
$post=new Post;
$post->title='sample post';
$post->content='post body content';
$post->save();
Thanks in advance
You need to do in your actionIndex:
public function actionIndex()
{
$model = new User;
if(isset($_POST['User']))
{
$model->attributes = $_POST['User'];
if($model->save())
//Do any stuff here. for example redirect to created user view.
}
$this->render('index', array('model'=>$model));
}
I recommend you to read the Building a blog system with Yii tutorial. This is very good resource for learning yii better and also learn you the most important parts of any web application.

Geting multiple Records in update in Yii

I am developing this website that requires me to combine two models in one view where they have one to many relationship between them. The models name is Home and Image meaning Home has many Images but Image only has one Home.
I have manged to combine The view together but the problem that i encountering is to get all of the images. For example i have 6 images i want to display them or if i have 5 images i want to display them.
Home Controller UpdateMethod
public function actionUpdate($id)
{
$home=$this->loadModel($id);
$image=Image::model()->findByAttributes(array('homeId'=>$home->id));
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Home'],$_POST['Image'])){
$home->attributes=$_POST['Home'];
$image->attributes=$_POST['Image'];
$valid=$home->validate();
$valid=$image->validate() && $valid;
if($valid){
if($home->save()){
$image->save();
}
}
}
$this->render('update',array(
'home'=>$home,
'image'=>$image,
));
}
My _form.php to join them together
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'home-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'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($home); ?>
<div class="row">
<?php echo $form->labelEx($image,'imageUrl'); ?>
<?php echo $form->textField($image,'imageUrl',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($image,'imageUrl'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($home,'recentEvents'); ?>
<?php echo $form->textField($home,'recentEvents',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($home,'recentEvents'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($home,'introduction'); ?>
<?php echo $form->textArea($home,'introduction',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($home,'introduction'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($home->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
Update I had FindByattribues instead of FindAllByAttribues in the model so now it is returning an array. Now how to process that array in the view?
Okay i figured it out by myself posting this to maybe help someone who needs it. I the view i did the following.
<?php
foreach($image as $image){
?>
<div class="row">
<?php echo $form->labelEx($image,'imageUrl'); ?>
<?php echo $form->textField($image,'imageUrl',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($image,'imageUrl'); ?>
</div>
<?php
}
?>

Return Key Not Working For Textarea PHP/Yii

The following is done using Yii and PHP already tried to ask on the Yii Forum but no solutions where given.
I have the following textArea in one of my views.
<div class="row">
<?php echo $form->labelEx($model,'ref_description'); ?>
<?php echo $form->textArea($model,'ref_description',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,'ref_description'); ?>
</div>
Why isn't it able to return a newline when pressing the enter key but instead it's moving to the following textField?
Whole Code:
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'report-references-form',
'enableAjaxValidation'=>false,
)); ?>
<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,'ref_name'); ?>
<?php echo $form->textField($model,'ref_name',array('size'=>60,'maxlength'=>150)); ?>
<?php echo $form->error($model,'ref_name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'ref_description'); ?>
<?php echo $form->textArea($model,'ref_description',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,'ref_description'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'ref_quarter'); ?>
<?php echo $form->textField($model,'ref_quarter'); ?>
<?php echo $form->error($model,'ref_quarter'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'ref_year'); ?>
<?php echo $form->textField($model,'ref_year'); ?>
<?php echo $form->error($model,'ref_year'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'ref_date'); ?>
<?php echo $form->textField($model,'ref_date'); ?>
<?php echo $form->error($model,'ref_date'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
Please suggest about anything as this thing looks really stupid and I can't seem to find a way around it tried javscript with shift+enter command and other things.
Try to disable all js code (webdeveloper addon for firefox for example).
Save to pure html page and delete particular parts. This will help you diagnose reason.
If below fail try to build pure html form without anything more than pure html tags. No css, no js.
Check your browser addons - mabye there is something.
For further help to this question regarding the Yii. What needs to be changed is in the main layout.
There is a javascript code:
$('body').on('keydown', 'input, select' , 'textarea', function(e) {
var self = $(this)
, form = self.parents('form:eq(0)')
, focusable
, next
;
if (e.keyCode == 13) {
focusable = form.find('input,a,select,button,textarea').filter(':visible:not(:disabled)');
next = focusable.eq(focusable.index(this)+1);
if (next.length) {
next.focus();
} else {
form.submit();
}
return false;
}
});
Remove the textarea from the $('body').on function ONLY!

model validations not working when creating record in yii

I really don't know why is it happening my model validations are not working while creating record in yii.
doesn't display any errors .
The thing is if any of the required field is empty though it passes to the display page not displaying errors
but it doesn't insert the record as all required field a not filled.
My need is display errors in the same form i.e., validations should not pass if required fields are empty.
validation works with no issues in update, issues with create form
but it inserts the record if all required field are filled.
errors displayed in update are black not red as default by yii ...... is it due to the extension am using
model rules
array('name, category, model, brand, description, price', 'required'),
array('pimg', 'file','types'=>'jpg','on'=>'create'),
array('pimg', 'file','types'=>'jpg','on'=>'update', 'allowEmpty'=>true),
controller for create
$model=new controllername;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['controllername']))
{
$model->attributes=$_POST['controllername'];
$model->pimg=CUploadedFile::getInstance($model,'pimg');
$fileName = $model->pimg;
if($model->save())
$model->pimg->saveAs('images/'.$fileName);
$this->redirect(array('display','id'=>$model->productid));
}
$this->render('create',array(
'model'=>$model,
));
view
<?php $form=$this->beginWidget('CActiveForm',array(
'id'=>'form_name',
'enableAjaxValidation'=>false,
'htmlOptions'=>array('enctype'=>'multipart/form-data'),
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->labelEx($model,'name'); ?>
<?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>60)); ?>
<?php echo $form->error($model,'name'); ?>
<?php echo $form->labelEx($model,'model'); ?>
<?php echo $form->textField($model,'model',array('size'=>30,'maxlength'=>30)); ?>
<?php echo $form->error($model,'model'); ?>
<?php echo $form->labelEx($model,'description'); ?>
<?php echo $form->textField($model,'description',array('size'=>60,'maxlength'=>256)); ?>
<?php echo $form->error($model,'description'); ?>
<?php echo $form->labelEx($model,'pimg'); ?>
<?php echo $form->hiddenField($model,'pimg',array('length'=>222)); ?>
<?php echo $form->fileField($model, 'pimg',array('id'=>'imgInput',)); ?>
<?php echo $form->error($model,'pimg'); ?>
<?php echo $form->labelEx($model,'category'); ?>
<?php echo $form->dropDownList($model,'category',$model->getCat()); ?>
<?php echo $form->error($model,'category'); ?>
<?php echo $form->labelEx($model,'brand'); ?>
<?php echo $form->textField($model,'brand',array('size'=>30,'maxlength'=>30)); ?>
<?php echo $form->error($model,'brand'); ?>
<?php echo $form->labelEx($model,'price'); ?>
<?php echo $form->textField($model,'price'); ?>
<?php echo $form->error($model,'price'); ?>
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
<?php $this->endWidget(); ?>
can someone PLEASE tell me how can i achieve this . Thank you
try with
array('name, category, model, brand, description, price', 'required'),
array('pimg', 'file','types'=>'jpg','on'=>'insert', 'allowEmpty'=>false),
array('pimg', 'file','types'=>'jpg','on'=>'update', 'allowEmpty'=>true),
if you redirect a page, the error will not be shown,
your code redirects anyway, if (save()) or not .
add a {} after your if
if($model->save())
{
$model->pimg->saveAs('images/'.$fileName);
$this->redirect(array('display','id'=>$model->productid));
}

there is issue in yii " comment form view in post list"

I tried to bring the comment form in the post view list where user can put a comment .
my code which I am write for the above problem ...
<h5>Add your Comment</h5>
<?php if(Yii::app()->user->hasFlash('commentSubmitted')): ?>
<div class="flash-success">
<?php echo Yii::app()->user->getFlash('commentSubmitted'); ?>
</div>
<?php else: ?>
<?php $this->renderPartial('/comment/_form',array(
'model'=>$comment
)); ?>
<?php endif; ?>
"The _form contain....."
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'comment-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,'content'); ?>
<?php echo $form->textArea($model,'content',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,'content'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'author'); ?>
<?php echo $form->textField($model,'author',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'author'); ?>
</div>
It gives the error "Undefined variable: comment "
You need to define $comment. You are trying to pass a model to the form. This is usually a model of a database table. It looks like you are using active form. That means you are using the Active Record model in Yii. You should have a model that covers your comment table. If you need to know how to create a model you can find out how to use Gii here.
If you already have a comment model then you just need to define the model. Something like:
$comment = new Comment();
$this->renderPartial('/comment/_form',array('model'=>$comment));
It looks like this is a view that sometimes calls another view. You could define the $comment variable in the controller that calls the original view. You would just have to pass the comment variable into the original view as well as the second one.
Without knowing exactly where the error occurs, it seems to me that the most logical location is in this snippet:
<?php $this->renderPartial('/comment/_form',array(
'model'=>$comment
)); ?>
The solution would then probably be to replace $comment by 'Comment' (or something similar, I'm not really familiar with Yii).

Categories