Yii2: show user friendly validation errors when using try catch with transactions - yii

I am using bootstrap ActiveForm. Here is my form:
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use kartik\file\FileInput;
/* #var $this yii\web\View */
/* #var $model common\models\Customer */
/* #var $form yii\widgets\ActiveForm */
?>
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data'],
'id' => 'customer-form',
'enableClientValidation' => true,
'options' => [
'validateOnSubmit' => true,
'class' => 'form'
],
'layout' => 'horizontal',
'fieldConfig' => [
'horizontalCssClasses' => [
'label' => 'col-sm-4',
// 'offset' => 'col-sm-offset-2',
'wrapper' => 'col-sm-8',
],
],
]); ?>
<?= $form->field($model, 'email')->textInput(['maxlength' => true]) ?>
<?php ActiveForm::end(); ?>
Here is my model:
class Customer extends \yii\db\ActiveRecord
{
public $username;
public $password;
public $status;
public $email;
public $uploads;
public function rules()
{
return [
[['user_id', 'created_by', 'updated_by'], 'integer'],
[['created_at','uploads', 'updated_at','legacy_customer_id','fax','phone_two','trn'], 'safe'],
[['company_name','customer_name','username','password', 'tax_id'], 'string', 'max' => 200],
[['customer_name','email','legacy_customer_id','company_name','city'], 'required'],
[['is_deleted','status'], 'boolean'],
[['address_line_1', 'state','phone', 'country'], 'string', 'max' => 450],
[['address_line_2', 'city', 'zip_code'], 'string', 'max' => 45],
[['user_id','legacy_customer_id'], 'unique'],
['email', 'email'],
[['uploads'], 'file', 'maxFiles' => 10],
[['email'], 'unique', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['email' => 'email'], 'message' => 'This email address has already been taken.'],
[['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
];
}
}
Here is Controller
public function actionCreate() {
$model = new Customer();
if ($model->load(Yii::$app->request->post())) {
$transaction = Yii::$app->db->beginTransaction();
try
{
$user_create = \common\models\User::customeruser($model);
if ($user_create) {
$model->user_id = $user_create->id;
$auth = \Yii::$app->authManager;
$role = $auth->getRole('customer');
$auth->assign($role, $model->user_id);
}
if ($user_create && $model->save()) {
$photo = UploadedFile::getInstances($model, 'uploads');
if ($photo !== null) {
$save_images = \common\models\CustomerDocuments::save_document($model->user_id, $photo);
}
$transaction->commit();
return $this->redirect(['view', 'id' => $model->user_id]);
}
}catch (Exception $e)
{
$transaction->rollBack();
}
}
if (Yii::$app->request->isAjax) {
return $this->renderAjax('create', [
'model' => $model,
]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
Now the required attribute working used in the rules. It does not allow the form to submit until the required field filled with some value, but at the same time the unique attribute using with target class not working and allow the form to submit. After click the submit form the form will not submit but it does not show the error of unique validation. Regard I am using the form in bootstrap modal and I want the form will show the unique submit error before submission like the same as required working. I can do it using jQuery on blur function and send custom AJAX request but I want the default solution of Yii 2.
EDIT
This is where the error is throw due to user not being saved
public static function customeruser( $model ) {
$user = new User();
$user->username = $model->email;
$user->email = $model->email;
$user->setPassword ( $model->legacy_customer_id );
$user->generateAuthKey ();
if ( !$user->save () ) {
var_dump ( $user->getErrors () );
exit ();
} return $user->save () ? $user : null;
}
var_dump() shows the following
'username' => array (size = 1)
0 => string 'This username has already been taken.' (length = 37)
'email' => array (size = 1) 0 => string 'This email address has already been taken.' (length = 42).

As you are using the try catch block along with the transaction you should throw and catch such errors as the exception so that the transaction is rolled back, and the message is displayed to the user too.
You are not consuming or using the beauty of try{}catch(){} block with transactions. You should always throw an Exception in case any of the models are not saved and the catch block will rollback the transaction.
For example, you are saving the user in the function customeruser() by calling
$user_create = \common\models\User::customeruser($model);
and returning the user object or null otherwise and then in the very next line, you are verifying the user was created or not.
if ($user_create) {
You should simply throw an exception from the function customeruser() in case the model was not saved and return the $user object otherwise, you don't have to check $user_create again to verify if user was not saved the exception will be thrown and control will be transferred to the catch block and the lines after $user_create = \common\models\User::customeruser($model); will never be called.
I mostly use the following way when i have multiple models to save and i am using the transaction block.
$transaction = Yii::$app->db->beginTransaction ();
try {
if ( !$modelUser->save () ) {
throw new \Exception ( implode ( "<br />" , \yii\helpers\ArrayHelper::getColumn ( $modelUser->errors , 0 , false ) ) );
}
if ( !$modelProfile->save () ) {
throw new \Exception ( implode ( "<br />" , \yii\helpers\ArrayHelper::getColumn ( $modelProfile->errors , 0 , false ) ) );
}
$transaction->commit();
} catch ( \Exception $ex ) {
$transaction->rollBack();
Yii::$app->session->setFlash ( 'error' , $ex->getMessage () );
}
So you can do the same for your code
public static function customeruser( $model ) {
$user = new User();
$user->username = $model->email;
$user->email = $model->email;
$user->setPassword ( $model->legacy_customer_id );
$user->generateAuthKey ();
if(!$user->save()){
throw new \Exception ( implode ( "<br />" , \yii\helpers\ArrayHelper::getColumn ( $user->errors , 0 , false ) ) );
}
return $user;
}
change your actionCreate to the following
public function actionCreate() {
$model = new Customer();
if ( $model->load ( Yii::$app->request->post () ) ) {
$transaction = Yii::$app->db->beginTransaction ();
try {
$user_create = \common\models\User::customeruser ( $model );
$model->user_id = $user_create->id;
$auth = \Yii::$app->authManager;
$role = $auth->getRole ( 'customer' );
$auth->assign ( $role , $model->user_id );
if ( !$model->save () ) {
throw new \Exception ( implode ( "<br />" , \yii\helpers\ArrayHelper::getColumn ( $model->errors , 0 , false ) ) );
}
$photo = UploadedFile::getInstances ( $model , 'uploads' );
if ( $photo !== null ) {
$save_images = \common\models\CustomerDocuments::save_document ( $model->user_id , $photo );
}
$transaction->commit ();
return $this->redirect ( [ 'view' , 'id' => $model->user_id ] );
} catch ( \Exception $ex ) {
Yii::$app->session->setFlash ( 'error' , $ex->getMessage () );
$transaction->rollBack ();
}
}
if ( Yii::$app->request->isAjax ) {
return $this->renderAjax ( 'create' , [
'model' => $model ,
] );
}
return $this->render ( 'create' , [
'model' => $model ,
] );
}

There are two three thing :
1. You are not accepting email address by user input .
2. It will insert a blank entry if in database your field is taken as NULL
3. In second insertion it will show you the error message if db field is not NUll.
Solution:
1. Make db field Not NUll .
2. Take email as user input .
3. Try to print error
if ($model->validate()) {
// all inputs are valid
} else {
// validation failed: $errors is an array containing error messages
$errors = $model->errors;
}
4. remove 'skipOnError' => true,
Try Above I am sure you will get the solution
Help Full LINK For validation

Related

Yii2 : Upload Image using Kartik/FileInput Plugin

I want to upload an image via Kartik widget. After submitting the form, the $_FILE['Product'] has the data about the image but getInstance($model, 'images') returns null. Tried with images[], also null.
This is what I'm trying to var_dump in the controller:
public function actionCreate()
{
$model = new Product();
if ($model->load(Yii::$app->request->post())) {
var_dump(UploadedFile::getInstance($model, 'images[]'));die;
And this is my model Product:
<?php
namespace app\models;
use backend\models\CActiveRecord;
use Yii;
use omgdef\multilingual\MultilingualQuery;
use omgdef\multilingual\MultilingualBehavior;
use yii\web\UploadedFile;
/**
* This is the model class for table "product".
*
* #property int $id
* #property int $category_id
* #property int $quantity
* #property double $price
* #property int $sort
*
* #property Productlang[] $productlangs
*/
class Product extends CActiveRecord
{
public $images;
public static function find()
{
return new MultilingualQuery(get_called_class());
}
public function behaviors()
{
$allLanguages = [];
foreach (Yii::$app->params['languages'] as $title => $language) {
$allLanguages[$title] = $language;
}
return [
'ml' => [
'class' => MultilingualBehavior::className(),
'languages' => $allLanguages,
//'languageField' => 'language',
//'localizedPrefix' => '',
//'requireTranslations' => false',
//'dynamicLangClass' => true',
//'langClassName' => PostLang::className(), // or namespace/for/a/class/PostLang
'defaultLanguage' => Yii::$app->params['languageDefault'],
'langForeignKey' => 'product_id',
'tableName' => "{{%productLang}}",
'attributes' => [
'title',
'description',
'meta_title',
'meta_desc',
'url'
]
],
];
}
/**
* #inheritdoc
*/
public static function tableName()
{
return 'product';
}
/**
* #inheritdoc
*/
public function rules()
{
$string = $this->multilingualFields(['description', 'url']);
$string_59 = $this->multilingualFields(['meta_title']);
$string_255 = $this->multilingualFields(['meta_desc', 'title']);
$string[] = 'description';
$string[] = 'url';
$string_59[] = 'meta_title';
$string_255[] = 'meta_desc';
$string_255[] = 'title';
return [
[['quantity', 'price', 'title', 'meta_title', 'meta_desc'], 'required'],
[['category_id', 'quantity', 'sort'], 'integer'],
[$string, 'string'],
[$string_59, 'string', 'max' => 59],
[$string_255, 'string', 'max' => 255],
[['price'], 'number'],
['images', 'file']
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'category_id' => 'Category ID',
'quantity' => 'Quantity',
'price' => 'Price',
'sort' => 'Sort',
];
}
public function upload()
{
if ($this->validate()) {
foreach ($this->image as $file) {
$file->saveAs(\Yii::getAlias("#images") . "/products/" . $this->id . "_" . $this->image->baseName . '.' . $this->image->extension);
}
return true;
} else {
return false;
}
}
}
Tried with rules ['images', 'safe'] also ['images', 'file'] ( think the second one is not right because the attribute is an array, right ? ). The form is <?php $form = ActiveForm::begin(['options' => ['multipart/form-data']]); ?>.
Finally my input:
<?= $form->field($model, 'images[]')->widget(FileInput::class, [
'showMessage' => true,
]) ?>
Full controller action:
public function actionCreate()
{
$model = new Product();
if ($model->load(Yii::$app->request->post())) {
foreach (Yii::$app->params['languages'] as $language){
if(Yii::$app->params['languageDefault'] != $language){
$title_lang = "title_$language";
$model->$title_lang = Yii::$app->request->post('Product')["title_$language"];
$description_lang = "description_$language";
$model->$description_lang = Yii::$app->request->post('Product')["description_$language"];
$meta_title_lang = "meta_title_$language";
$model->$meta_title_lang = Yii::$app->request->post('Product')["meta_title_$language"];
$meta_desc_lang = "meta_desc_$language";
$model->$meta_desc_lang = Yii::$app->request->post('Product')["meta_desc_$language"];
}
}
if($model->save()){
$model = $this->findModel($model->id, true);
//Make urls
foreach (Yii::$app->params['languages'] as $language) {
if (Yii::$app->params['languageDefault'] != $language) {
$url_lang = "url_$language";
$title_lang = "title_$language";
$model->$url_lang = $model->constructURL(
$model->$title_lang,
$model->id
);
}else{
$model->url = $model->constructURL(
$model->title,
$model->id
);
}
}
//Upload Images
$model->images = UploadedFile::getInstance($model, 'images');
if (!($model->upload())) {
Yii::$app->session->setFlash('error', Yii::t('app', 'Some problem with the image uploading occure!'));
return $this->redirect(['create']);
}
if($model->update() !== false){
return $this->redirect(['view', 'id' => $model->id]);
}else{
Yii::$app->session->setFlash('error', Yii::t('app', 'Something went wrong. Please, try again later!'));
return $this->redirect(['create']);
}
}
}
return $this->render('create', [
'model' => $model,
]);
}
What looks like you are trying to upload a single image you should remove the [] from the input field name from the ActiveForm field declaration, and from models rules.
Single File
<?= $form->field($model, 'images')->widget(FileInput::class, [
'showMessage' => true,
'pluginOptions' => [
'showCaption' => false ,
'showRemove' => false ,
'showUpload' => false ,
'showPreview' => false ,
'browseClass' => 'btn btn-success btn-block' ,
'browseIcon' => '<i class="glyphicon glyphicon-camera"></i> ' ,
'browseLabel' => 'Select Profile Image'
] ,
'options' => ['accept' => 'image/*' ] ,
]) ?>
and from the following line
UploadedFile::getInstance($model, 'images');
Multiple Files
For multiple files you need to add 'options' => ['multiple' => true] for the field and change the attribute name to images[]
<?= $form->field($model, 'images[]')->widget(FileInput::class, [
'showMessage' => true,
'pluginOptions' => [
'showCaption' => false ,
'showRemove' => false ,
'showUpload' => false ,
'showPreview' => false ,
'browseClass' => 'btn btn-success btn-block' ,
'browseIcon' => '<i class="glyphicon glyphicon-camera"></i> ' ,
'browseLabel' => 'Select Profile Image'
] ,
'options' => ['accept' => 'image/*' ,'multiple'=>true] ,
]) ?>
and for receiving the uploaded files you should not specify the attribute as an array just change getInstance to getInstances and then try printing, it will show you all the images use foreach() to save all of them.
UploadedFile::getInstances($model, 'images');
I personally prefer to use a separate model for file uploading rather than using the ActiveRecord model.
Note: When using multiple files upload, you can also specify the 'maxFiles'=>1000 inside you model rules to limit the number of files to be uploaded
EDIT
For troubleshooting your code you should comment out the actionCreate from the controller and replace with the one i added below
public function actionCreate() {
$model = new Product();
if ( $model->load ( Yii::$app->request->post () ) ) {
foreach ( Yii::$app->params['languages'] as $language ) {
if ( Yii::$app->params['languageDefault'] != $language ) {
$title_lang = "title_$language";
$model->$title_lang = Yii::$app->request->post ( 'Product' )["title_$language"];
$description_lang = "description_$language";
$model->$description_lang = Yii::$app->request->post ( 'Product' )["description_$language"];
$meta_title_lang = "meta_title_$language";
$model->$meta_title_lang = Yii::$app->request->post ( 'Product' )["meta_title_$language"];
$meta_desc_lang = "meta_desc_$language";
$model->$meta_desc_lang = Yii::$app->request->post ( 'Product' )["meta_desc_$language"];
}
}
$transaction = Yii::$app->db->beginTransaction ();
try {
if ( !$model->save () ) {
throw new \Exception ( implode ( "<br />" , \yii\helpers\ArrayHelper::getColumn ( $model->errors , 0 , false ) ) );
}
//Make urls
foreach ( Yii::$app->params['languages'] as $language ) {
if ( Yii::$app->params['languageDefault'] != $language ) {
$url_lang = "url_$language";
$title_lang = "title_$language";
$model->$url_lang = $model->constructURL (
$model->$title_lang , $model->id
);
} else {
$model->url = $model->constructURL (
$model->title , $model->id
);
}
}
//save the new urls
if ( !$model->save () ) {
throw new \Exception ( implode ( "<br />" , \yii\helpers\ArrayHelper::getColumn ( $model->errors , 0 , false ) ) );
}
//Upload Images
$model->images = UploadedFile::getInstances ( $model , 'images' );
$model->upload ();
//commit the transatction to save the record in the table
$transaction->commit ();
Yii::$app->session->setFlash ( 'success' , 'The model saved successfully.' );
return $this->redirect ( [ 'view' , 'id' => $model->id ] );
} catch ( \Exception $ex ) {
$transaction->rollBack ();
Yii::$app->session->setFlash ( 'error' , Yii::t ( 'app' , $ex->getMessage () ) );
}
}
return $this->render ( 'create' , [
'model' => $model ,
] );
}
And comment out the upload() function of your model and add below function
public function upload() {
$skipped = [];
foreach ( $this->images as $file ) {
if ( !$file->saveAs ( \Yii::getAlias ( "#images" ) . "/products/" . $this->id . "_" . $this->image->baseName . '.' . $this->image->extension ) ) {
$skipped[] = "File " . $file->baseName . " was not saved.";
}
}
if ( !empty ( $skipped ) ) {
Yii::$app->session->setFlash ( 'error' , implode ( "<br>" , $skipped ) );
}
}
And for the ActiveForm make sure your input matches the following
$form->field($model, 'images[]')->widget(FileInput::class, [
'showMessage' => true,
'pluginOptions' => [
'showCaption' => false ,
'showRemove' => false ,
'showUpload' => false ,
'showPreview' => false ,
'browseClass' => 'btn btn-success btn-block' ,
'browseIcon' => '<i class="glyphicon glyphicon-camera"></i> ' ,
'browseLabel' => 'Select Profile Image'
] ,
'options' => ['accept' => 'image/*','multiple'=>true ] ,
]) ;

is_user_logged_in() return false even when logged in to WordPress?

I have a plugin that I created and I want to use the WP rest api controller pattern and extend the api.
<?php
/**
* Plugin Name: myplugin
* Plugin URI: h...
* Description: A simple plugin ...
* Version: 0.1
* Author: Kamran ...
* Author ....
* License: GPL2
function myplugin_register_endpoints(){
require_once 'server/controllers/my_ctrl.php';
$items=new items();
$items->register_routes();
}
add_action('rest_api_init','myplugin_register_endpoints');
.
.
I created a class in folder called server/controllers and inside it my_ctrl.php file with a class that extends WP_REST_Controller that looks like this
<?php
class items extends WP_REST_Controller {
/**
* Register the routes for the objects of the controller.
*/
public function register_routes() {
$version = '1';
$namespace = 'my-namespase/v' . $version;
$base = 'abc';
register_rest_route( $namespace, '/' . $base, array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => array(
'id' => array(
'required' => true,
'validate_callback' => function($param, $request, $key) {
return is_numeric( $param ) and ! is_null(get_post($param));//numeric post id value and there is valid post for this id
},
'sanitize_calback' => 'absint'
)
),
),
) );
register_rest_route( $namespace, '/' . $base . '/(?P<id>[\d]+)', array(
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'create_item' ),
'permission_callback' => array( $this, 'create_item_permissions_check' ),
'args' => array(
'id' => array(
'required' => true,
'validate_callback' => function($param, $request, $key) {
return is_numeric( $param ) and ! is_null(get_post($param));//numeric post id value and there is valid post for this id
},
'sanitize_calback' => 'absint'
)
),
),
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array( $this, 'delete_item' ),
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
'args' => array(
'id' => array(
'required' => true,
'validate_callback' => function($param, $request, $key) {
return is_numeric( $param ) and ! is_null(get_post($param));//numeric post id value and there is valid post for this id
},
'sanitize_calback' => 'absint'
)
),
),
) );
register_rest_route( $namespace, '/' . $base . '/schema', array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_public_item_schema' ),
) );
}
function get_items( $request ){
return new WP_REST_Response( array('message' => "list items"), 200 );
}
function create_item( $request ) {
.....
if($author_email==$user_email) {
return new WP_REST_Response( array('message' => 'success', 200 );
} else {
return new WP_Error('my-error', __(' error...','abc'), array( 'status' => 500 ));
}
}
//Remove vote////////////////////////////////////////////
function delete_item( $request ) {
...
if($author_email==$user_email) {
return new WP_REST_Response( array('message' => 'success', 200 );
} else {
return new WP_Error('my-error', __(' error...','abc'), array( 'status' => 500 ));
}
}
public function get_items_permissions_check( $request ) {
return true;
}
public function create_item_permissions_check( $request ) {
if ( !is_user_logged_in()) {
return new WP_Error('login error',__('You are not logged in','KVotes-voting'));
}
return true;
}
public function delete_item_permissions_check( $request ) {
return $this->create_item_permissions_check( $request );
}
protected function prepare_item_for_database( $request ) {
return array();
}
public function prepare_item_for_response( $item, $request ) {
return array();
}
public function get_collection_params() {
return array(
'page' => array(
'description' => 'Current page of the collection.',
'type' => 'integer',
'default' => 1,
'sanitize_callback' => 'absint',
),
'per_page' => array(
'description' => 'Maximum number of items to be returned in result set.',
'type' => 'integer',
'default' => 10,
'sanitize_callback' => 'absint',
),
'search' => array(
'description' => 'Limit results to those matching a string.',
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
),
);
}
}
I am logged in and I am using cookie authentication with Nonce in my plugin.
when I run my code and debug it with sublime xdebug extension I can see that I indeed hit the end points routes but although I am logged it in the lines: "is_user_logged_in()" = (bool) 0 and therefore the function create_item_permissions_check return new WP_Error(....);and not true;
therefore my rest callback "create_item" is not invoked, I don't understand why is_user_logged_in() return false even when I am logged in.
The solution was to send the logged in user info to my custom class as a parameter to the constructor and then use the user data in the permission check function and other functions that needs the user info:
class items extends WP_REST_Controller {
/**
* Register the routes for the objects of the controller.
*/
private $loged_in;//bool
private $user;
public function __construct($logged,$cur_user) {
= $logged;
$this->user = $cur_user;
}
.
.
.
public function create_item_permissions_check( $request ) {
if($this->loged_in!=1){
return new WP_Error('login error',__('You are not logged in','....'));
}
return true;
}
.
.
.
}
And my plugin myplugin_register_endpoints looks as follows:
function myplugin_register_endpoints(){
require_once 'server/controllers/my_ctrl.php';
$items=new items(is_user_logged_in(),wp_get_current_user());
$items->register_routes();
}
now when I route to one of the URL's And hit the end points and the check permission is invoked with the needed user data. $this->loged_in!=1 when thew user is not logged in, otherwise the permission check returns true .
I ran into precisely this issue. It seems that the architecturally correct way to handle the problem is for the ajax request to include a nonce. See the references discussed in this answer

Yii CJuiAutoComplete widget: event of empty response message

If the response does not contain data about the city, I want to see the output message. Also i want to change css of text in textfield when I get an empty response.
I have:
View:
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name'=>'city_id',
'value'=>'',
'source'=>CController::createUrl('/PromouterCity/autoComplete'),
'options'=>array(
'showAnim'=>'fold',
'minLength'=>'0',
'select'=>'js:function( event, ui ) {
$("#city_id").val( ui.item.name );
$("#selectedvalue").val( ui.item.id);
return false;
}',
),
'placeholder' => "Search...",
),
));
Controller:
public function actionAutoComplete(){
$match=$_GET['term'];
if(!empty($match)){
$match = addcslashes($match, '%_');
$q = new CDbCriteria( array(
'condition' => "name LIKE :match",
'params' => array(':match' => "$match%")
));
$query = City::model()->findAll($q);
}else{
$query=array(
'0'=>array(
'id'=>'1',
'name'=>'London',
)
);
}
$list = array();
foreach($query as $q){
$data['label']=$q['name'];
$data['id']= $q['id'];
$data['name']= $q['name'];
$list[]= $data;
unset($data);
}
echo CJSON::encode($list);
Yii::app()->end();
}
Decision:
'response'=> 'js:function( event, ui ) {
if (ui.content.length === 0) {
$("#empty-message").text("your message");
} else {
$("#empty-message").empty();
}
}',

