Yii create, update form with different fields - yii

In Yii, the same form is generally used for Create and Update.
So, if I have fields like Email, Password, ...other_fields.... during Create but I do not want to display Email and Password field specifically during update, but all other remaining fields should be there in Update. How can this be done, without changing the _form.php
Also, there are more instances, like - date_entered, date_updated etc., which are never required to be displayed, but evaluated internally and stored into table. Hidden fields will not be useful as, they will still be visible in the source. The only way, I can think of is, by completely removing these fields from the _form.php
I have tried with rules() like 'safe' and also used scenarios, but I could not solve this problem.
Any help in this regard, will be highly appreciated.

For edit/update action, isNewRecord field is set true / 1 in model object.
Like
<?php
if($model->isNewRecord)
{
$form->textField ......
}
?>
Just check in this field in _form.php and add email and password field if it is false / 0.

What you are trying to do is really simple.
You can just create another form lets say, _form2 and copy paste the original data from the _form into it and just remove the data that you don't want to be filled.
So after that, lets say that you don't want email to be there when you update, so go into the controller and edit the action like so and render the other form :
public function actionUpdate($id){
$model = new WhateverModel();
// Lets say that you want to insert your own data it any field, do it like this :
$model->whateverattribute_column = $whateverData;
#some code is here... yada yada
$this->render('update',array('model'=>$model));
}
After that you can render the partial in the 'update' view like so :
<?php $this->renderPartial('_form2',array('model'=>$model)); ?>
Done!

Related

How can we make an Item in socialengine?

I know how to get an item of the particular table. Like for user we can have
$userItem = Engine_Api::_()->getItem("user", $userId);
or for a custom table
$customItem = Engine_Api::_()->getItem("custom", $customeId);
I want to know the code or method how can I make my $customItem to work the same way as $userItem works for users table. So that I can get data or manipulate the data of custom table
Thanks for your help. :-)
You can achieve that by creating a model. Check how it's done in /application/modules/User/Model. There is User.php file that declares User_Model_User and methods available for User such as getTitle, get Href etc.
You may use similar approach for your custom item. You will also need to create a file similar to /application/modules/User/Model/DbTable/User.php to declare table for your custom items.

silverstripe 3 - How to add access control to generated data objects?

Good afternoon,
Please let me know if this question is not clear enough, I'll try my best to make as straight-forward as possible.
How can I add access control to objects that are generated by an end-user using my data object?
Example: I have a class that extends a DataObject. Someone logs in the back-end; fills out the form that's generated by the CMS for the data object. A record is then created in the database by the CMS.
I would like to add an access control to that newly created record in the database.
For a code scenario you can take a look at one of my posts: Silverstripe 3 - Unable to implement controller access security from CMS
The only other way I can think of asking this question is: How to Dynamically (or programmatically) create permissions for records that are created by a DataObject extension via the CMS?
Thanks for your assistance.
Update - Sample Code
///>snippet, note it also has a Manager class that extends ModelAdmin which manages this!
class component extends DataObject implements PermissionProvider{
public static $db = array(
'Title' => 'Varchar',
'Description' => 'Text',
'Status' => "Enum('Hidden, Published', 'Hidden')",
'Weight' => 'Int'
);
///All the regular permission checks (overrides), for the interface goes here, etc...
///That is: canView, canDelete, canEdit, canCreate, providePermissions
}
Now, from the back-end an end-user can add components using the Manager Interface that's generated by extending ModelAdmin. How can I add individual permissions to those added components by the end-user?
Thanks.
Update 2
Example: Add Process Data Object that extends ModelAdmin will give you this in the back end
Then, when you click on the generated 'Add Process' button, you'll get this:
Finally, someone fills out the form and clicks on the 'Create' button, which saves the data in the database. That looks like this:
Now, on that record thats created in MySQL I'd like to add granular permissions to that record. Meaning, for every record created I want to be able to Deny/Allow access to it via a Group/Individual, etc.
Is that even possible with the SilverStripe framework? Thanks.
Implement the functions canView, canEdit, canDelete, and/or canCreate on your DataObject.
Each function will return true or false depending on the conditions you set - any conditions, not just what is defined in the CMS.
See the example code on the tutorial site.

How to load an ObjectModel with submitted values in Prestashop?

I'm using an HelperForm to display my form which has multilang features.
I also created a Model with fields (some are multilang).
The problem is, once the user submit the form, how can I fill the data from the submit form into my new model ?
I tried this :
$instance = new MyModel();
$instance->validateController();
But for some kind of odd reasons, it doesn't save the language fields, only the "direct" fields.
I thought about using validateFields and validateFieldsLang, but they stop at the first encountered error ; I'd like to list all the possible errors at once.
How can I do this?
Are you using it from the front or back office?
Admin controllers have this
$this->copyFromPost($this->object, $this->table);
Thich basically calls the object's validation

How to assign unique id attribute to each table row of a CGridView?

I am attempting to assign a unique id to each table row in Yii's CGridView.
Preferably something like $data->id from the database table.
I have been unsuccessful at adding an id attribute to each rendered <tr>.
Any suggestions would be most appreciated.
CGridView have an option called 'rowHtmlOptionsExpression' , you can declare like the followings to assign row an id
'rowHtmlOptionsExpression' => 'array("id"=>$data->id)',
It's better than hacking into 'rowCssClassExpression'
Good luck !
Modern solution (since Yii 1.1.13)
This is now possible to do using the rowHtmlOptionsExpression attribute, which allows assigning arbitrary HTML attributes to each rendered table row. For example:
'rowHtmlOptionsExpression' => '["id" => $data->id]'
Original answer (earlier versions)
Not directly possible because CGridView does not support it, but there are a couple of straightforward solutions that you can try.
Subclass CGridView (good)
Simply create your own class MyGridView extends CGridView and override the renderTableRow method to spit out ids on every row. Have a look at the stock implementation, which does for the class attribute exactly what you 'd like to do for the id attribute.
Use a CSS class instead (not so good)
Speaking of class attributes, the rowCssClassExpression property can be used to dynamically generate classes out of the box. IMHO this is a bad workaround, but it's there.
You could extend CGridView to add that functionality.
or be a bit hacky with rowCssClassExpression.
'rowCssClassExpression' => '\'" data-id="\' . $data->rowID'
Try the information I posted here:
How to set key value in CGrideView when grid is populated from table-view
In essence, as long as your dataprovider to the CGridview provides the data->id in a form that it understands, it will auto handle the $data->id stuff for you automatically so that it's easily available to javascript.
CGridView.rowHtmlOptionsExpression is undefined
I don't think that we can use rowHtmlOptionsExpression

Yii framework generated forms field types dont corespond to database

I am using gii to generate CRUD operations for a large number of tables, but the generated form uses a text box for the boolean data types instead of the checkbox
In my table model I modified the rules function to use a boolean validator for my fields
public function rules() {
return array(
...
array('single, random, public', 'boolean'),
...
);
}
but the generated form still uses a text box instead of a checkbox, any idea why ?
looks like you found as good an answer as you are going to get over in the Yii forums :) :
http://www.yiiframework.com/forum/index.php?/topic/10690-gii-crud-generator-field-type-is-always-text-for-boolean/