Hey all, have a slight problem here. In CakePHP I have a controller that uses several models. When creating a form in a view, the view will always name my UI elements based on what the first model is when I specify $uses = array('Model') so for instance if my User model is the first in my array then my UI elements will receive id="User(fieldname)" and name="data['User'][fieldname]"
Anybody know how I switch the models my views are using so I can name them properly according to the data I am manipulating?
When creating a form use the full dot notation:
echo $this->Form->create('ModelName');
echo $this->Form->text('ModelName.field_name');
echo $this->Form->input('ModelName.field_name');
Related
I'm using ActiveForm with Yii2 and by default it seems to generate default id's for fields if you don't set one, in the format of:
{action-name}-{field-name}
So for example if I had a field with the name of foo_bar used in an action of actionSettings then the id of this field would be generated as:
settings-foo_bar
I would prefer this to just be foo_bar.
Is this possible to change on a form by form basis?
Based on the answer provided by #Bizley I was investigating how the method calculated the name and found out there is another way to achieve this as well.
You can simply override the formName method of your respective model to return a blank value, such as:
public function formName() {
return '';
}
Whilst this has less overheads as you don't need to create a new class, it will also affect other things within your form such as the field names and also should not be used for forms which contain multiple different models as explained here.
Lastly, because this question was about changing how Yii formats the id, #Bizleys answer is the correct one; my solution is just another option of possibly achieving it another way.
ActiveField id is by default created based on the form's model name and field's name.
If you want to change it for the whole form override the method that does it:
protected function getInputId()
{
return $this->_inputId ?: Html::getInputId($this->model, $this->attribute);
}
and use this modified class in your form.
In Yii, I have a view belonging to class A, and in a view corresponding to this class, I want to add a form to create a model of another class.
So, in protected/views/pictures/myview.php, I have:
<?php
/* #var $this PicturesController */
/* #var $model Pictures */
$objectForm = new Objects();
$newForm = ObjectsController::renderPartial('create',array('model'=>$objectForm),true);
?>
And I am trying to add a form to render protected/views/objects/create.php. But the above code doesn't work since the view is still trying to load the create form from the Pictures class. Since I am obtaining errors saying that same properties of Objects are not defined, because the system is loading the Pictures create form.
How can I add the create form of the Objects model?
Note: I added "applications.controllers.*" to the import array in main.php, but I understand this is a bad practice. Is there any possible solutions that doesn't involve me to do this?
Thanks.
In this case you just needed to link the correct view in the renderPartial:
$objectForm = new Objects();
$newForm = ObjectsController::renderPartial('/objects/create',array('model'=>$objectForm),true); // "/objects/create", and not "create".
Those errors were giving because Yii understood you were rendering to the create view of pictures, and you gave it the model of Objects (rather than the model of Pictures).
If we were talking about a render call in the controller, and you want to render a view from another action that belongs to another controller, you do not render to this view directly. You have to "redirect" to the action of this other controller (that belongs to the other class).
try like this this
$this->renderPartial('application.staff.views.default.create',array());
this is the format
$this->renderPartial('context_text.views.display._display_text');
How can I access the elements of my view in controller. lets I have a field name 'type' in my form field and I want to access the value of that field in my controller class. How can I achieve that.
I saw where we are fetching all attributes
$model->attributes=$_POST['Alerts'];
How can I get a specific attribute from $model->attributes
Thanks in advance
Yii model attributes are named as objects of class.
An activeRecord class you can find the model attributes by their names.
Considering you used the correct names which match the model attributes in the post, you can access them simply by using
$model->attname
Eg , a model "student" has attribute name and class sent in the post, you can access
$class = $model->class;
$name = $model->name;
If you have some post value which is not part of model (should not happen normally), you can access it as you do normally using the $_POST directive or using print_r to find all parameters if debugging.
print_r($_POST);
I'm wondering how can I insert tabular data in Yii.
Of course, I've followed docs in this aspect however there are few differences in my situation.
First of all, I want to save two models, exactly as in the docs article. The main difference is that there might be more that one element for second model (simple one to many relation in database).
I use CHtml to build my forms. I implemented a jQuery snippet to add more input groups dynamically.
I'm unable to show my code now as it's totally messed up and not working currently.
My main question is: how to handle the array of elements for second model in Yii?
Define your two models in controller
$model1= new Model1();
$model2= new Model2();
//massive assignments
$model1->attributes=$_POST['Model1']
$model2->attributes=$_POST['Model2']
//validation
$valid= $model1->validate();
$valid =$valid && $model2->validate();
if($valid){
$model1->save(false);
$model1->save(false);
}
if you want to access fields individually dump your post and you can view the the
post array format or instead of doing massive assignments you can manually assign like this
$model1->field1 =$_POST['Model1']['field1'];
//validation logic
...
if($valid){
$model1->save(false);
$model1->save(false);
}
How to create a multi-model form in Yii? I searched the entire documentation of Yii, but got no interesting results. Can some one give me some direction or thoughts about that? Any help will be appreciable.
In my expirience i got this solution to work and quickly understandable
You have two models for data you wish collect. Let's say Person and Vehicle.
Step 1 : Set up controller for entering form
In your controller create model objects:
public function actionCreate() {
$Person = new Person;
$Vehicle = new Vehicle;
//.. see step nr.3
$this->render('create',array(
'Person'=>$Person,
'Vehicle'=>$Vehicle)
);
}
Step 2 : Write your view file
//..define form
echo CHtml::activeTextField($Person,'name');
echo CHtml::activeTextField($Person,'address');
// other fields..
echo CHtml::activeTextField($Vehicle,'type');
echo CHtml::activeTextField($Vehicle,'number');
//..enter other fields and end form
put some labels and design in your view ;)
Step 3 : Write controller on $_POST action
and now go back to your controller and write funcionality for POST action
if (isset($_POST['Person']) && isset($_POST['Vehicle'])) {
$Person = $_POST['Person']; //dont forget to sanitize values
$Vehicle = $_POST['Vehicle']; //dont forget to sanitize values
/*
Do $Person->save() and $Vehicle->save() separately
OR
use Transaction module to save both (or save none on error)
http://www.yiiframework.com/doc/guide/1.1/en/database.dao#using-transactions
*/
}
else {
Yii::app()->user->setFlash('error','You must enter both data for Person and Vehicle');
// or just skip `else` block and put some form error box in the view file
}
You can find some examples in these two Yii wiki articles:
Yii 1.1: How to use a single form to collect data for two or more models?
Yii 1.1: How to use single form to collect data for two or more models (CActiveForm and Ajax Validation edition).
You don`t need a multi-model. The right use of the MVC pattern requires a Model that reflects your UI.
To solve it, you'll have to use a CFormModel instead of an ActiveRecord to pass the data from View to Controller. Then inside your Controller you`ll parse the model, the CFormModel one, and use the ActiveRecord classes (more than one) to save in database.
Forms Overview and Form Model chapters in Yii Definitive Guide contains some details and samples.
Another suggestions -
Also we can use Wizard Behavior, It's an extension that simplifies the handling of multi-step forms. In which we can use multi model forms for registration process flow or others.
Demo - http://wizard-behavior.pbm-webdev.co.uk/