i want to give the comments in the views form & i follow the function..
<?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'=>$data,
)); ?>
but now it show an error....
'htmlspecialchars() expects parameter 1 to be string, object given'
my '_form.php' under 'comment' is
<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>
The problem with yii's 'htmlspecialchars() expects parameter 1 to be string, object given' is that we don't know what is causing this error.
I have made this crude change yii/web/helpers/CHTML.php:
public static function encode($text)
{
if (is_object($text)){
echo __FILE__ ." ".__LINE__;
var_dump($text);
die(" This is an object, and it must be string!");
}
else
return htmlspecialchars($text,ENT_QUOTES,Yii::app()->charset);
}
So at least I know what object has been sent to that method.
ouch, dont change core classes.
Yii active record model is glued to your database table. one possible reason for this warning message is that your db table does not contain all columns you are trying to make available in your form.
just guessing :)
Related
I have a form like this as yii active record :
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'arsip-perihal-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($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'nama_arsip'); ?>
<?php echo $form->textField($model,'nama_arsip',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'nama_arsip'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'kode_primary'); ?>
<?php echo $form->textField($model,'kode_primary',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'kode_primary'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'Keterangan'); ?>
<?php echo $form->textField($model,'Keterangan'); ?>
<?php echo $form->error($model,'Keterangan'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
Can I use a HTML tag, An example textbox
First name: <input type="text" name="fname"><br>
if it can, how to use it? how to set default from $model like the form above?
Thank You,
When you use $form->textField, CActiveForm generates a textbox with ModelName[attributeName] name. So you can provide name for your textbox with this structure.
<input type="text" name="ModelName[fname]">
Note, "ModelName" is your model class, for example, "User".
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
}
?>
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!
I have two different table and i want to insert data in both of them at a time.
ONE table is verse
verse(id, topic_id, surah_id, verse_text) // id is primary key,
Second table is verse_translations
verse_translations(id, verse_id, language_id, translations_text) // id is primary key, language_id is foreign key references with language table, // verse_id is foreign key references with verse table.
Verse Create File (_form.php)
<div class="form">
<?php $form = $this->beginWidget('CActiveForm', array('id'=>'verse-form', 'enableAjaxValidation'=>true)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<?php echo $form->labelEx($model, 'verse_text'); ?>
<?php echo $form->textArea($model, 'verse_text', array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,'verse_text'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'verse_translations'); ?>
<p class="<?php echo "tran".$model->id ?>">
<?php
$errors = array_filter($model->verseTranslations);
if(!empty($errors)) {
foreach($model->verseTranslations as $vt) {
echo $form->textArea($model, 'translation_text', array('value'=>$vt['translation_text'], 'rows'=>6, 'cols'=>50));
}
}
?>
</p>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
As you may see from _form file, I have called the data from verse_translations table.
Now my questions are:
How I keep the value of textArea that it will go in an array in controller? And How can I insert data in verse_translation table from verse create controller.
The output of _form file is like that.
Verse Create Controller Code.
public function actionCreate()
{
$model=new Verse;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Verse']))
{
$model->attributes=$_POST['Verse'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
Hope you understand it clearly.
Thanks
I think you should try this
public function actionCreate()
{
$verse_model=new Verse;
$verse_translation_model=new Verse_translations;
if(isset($_POST['Verse']) && isset($_POST['Verse_translations']))
{
$verse_model->attributes=$_POST['Verse'];
$verse_translation_model->attributes=$_POST['Verse_translations'];
$verse_model->save();
$verse_translation_model->save();
echo 'data is saved in both tables';
}
$this->render('create',array('verse_model'=>$verse_model,'verse_translation_model'=>$verse_translation_model));
}
In views in create.php
$this->renderPartial('_form',array('verse_model'=>$verse_model,'verse_translation_model'=>$verse_translation_model));
in _form.php
<?php $form = $this->beginWidget('CActiveForm', array('id'=>'verse-form', 'enableAjaxValidation'=>true)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<?php echo $form->labelEx($verse_model, 'verse_text'); ?>
<?php echo $form->textArea($verse_model, 'verse_text', array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($verse_model,'verse_text'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($verse_model, 'topic_id'); ?>
<?php echo $form->textArea($verse_model, 'topic_id', array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($verse_model,'topic_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($verse_model, 'surah_id'); ?>
<?php echo $form->textArea($verse_model, 'surah_id', array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($verse_model,'surah_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($verse_transaction_model, 'verse_id'); ?>
<?php echo $form->textArea($verse_transaction_model, 'verse_id', array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($verse_transaction_model,'verse_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($verse_transaction_model, 'translations_text'); ?>
<?php echo $form->textArea($verse_transaction_model, 'translations_text', array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($verse_transaction_model,'translations_text'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($verse_transaction_model, 'language_id'); ?>
<?php echo $form->textArea($verse_transaction_model, 'language_id', array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($verse_transaction_model,'language_id'); ?>
</div>
To store in two tables (Model), you have to create two model object for each.
for ex:
$v_model = new Verse; // For save function
$vt_model = new VerseTransctions; // For save function
Pass these model object to view file and use as below.
For verse model text boxes, you have to use:
labelEx($v_model, 'verse_text');
For verse_tarnsction model text boxes, you have to use:
labelEx($vt_model, 'translation_text');
In action (save / update):
$v_model->attributes = $_POST["Verse"];
if($v_model->save())
{ $vt_model->attributes = $_POST["VerseTransctions"];
$vt_model->verse_id = $v_model->id; $vt_model->save();
}
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).