call model in zend form using dependencies + zend framework 2

I am trying to fetch my category model in zend form for working out with select element with zend framework 2.
after lot of code searching I found I can either inject or pull dependencies.
Following code I did in my module.php
I want categoryTable.php(model) file in my CategoryForm.php
public function getServiceConfig()
{
return array(
'factories' => array(
'Category\Model\CategoryTable' => function($sm) {
$tableGateway = $sm->get('CategoryTableGateway');
$table = new CategoryTable($tableGateway);
//echo "<pre>";print_r($table);echo "</pre>";
return $table;
},
'CategoryTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Category());
return new TableGateway('Of_Restaurants_Category', $dbAdapter, null, $resultSetPrototype);
},
'Category\Form\CategoryForm' => function ($sm) {
$service = $sm->get('Category\Model\CategoryTable');
$form = new Form;
$form->setService($service);
return $form;
}
),
);
}
then I put following code in my controller.
$form = $this->getServiceLocator()->get("Category\Form\CategoryForm");
Then I Put following code in my CategoryForm.php
public function getCategoryTable()
{
if (!$this->categoryTable) {
$sm = $this->getServiceLocator();
$this->categoryTable = $sm->get('Category\Model\CategoryTable');
}
return $this->categoryTable;
}
And then I call it in same file like this way
public function __construct($name = null)
{
parent::__construct('category');
echo "<pre>";print_r($this->getCategoryTable());die;
.... other code
I found this error
Fatal error: Call to undefined method Category\Form\CategoryForm::getServiceLocator() in D:\wamp\www\zendapp\module\Category\src\Category\Form\CategoryForm.php on line 120
please help. and am I missing something?
I found the solution
Step :1
Here is my module.php code
public function getServiceConfig()
{
return array(
'invokables' => array(
'Category\Form\CategoryForm' => 'Category\Form\CategoryForm',
),
'factories' => array(
'Category\Model\CategoryTable' => function($sm) {
$tableGateway = $sm->get('CategoryTableGateway');
$table = new CategoryTable($tableGateway);
//echo "<pre>";print_r($table);echo "</pre>";
return $table;
},
'CategoryTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Category());
return new TableGateway('Of_Restaurants_Category', $dbAdapter, null, $resultSetPrototype);
},
),
);
}
Step :2
Then in controller I made this change
// $form = new CategoryForm();
// Service locator now injected
$form = $this->getServiceLocator()->get('Category\Form\CategoryForm');
Step :3
Then In my categoryForm.php I made below changes
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
protected $serviceManager;
public function getCategoryTable()
{
if (!$this->categoryTable) {
$sm = $this->getServiceManager();
$this->categoryTable = $sm->get('Category\Model\CategoryTable');
}
return $this->categoryTable;
}
protected function getCatList()
{
$groups = $this->getCategoryTable()->fetchAll();
return $groups;
}
public function getServiceManager()
{
if ( is_null($this->serviceManager) ) {
throw new Exception('The ServiceManager has not been set.');
}
return $this->serviceManager;
}
public function setServiceManager(ServiceManager $serviceManager)
{
$this->serviceManager = $serviceManager;
// Call the init function of the form once the service manager is set
$this->init();
return $this;
}
public function __construct($name = null) // constructor I finished immediately
{
parent::__construct('category');
}
I add INIT() function to fetch servicemanager
public function init()
{
$this->setAttribute('method', 'post');
$options = array();
foreach ($this->getCatList() as $cat) {
$options[$cat->id] = $cat->title;
}
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'parent_id',
'options' => array(
'label' => 'Parent Category',
'empty_option' => 'Please choose Parent Category',
'value_options' => $options,
),
));
}
Hope this will help who are new ZF2.

