registration form post field as empty - yii

i'm using Yii-user, and have made some modification to the user/views/user/registration.php file.
for some reason even when i fill in firstname and lastname, it still says i left those fields empty. any idea why?
$form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'registration-form',
'type'=>'vertical',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
));
<?php echo $form->textField($model,'email', array('class' => 'input-block-level', 'placeholder' => $model->getAttributeLabel('email'))); ?>
<?php echo $form->passwordField($model,'password', array('class' => 'input-block-level', 'placeholder' => $model->getAttributeLabel('password'))); ?>
<?php echo $form->passwordField($model,'verifyPassword', array('class' => 'input-block-level', 'placeholder' => $model->getAttributeLabel('verifyPassword'))); ?>
<p class="text-seperator"> about you </p>
<?php echo $form->textField($profile,'firstname', array('class' => 'input-block-level', 'placeholder' => $model->getAttributeLabel('firstname'), 'maxlength'=> 255)); ?>
<?php echo $form->textField($profile,'lastname', array('class' => 'input-block-level', 'placeholder' => $model->getAttributeLabel('lastname'), 'maxlength'=> 255)); ?>
and in my user/models/registrationform.php i have this
class RegistrationForm extends User {
public $firstname; //added
public $lastname; //added
public $verifyPassword;
public $verifyCode;
public function rules() {
$rules = array(
array('firstname, lastname, password, verifyPassword, email', 'required'),
array('firstname', 'length', 'max'=>225, 'min' => 3,'message' => UserModule::t("Incorrect First Name (length between 3 and 225 characters).")),
array('lastname', 'length', 'max'=>225, 'min' => 3,'message' => UserModule::t("Incorrect Last Name (length between 3 and 225 characters).")),
.........
}
}
to fill up firstname and last name using the original code they used this...
$profileFields=Profile::getFields();
if ($profileFields) {
foreach($profileFields as $field) {
?>
<!--div class="row"-->
<?php echo $form->labelEx($profile,$field->varname); ?>
<?php
if ($widgetEdit = $field->widgetEdit($profile)) {
echo $widgetEdit;
} elseif ($field->range) {
echo $form->dropDownList($profile,$field->varname,Profile::range($field->range));
} elseif ($field->field_type=="TEXT") {
echo$form->textArea($profile,$field->varname,array('rows'=>6, 'cols'=>50));
} else {
echo $field->varname.'<br />';
echo $form->textField($profile,$field->varname,array('size'=>60,'maxlength'=>(($field->field_size)?$field->field_size:255)));
}
?>
<?php echo $form->error($profile,$field->varname); ?>
<!--/div-->
<?php
}
}

I think you need to change this,
you have passed $model in here:
<?php echo $form->textField($model,'email', array('class' => 'input-block-level', 'placeholder' => $model->getAttributeLabel('email'))); ?>
but you have given $profile here:
<?php echo $form->textField($profile,'firstname', array('class' => 'input-block-level', 'placeholder' => $model->getAttributeLabel('firstname'), 'maxlength'=> 255)); ?>
if you check out the generated html, you will notice that the generated input has different name!

i'm not sure why this works (would be great is someone could explain in detail)
but the issue was here..
public function rules() {
$rules = array(
array('firstname, lastname, password, verifyPassword, email', 'required'),
array('firstname', 'length', 'max'=>225, 'min' => 3,'message' => UserModule::t("Incorrect First Name (length between 3 and 225 characters).")),
array('lastname', 'length', 'max'=>225, 'min' => 3,'message' => UserModule::t("Incorrect Last Name (length between 3 and 225 characters).")),
.........
}
}
firstname, lastname when this is removed it works. and it still is set as required.

Related

Yii2: Images stored in different tables

Here my controller:
$model = new VehicleType();
if ($model->load(Yii::$app->request->post())) {
if($model->validate()){
$model->save();
$id = $model->id;
$model->file = UploadedFile::getInstance($model, 'file');
if($model->file){
$id = $model->id;
$imageName = "vehicletype_".$id.'_'.getdate()[0];
$model->file->saveAs('uploads/'.$imageName.'.'.$model->file->extension);
$station = VehicleType::findOne($id);
$station->image = '#web/uploads/'.$imageName.'.'.$model->file->extension;
$station->save();
}
return $this->redirect(['vehicletype/index']);
}
} else {
return $this->renderAjax('create', [
'model' => $model,
]);
}
}
My view:
<div class="row">
<div class="col-lg-5">
<?php $form = ActiveForm::begin(['id' => 'station-form', 'options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($model, 'name') ?>
<?= $form->field($model, 'description')->textarea() ?>
<?= $form->field($model, 'file')->fileInput() ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'contact-button']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
My model:
public function rules()
{
return [
[['description'], 'string'],
[['record_status'], 'integer'],
[['name', 'image'], 'string', 'max' => 255]
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Name',
'description' => 'Description',
'image' => 'Image',
'record_status' => 'Record Status',
];
}
/**
* #return \yii\db\ActiveQuery
*/
public function getVehicles()
{
return $this->hasMany(Vehicles::className(), ['vehicle_type_id' => 'id']);
}
}
With this I can upload only one picture/post, I want one post to have multiple pictures, so I create a new table call 'Image' to stored my pictures and have a one-to-many relationship.
But I run into a problem, how can I add data to 2 tables from just 1 form
I'm using Yii2 basic template
Thanks
Step 1
First create two variables in your vehicletype model.
public $uploadedImages;
public $imageforNewtable = array();
Step 2
Make sure to mention this imageforNewtable variable in your model rule.
[['imageforNewtable'], 'image', 'extensions' => 'png, jpg, JPEG'],
Step 3
In your form:
<?php $form = ActiveForm::begin(['id' => 'station-form', 'options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($model, 'imageforNewtable[]')->fileInput(['accept' => 'image/*']); ?>
Step 4
In Your Controller:
$model->uploadedImages = UploadedFile::getInstances($model,'imageforNewtable');
// Make sure to put "Instances" (plural of Instance) for uploading multiple images
$model->imageforNewtable = array(); // To avoid blank entries as we have entries in $_FILES not in $_POST.
foreach ($model->uploadedImages as $singleImage)
{
$model->imageforNewtable[] = time() . '_' . $singleImage->name;
}
Now bulk insert data in Images table :
$bulkInsertforimages = array(); //defined benchInsert array for images
$columnsArray = ['blog_id', 'image']; //Column names in which bulk insert will take place.
if ($model->imageforNewtable != '') {
foreach ($model->imageforNewtable as $singleData) {
$bulkInsertforimages[] = [
'blog_id' => $model->id,
'image' => $singleData,
];
}
}
if (count($bulkInsertforimages) > 0) {
$command = \Yii::$app->db->createCommand();
$command->batchInsert('YOUR_IMAGE_TABLE', $columnsArray, $bulkInsertforimages)->execute();
}

Yii update cgridview via ajax button not working.

I have in my controller:
public function actionFilterClients {
if (Yii::app()->request->isAjaxRequest) {
if (isset($_POST['category_id'])) {
$criteria = new CDbCriteria;
$criteria->condition = "user_id=:user_id";
$criteria->params = array(':user_id' => Yii::app()->user->id);
$criteria->compare('category_id',$_POST['category_id'],true);
$dataProvider = new CActiveDataProvider('Client', array(
'criteria'=>$criteria,
));
$this->renderPartial('transfer_step_3' , array('dataProvider'=>$dataProvider)) ;
}
}
}
In my view among other things I have:
<?php $filter=$this->beginWidget('CActiveForm', array(
'id'=>'client-filter-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array('class'=>'form-horizontal'),
)); ?>
<label for="category_id">View clients in category:</label>
<?php echo CHtml::dropDownList('category_id','',Client::clientCategories(), array('options' => array('2'=>array('selected'=>true)))); ?>
<?php
echo CHtml::ajaxButton(
'Filter Clients',
'filterclients',
array(
'type'=>'POST',
'update' => 'client-grid' ,
'success' =>"function(data) {
\$.fn.yiiGridView.update('client-grid');}",
)
);
?>
<?php $this->endWidget(); ?>
and
<?php $this->widget('bootstrap.widgets.TbGridView',array(
'type'=>'bordered striped condensed',
'id'=>'client-grid',
'ajaxUpdate' => true ,
'rowCssClassExpression'=>'($data->duplicate==2)?"yellow":($data->duplicate==1?"blue":"")',
'dataProvider'=>(isset($dataProvider)?$dataProvider:$clients->moveclients()),
'template'=>"{items}\n{pager}",
'columns'=>array(
array(
'class'=>'CCheckBoxColumn',
'selectableRows'=>2,
'id'=>'clients',
),
'name',
'surname',
'telephone',
'email',
array(
'header'=>'Category',
'name' => 'category_title',
'type' => 'raw',
'value' => '$data->category->title',
),
),
)); ?>
Because this is a multi-step form, the cgridview dataprovider defaults to listing all clients ($clients->moveclients() lists all clients).
The ajax button posts the category_id to the client/filterclients url correctly.
I can see with firebug that actionFilterClients returns the rendered html correctly (with the correct clients) but the gridview is not updated...
Any ideas on why not?
In the end I added another view that had only a gridview in it and modified my code thus:
Controller:
$this->renderPartial('_ajax_transfer_step_3' , array('dataProvider'=>$dataProvider)) ;
Original view:
<?php $filter=$this->beginWidget('CActiveForm', array(
'id'=>'customer-filter-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array('class'=>'form-horizontal'),
)); ?>
<label for="category_id">View customers in category:</label>
<?php
echo CHtml::dropDownList('category_id', '', Customer::customerCategories(),
array(
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('filtercustomers'),
'data'=>'js:jQuery(this).serialize()',
'success'=>'function(response) {
jQuery("#customer-grid").html(response)
}',
)
)
);
?>
<?php $this->endWidget(); ?>
Now it just replaces a portion of the page.
I still haven't figured out why my original code didn't update the gridview though.

change placeholder text from array

I'm using the yii-user extension and i'm trying the add proper label to the 'placeholder' attribute. really new to Yii so still trying to get the grasp of things.
I've added the attributeLabels() method in the class in the models folder.
class RegistrationForm extends User {
/**
* Declares attribute labels.
*/
public function attributeLabels()
{
return array(
'email'=>'Email Address',
'firstname'=>'First Name',
'lastname' => 'Last Name',
'verifyPassword' = 'Retype Password'
);
}
}
Here is my code in my /views/ folder
$form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'registration-form',
'type'=>'vertical',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
));
<?php echo $form->textField($model,'email', array('class' => 'input-block-level', 'placeholder' => 'email')); ?>
<?php echo $form->passwordField($model,'password', array('class' => 'input-block-level', 'placeholder' => 'password')); ?>
<?php echo $form->passwordField($model,'verifyPassword', array('class' => 'input-block-level', 'placeholder' => 'verifyPassword')); ?>
<?php
$profileFields=Profile::getFields();
if ($profileFields) {
foreach($profileFields as $field) {
if ($widgetEdit = $field->widgetEdit($profile)) {
//echo $widgetEdit;
} elseif ($field->range) {
echo $form->dropDownList($profile,$field->varname,Profile::range($field->range),array('class' => 'input-block-level'));
} elseif ($field->field_type=="TEXT") {
echo $form->textArea($profile,$field->varname,array('rows'=>6, 'cols'=>50));
} else {
//echo $field->varname;
if ($field->varname == 'firstname')
{
$placeholder = 'First Name';
}
else if ($field->varname == 'lastname')
{
$placeholder = 'Last Name';
}
else
{
$placeholder = $field->varname;
}
echo $form->textField($profile,$field->varname,array('size'=>60,'maxlength'=>(($field->field_size)?$field->field_size:255),'class' => 'input-block-level', 'placeholder' => $placeholder));
}
echo $form->error($profile,$field->varname);
}
}
?>
how would i make attributeLabels() work on my echo $form->textField($profile,$field->varname,array('size'=>60,'maxlength'=>(($field->field_size)?$field->field_size:255),'class' => 'input-block-level', 'placeholder' => $placeholder)); ?
You can get the text label for the specified attribute with getAttributeLabel() like:
$model->getAttributeLabel('verifyPassword');
E.x:
<?php echo $form->passwordField($model,'verifyPassword',
array('class' => 'input-block-level',
'placeholder' => $model->getAttributeLabel('verifyPassword')));
?>
you don't have to edit class RegistrationForm extends User
open protected/modules/user/model/User.php
add add/edit your custom labels in the attributeLabels() method
public function attributeLabels()
{
return array(
'id' => UserModule::t("Id"),
'username'=>UserModule::t("username"),
'password'=>UserModule::t("Password"),
'verifyPassword'=>UserModule::t("Retype Password"),
'firstname'=>UserModule::t("First Name"), //ADDED
'lastname'=>UserModule::t("Last Name"), // ADDED
'email'=>UserModule::t("Email Address"), //EDITED
'verifyCode'=>UserModule::t("Verification Code"),
'activkey' => UserModule::t("Activation Key"),
'createtime' => UserModule::t("Registration Date"),
'create_at' => UserModule::t("Registration Date"),
'lastvisit_at' => UserModule::t("Last Visit"),
'superuser' => UserModule::t("Superuser"),
'status' => UserModule::t("Status"),
);
}
and to get the label to show in your view file. use this
<?php echo $form->passwordField($model,'verifyPassword',
array('class' => 'input-block-level',
'placeholder' => $model->getAttributeLabel('email')));
?>

