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);
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.
I'm trying to validate submitted data against existing Model/Entity/POPO, however I can't get it to work in any simple way.
All of this is takes place inside a controller action.
So, I can do like this:
$constraints = new Assert\Collection([
'username' => [new Assert\NotBlank()],
'email' => [new Assert\Email()],
]);
$violationList = $this->get('validator')->validate($request->request->all(), $constraints);
However to do that in every action makes no sense, as having all constraints in a class would be a lot better. So, Validation component allows to do like this:
// All constraints are defined inside Customer class
$customer = new Customer();
$violationList = $this->get('validator')->validate($customer);
Violation list is full of errors now, as $customer is an empty object, but the problem is I can't find a way to use data from POST AND validate it against constraints that are defined in the class.
It is possible to write extra component/helper that would take POST data and then will call bunch of ->setUsername(), ->setEmail(), etc., but that doesn't seem right considering you can easily map Model to POST data, if:
Form component is involved;
OR using ConstraintsCollection manually;
Am I missing something obvious here or there is no out-of-the-box possibility? Thanks!
AFAIK the form component is the one responsible for mapping post data to your entity. So you have two choices
Use a form, like that you will have your data mapped and your model validated
Skip the form but then you have to map request params to your entity manually. then validate your model with $this->get('validator')->validate($customer);
Edit :
The form role is to map data coming from request ( html form , api .... ) to a model. Validation could be done with from or without it as its the validator component who does the job , it should be noted that the validation is done on the model and not the form.
If you want to skip the form check this question: Populate entity from data array without form/request although the form component is very useful specially if you are using the same logic in many places ( create / edit .. )
I have a form which I use in my viewscript. My viewscript looks as follows:
<?php
$this->titel = "Arbeitskalender Termine";
$this->headTitle($this->titel);
foreach($this->aktermine as $termin) :
$this->nr=$this->escape($termin->nr);
$this->kopfnr=$this->escape($termin->kopfnr);
$this->datum=$this->escape($termin->datum);
$this->zeit=$this->escape($termin->zeit);
$this->thema=$this->escape($termin->thema);
echo $this->form ;
endforeach;
?>
I get my form (it is a table) I get the same repeats of the form like records in my table. But I don´t see any record in the form fields. What´s wrong? How can I get the values of my dataset objects in each field?
If I use a viewscript in html it works fine.
Use the the form’s bind() method to attach your model to the form. The value for each field will be extracted from the model and displayed in the form.
This is used in two ways:
When displaying the form, the initial values for each element are
extracted from the model.
After successful validation in isValid(), the data from the form is put back into the model.
To use this method you need to implement getArrayCopy() and exchangeArray() in your model Aktermine.
So in your action you'll have something like this:
$form = new YourForm();
$form->bind($aktermine);
Please see the example of Editing an Album, in the documentation.
Read also Binding Objects to Forms
If you are using doctrine, just add getArrayCopy() into your entity like this:
public function getArrayCopy(){
return get_object_vars($this);
}
And then within your controller action :
$form->bind($yourEntity);
In a joomla custom made component there are multiple posts on a page, and every post contains multiple comment, so in view i want to call comments by post id. please suggest a good method to make it working.
You have two options. The first, is to attach the comment id as a URL paramater and retrieve it within the model as needed like so:
$comment_id = JRequest::getApplication()->input->get('comment_id');
If you wish to pass in a parameter when calling the model from the view class, you need to get an instance of the MVC path model instead of using the short cut method. So, instead of using this in the JView class:
$this->items = $this->get('Items');
You would do this instead:
$model = $this->getModel();
$this->items = $model->getItems($comment_id);
Hope this helps.
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');