yii user-management edit field

I have managed to set-up yii-user-management. Thanks to help from here.
However, when I am in profile/fields/admin .
I click on edit field and then change the field from required 'no' to 'yes' in the dropdown, then save, but nothing happens.
I also get :
CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(0) NOT NULL DEFAULT 0' at line 1. The SQL statement executed was: ALTER TABLE profile ADD `` (0) NOT NULL DEFAULT 0
When creating a field.
Let me know if you need some code or files. Any help appreciated.
Here is the profile/models/YumProfileField.php
class YumProfileField extends YumActiveRecord
{
const VISIBLE_HIDDEN=0;
const VISIBLE_ONLY_OWNER=1;
const VISIBLE_REGISTER_USER=2;
const VISIBLE_USER_DECISION=3;
const VISIBLE_PUBLIC=4; // Field is public even if the user decides to hide it
/**
* Returns the static model of the specified AR class.
* #param string $className
* #return YumProfileField
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function isPublic($user = null) {
if($user == null)
$user = Yii::app()->user->id;
if(!$this->visible)
return false;
if($privacy = YumUser::model()->findByPk($user)->privacy) {
if($privacy->public_profile_fields & pow(2, $this->id))
return true;
}
return false;
}
public function tableName()
{
$this->_tableName = Yum::module('profile')->profileFieldTable;
return $this->_tableName;
}
public function scopes()
{
return array(
'forAll'=>array(
'condition'=>'visible='.self::VISIBLE_PUBLIC,
),
'forUser'=>array(
'condition'=>'visible>='.self::VISIBLE_REGISTER_USER,
),
'forOwner'=>array(
'condition'=>'visible>='.self::VISIBLE_ONLY_OWNER,
),
);
}
public static function itemAlias($type,$code=NULL) {
$_items = array(
'field_type' => array(
'INTEGER' => Yum::t('INTEGER'),
'VARCHAR' => Yum::t( 'VARCHAR'),
'TEXT'=> Yum::t( 'TEXT'),
'DATE'=> Yum::t( 'DATE'),
'DROPDOWNLIST' => Yum::t('DROPDOWNLIST'),
'FLOAT'=> Yum::t('FLOAT'),
'BOOL'=> Yum::t('BOOL'),
'BLOB'=> Yum::t('BLOB'),
'BINARY'=> Yum::t('BINARY'),
'FILE'=> 'FILE',
),
'required' => array(
'0' => Yum::t('No'),
'1' => Yum::t('Yes'),
),
'visible' => array(
self::VISIBLE_USER_DECISION => Yum::t('Let the user choose in privacy settings'),
self::VISIBLE_PUBLIC => Yum::t('For all'),
self::VISIBLE_REGISTER_USER => Yum::t('Registered users'),
self::VISIBLE_ONLY_OWNER => Yum::t('Only owner'),
self::VISIBLE_HIDDEN => Yum::t('Hidden'),
),
);
if (isset($code))
return isset($_items[$type][$code]) ? $_items[$type][$code] : false;
else
return isset($_items[$type]) ? $_items[$type] : false;
}
}
The YumFieldsController:
class YumFieldsController extends YumController
{
const PAGE_SIZE=10;
public function accessRules()
{
return array(
array('allow',
'actions'=>array('index', 'create', 'update', 'view', 'admin','delete'),
'users'=>array(Yii::app()->user->name),
'expression' => 'Yii::app()->user->isAdmin()'
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
public function actionView()
{
$this->layout = Yum::module()->adminLayout;
$this->render('view',array(
'model'=>$this->loadModel('YumProfileField'),
));
}
public function actionCreate() {
$this->layout = Yum::module()->adminLayout;
$model = new YumProfileField;
// add to group?
if(isset($_GET['in_group']))
$model->field_group_id=$_GET['in_group'];
if(isset($_POST['YumProfileField'])) {
$model->attributes = $_POST['YumProfileField'];
$field_type = $model->field_type;
if($field_type == 'DROPDOWNLIST')
$field_type = 'INTEGER';
if($model->validate()) {
$sql = 'ALTER TABLE '.YumProfile::model()->tableName().' ADD `'.$model->varname.'` ';
$sql .= $field_type;
if ($field_type!='TEXT' && $field_type!='DATE')
$sql .= '('.$model->field_size.')';
$sql .= ' NOT NULL ';
if ($model->default)
$sql .= " DEFAULT '".$model->default."'";
else
$sql .= (($field_type =='TEXT' || $model->field_type=='VARCHAR')?" DEFAULT ''":" DEFAULT 0");
$model->dbConnection->createCommand($sql)->execute();
$model->save();
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('create',array(
'model'=>$model,
));
}
public function actionUpdate()
{
$this->layout = Yum::module()->adminLayout;
$model = $this->loadModel('YumProfileField');
if(isset($_POST['YumProfileField']))
{
$model->attributes=$_POST['YumProfileField'];
// ALTER TABLE `test` CHANGE `profiles` `field` INT( 10 ) NOT NULL
// ALTER TABLE `test` CHANGE `profiles` `description` INT( 1 ) NOT NULL DEFAULT '0'
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
));
}
public function actionDelete()
{
$this->layout = Yum::module()->adminLayout;
if(Yii::app()->request->isPostRequest)
{
// we only allow deletion via POST request
$model = $this->loadModel('YumProfileField');
$sql = 'ALTER TABLE '.YumProfile::model()->tableName().' DROP `'.$model->varname.'`';
if ($model->dbConnection->createCommand($sql)->execute()) {
$model->delete();
}
if(!isset($_POST['ajax']))
$this->redirect(array('index'));
}
else
throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
}
public function actionIndex()
{
$this->layout = Yum::module()->adminLayout;
$dataProvider=new CActiveDataProvider('YumProfileField', array(
'pagination'=>array(
'pageSize'=>self::PAGE_SIZE,
),
'sort'=>array(
'defaultOrder'=>'position',
),
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
public function actionAdmin()
{
$this->layout = Yum::module()->adminLayout;
$dataProvider=new CActiveDataProvider('YumProfileField', array(
'pagination'=>array(
'pageSize'=>self::PAGE_SIZE,
),
'sort'=>array(
'defaultOrder'=>'position',
),
));
$this->render('admin',array(
'dataProvider'=>$dataProvider,
));
}
}
YumProfile.php model
class YumProfile extends YumActiveRecord
{
const PRIVACY_PRIVATE = 'private';
const PRIVACY_PUBLIC = 'public';
/**
* #var array of YumProfileFields
*/
static $fields=null;
public function init()
{
parent::init();
// load profile fields only once
$this->loadProfileFields();
}
public function afterSave() {
if($this->isNewRecord)
Yii::log(Yum::t( 'A profile been created: {profile}', array(
'{profile}' =>json_encode($this->attributes))));
else
Yii::log(Yum::t( 'A profile been update: {profile}', array(
'{profile}' => json_encode($this->attributes))));
return parent::afterSave();
}
public function recentComments($count = 3) {
$criteria = new CDbCriteria;
$criteria->condition = 'id = ' .$this->id;
$criteria->order = 'createtime DESC';
$criteria->limit = $count;
return YumProfileComment::model()->findAll($criteria);
}
public function beforeValidate() {
if($this->isNewRecord)
$this->timestamp = time();
return parent::beforeValidate();
}
/**
* #param string $className
* #return YumProfile
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
// All fields that the user has activated in his privacy settings will
// be obtained and returned for the use in the profile view
public function getPublicFields() {
if(!Yum::module('profile')->enablePrivacySetting)
return false;
$fields = array();
if($privacy = YumUser::model()
->cache(500)
->with('privacy')
->findByPk($this->user_id)
->privacy->public_profile_fields) {
$i = 1;
foreach(YumProfileField::model()->cache(3600)->findAll() as $field) {
if(
(($i & $privacy)
&& $field->visible != YumProfileField::VISIBLE_HIDDEN)
|| $field->visible == YumProfileField::VISIBLE_PUBLIC)
$fields[] = $field;
$i*=2;
}
}
return $fields;
}
/**
* Returns resolved table name
* #return string
*/
public function tableName()
{
$this->_tableName = Yum::module('profile')->profileTable;
return $this->_tableName;
}
public function rules()
{
$required = array();
$numerical = array();
$rules = array();
$safe = array();
foreach (self::$fields as $field) {
$field_rule = array();
if ($field->required == 1)
array_push($required, $field->varname);
if ($field->field_type == 'int'
|| $field->field_type == 'FLOAT'
|| $field->field_type =='INTEGER'
|| $field->field_type =='BOOLEAN')
array_push($numerical, $field->varname);
if ($field->field_type == 'DROPDOWNLIST')
array_push($safe, $field->varname);
if ($field->field_type == 'VARCHAR' || $field->field_type == 'TEXT') {
$field_rule = array($field->varname,
'length',
'max'=>$field->field_size,
'min' => $field->field_size_min);
if ($field->error_message)
$field_rule['message'] = Yum::t($field->error_message);
array_push($rules,$field_rule);
}
if ($field->match) {
$field_rule = array($field->varname,
'match',
'pattern' => $field->match);
if ($field->error_message)
$field_rule['message'] = Yum::t( $field->error_message);
array_push($rules,$field_rule);
}
if ($field->range) {
// allow using commas and semicolons
$range=explode(';',$field->range);
if(count($range)===1)
$range=explode(',',$field->range);
$field_rule = array($field->varname,'in','range' => $range);
if ($field->error_message)
$field_rule['message'] = Yum::t( $field->error_message);
array_push($rules,$field_rule);
}
if ($field->other_validator) {
$field_rule = array($field->varname,
$field->other_validator);
if ($field->error_message)
$field_rule['message'] = Yum::t( $field->error_message);
array_push($rules, $field_rule);
}
}
array_push($rules,
array(implode(',',$required), 'required'));
array_push($rules,
array(implode(',',$numerical), 'numerical', 'integerOnly'=>true));
array_push($rules,
array(implode(',',$safe), 'safe'));
$rules[] = array('allow_comments, show_friends', 'numerical');
$rules[] = array('email', 'unique');
$rules[] = array('email', 'CEmailValidator');
$rules[] = array('privacy', 'safe');
return $rules;
}
public function relations()
{
$relations = array(
'user' => array(self::BELONGS_TO, 'YumUser', 'user_id'),
'comments' => array(self::HAS_MANY, 'YumProfileComment', 'profile_id'),
);
$fields = Yii::app()->db->cache(3600)->createCommand(
"select * from ".YumProfileField::model()->tableName()." where field_type = 'DROPDOWNLIST'")->queryAll();
foreach($fields as $field) {
$relations[ucfirst($field['varname'])] = array(
self::BELONGS_TO, ucfirst($field['varname']), $field['varname']);
}
return $relations;
}
// Retrieve a list of all users that have commented my profile
// Do not show my own profile visit
public function getProfileCommentators() {
$commentators = array();
foreach($this->comments as $comment)
if($comment->user_id != Yii::app()->user->id)
$commentators[$comment->user_id] = $comment->user;
return $commentators;
}
public function getProfileFields() {
$fields = array();
if(self::$fields)
foreach(self::$fields as $field) {
$varname = $field->varname;
$fields[$varname] = Yum::t($varname);
}
return $fields;
}
public function name() {
return sprintf('%s %s', $this->firstname, $this->lastname);
}
public function attributeLabels()
{
$labels = array(
'id' => Yum::t('Profile ID'),
'user_id' => Yum::t('User ID'),
'privacy' => Yum::t('Privacy'),
'show_friends' => Yum::t('Show friends'),
'allow_comments' => Yum::t('Allow profile comments'),
);
if(self::$fields)
foreach (self::$fields as $field)
$labels[$field->varname] = Yum::t($field->title);
return $labels;
}
/**
* Load profile fields.
* Overwrite this method to get another set of fields
* Makes use of cache so the amount of sql queries per request is reduced
* #since 0.6
* #return array of YumProfileFields or empty array
*/
public function loadProfileFields()
{
if(self::$fields===null)
{
self::$fields=YumProfileField::model()->cache(3600)->findAll();
if(self::$fields==null)
self::$fields=array();
}
return self::$fields;
}
}
looks like you're trying to add a column with no name or type? MySQL ALTER TABLE examples.
Also, check your db user has permissions to ALTER TABLE.