Yii framework CMultifileupload not working

I'm trying to implement a multiple file upload using CMultiFileUpload with CUploadedFile, but it doesn't work. Specifically, _POST is not working even considering that I'm using 'enctype' => 'multipart/form-data' in the options in the view:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'examen-form',
'enableAjaxValidation'=>false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>
and this is the widget and parameters used for CMultiFileUpload:
<div class="row">
<?php echo $form->labelEx($model,'archivo_foto')?>
<?php //echo CHtml::activeFileField($model,'archivo_foto')?>
<?php $this->widget('CMultiFileUpload', array(
'model' => $model,
'name' => 'archivo_foto',
'accept' => 'jpeg|jpg|gif|png|txt', // useful for verifying files
'duplicate' => 'Duplicate file!', // useful, i think
'denied' => 'Invalid file type', // useful, i think
'max' => 10,
'htmlOptions' => array( 'multiple' => 'multiple', 'size' => 25 ),
)); ?>
<?php echo $form->error($model,'archivo_foto')?>
</div>
On the other hand, the controller action is implemented this way:
public function actionUpdateam($id)
{
$model=$this->loadModel($id);
$dir=Yii::getPathOfAlias('application.uploads');
$model->archivo_documento='funciona 0';
if(isset($_POST['Examen'])) {
$model->attributes=$_POST['Examen'];
// THIS is how you capture those uploaded images: remember that in your CMultiFile widget, you set 'name' => 'archivo_foto'
$images = CUploadedFile::getInstancesByName('archivo_foto');
// proceed if the images have been set
$model->archivo_documento='funciona uno';
if (isset($images) && count($images) > 0) {
$model->archivo_documento='funciona dos';
// go through each uploaded image
foreach ($images as $image) {
echo $image->name.'<br />';
$image->saveAs($dir.'/'.$image->name);
$model->archivo_foto = $model->archivo_foto."+".$image->name;
}
// save the rest of your information from the form
if ($model->save()) {
$this->redirect(array('view','id'=>$model->id));
}
}
}
$this->render('update_am',array(
'model'=>$model,
));
}
And at last, I think that is important to mention the rule used for the model (it might be the cause of the problem as well):
array('archivo_foto','file','allowEmpty'=>true,'maxFiles'=>10),
I think that the problem is in post method, because the controller is not uploading the files and is not making any changes in the database. But I'm not sure.
Try changing to 'htmlOptions'=>array('multiple'=>true).
Add following code in the Form
'htmlOptions' => array(
'enctype' => 'multipart/form-data',
),
Like
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'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'=>true,
'htmlOptions' => array(
'enctype' => 'multipart/form-data',
),
)); ?>
I am using the same extension and it is wokring for me with these codes
In the view file
<?php $form = $this->beginWidget('CActiveForm', array(
'id'=>'adpost-form',
'enableClientValidation'=>true,
'clientOptions' => array(
'validateOnSubmit'=>true,
'validateOnChange'=>false,
'afterValidate'=>'js:submiAjaxForm'
),
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>
<?php
$this->widget('CMultiFileUpload', array(
'name' => 'photo_name',
'accept' => 'jpeg|jpg|gif|png',
'duplicate' => 'File is not existed!',
'denied' => 'Not images', // useful,
'htmlOptions' => array(
'style' =>'color: transparent;',
),
));
?>
And in the contoller
$images = CUploadedFile::getInstancesByName('photo_name');
foreach ($images as $image => $pic) {
//----------------- Renaming image before uploading
$extension = $pic->getExtensionName();
$newName = "image_".$adModel->id;
$newName .= "_".$imgCount.".".$extension;
$imgCount++;
if ($pic->saveAs($newPath.$newName)) {
// add it to the main model now
$img_add = new AdsPhotosTbl;
$img_add->photo_name = $newName;
$img_add->ad_id = $adModel->id;
$img_add->status = 1;
if(!$img_add->save()){
$error = true;
$upload_error = true;
break;
}
}else{
$upload_error = true;
$error = true;
break;
}
}

Pass array as attribute from form

In my model I have attribute - specifications:
class Category extends CActiveRecord
{
private $_specifications = array();
public function getSpecifications()
{
return $this->_specifications;
}
public function setSpecifications($specifications)
{
$this->_specifications = implode(', ', $specifications);
}
So I want specifications to be an array.
My view file:
<div id="specifications" class="row">
<?php echo $form->labelEx($model,'specifications'); ?>
<?php echo $form->textField($model,'specifications',array('rows'=>6, 'cols'=>50, 'name'=>'Category[specifications][0]', 'class' => 'clonedInput')); ?>
<?php echo $form->textField($model,'specifications',array('rows'=>6, 'cols'=>50, 'name'=>'Category[specifications][1]', 'class' => 'clonedInput')); ?>
<?php echo $form->error($model,'specifications'); ?>
</div>
When I send form I get an error:
htmlspecialchars() expects parameter 1 to be string, array given
...
public static function encode($text)
84 {
85 return htmlspecialchars($text,ENT_QUOTES,Yii::app()->charset);
86 }
I've tried to disable encoding:
<?php echo $form->textField($model,'specifications',array('encode'=>false, 'rows'=>6, 'cols'=>50, 'name'=>'Category[specifications][0]', 'class' => 'clonedInput')); ?>
<?php echo $form->textField($model,'specifications',array('encode'=>false, 'rows'=>6, 'cols'=>50, 'name'=>'Category[specifications][1]', 'class' => 'clonedInput')); ?>
But in that case it's another error:
Array to string conversion
...
2216 $html .= ' ' . $name . '="' . ($raw ? $value : self::encode($value)) . '"';
Can anybody give an advice, what should I do to pass an array from form? Thanks.
How can you pass an array to a single textfield? What should it display?
You can can create a virtual attribute for that.
In the model:
private $_specifications = array();
public function getSpecifications()
{
return implode(', ', $this->_specifications);
}
The view can remain untouched.
Edit:
Of course you need a setter too, if you want to be able to write to the attribute.
public function setSpecifications($specifications)
{
$this->_specifications = explode(', ', $specifications);
}
Please refer to http://www.yiiframework.com/wiki/167/understanding-virtual-attributes-and-get-set-methods/
Since your specifications attribute is an array, you should simply make a loop to display corresponding inputs, e.g. :
foreach ($model->specifications as $s)
{
echo Chtml::textField('Category[specifications][]', $s, array('rows'=>6, 'cols'=>50, 'class' => 'clonedInput'));
}
<?php echo $form->textField($model,'specifications',array('encode'=>false, 'rows'=>6, 'cols'=>50, 'name'=>'Category[specifications][0]', 'class' => 'clonedInput')); ?>
<?php echo $form->textField($model,'specifications',array('encode'=>false, 'rows'=>6, 'cols'=>50, 'name'=>'Category[specifications][1]', 'class' => 'clonedInput')); ?>
I think the specifications will be displayed twice.