I would echo the value of the fields of a Model in cakePhp, without creating a Form.
Is it possible with core Helper or I have to build a custom one?
In particulare, if I use
echo $this->Form->input('field',array('label'=>"This is my value:"))
it prints out an input with a label.
What I want is something like:
echo $this->Form->label('field',array('label'=>"This is my value:"))
and it shall print out
"This is my value: <value_of_field>"
All the values that the FormHelper uses are from $this->request->data, so, try printing echo $this->request->data[YourModel][field];
If not, then you can define a view variable in the controller via $this->set('variableName', variableValue); and assign it in the form field.
Related
In NETSUITE
is there any way to access to a value inside of a combo-box at the item line level?
I need to access to a value after inserting an item but all functions get me null value.
I have tried
nlapiGetCurrentLineItemValue
and
nlapiGetFieldValue
Both functions are getting me null values.
Thanks,
Pablo.
In general (for user event and client script) below code should work
nlapiGetLineItemValue(LINE_ITEM_TYPE, YOUR_FIELD_ID, LINE_NUMBER);
eg on SO to get the line item Id:
nlapiGetLineItemValue('item', 'item', 1);
PS: Syntax is independent of data type or field type
If you mean combo box as a mulitselect, and if you're trying to access via User Event Script, use:
nlapiGetLineItemValues(type, fldname, linenum);
Note the 's' in nlapiGetLineItemValues
If its just a standard field, nlapiGetLineItemValue(type, fldname, linenum) should work.
Which call to use depends on what event you are capturing.
For instance if you are trying to access the value in a post sourcing, field changed or line validate event of a client script you would use nlapiGetCurrentLineItemValue('item', 'fieldname');
I have an object CuratedPage with property pageName.
I am creating an array of CuratedPage objects in controller and setting it for the view like this:
$this->set('curatedPages', $curatedPages);
In the view I am creating a dropdown of page names like this:
$pageNames = array();
foreach($curatedPages as $curatedPage) {
array_push($pageNames, $curatedPage->getPageName());
}
echo $this->Form->input('curatedPage', array('options' => $pageNames));
Is there a way in cakephp that will allow me to pass the array of CuratedPage objects to the Form->input(...) instead of creating an array of scalar values.
I'm not sure what you would expect the form helper to do in that case. However, depending on your PHP version (>= 5.2.0 required) the magic __toString() method might do it. If you implement it to return the pagename, then you would end up with the same result as with your posted snippet, ie an numerical indexed (the value attribute) HTML option list with the page names as labels.
However, implementing this only for that purpose in this specific view seems wrong to me, you're probably better of utilizing a custom helper, or as #BrianGlaz suggested prepare the data in the controller.
Finally, a simple, straight-answer question :D
Background: My company has changed the way that that they handle when certain sites are used. Previously, certain sites were only delivered to on certain weeks; now, every site delivers every week. They have asked me to get rid of the weeks field on their "add new site" form. The link between week and site is still necessary for the code to work, however, so I am trying to hide the field and populate it with every single week.
Auto-populating it is simple. I just do this:
echo $this->Form->input('Week', array('value'=>array('1','2','3','4','5')));
And when I debug the data getting sent from the form, I get the following, which is exactly the way I want it to work.
'Week' => array(
'Week' => array(
(int) 0 => '1',
(int) 1 => '2',
(int) 2 => '3',
(int) 3 => '4',
(int) 4 => '5'
)
So the next step is simply hide that input. Easy, right? I just do one of the following two things to the form:
echo $this->Form->hidden('Week', array('value'=>array('1','2','3','4','5')));
or
echo $this->Form->input('Week', array('value'=>array('1','2','3','4','5'), 'type'=>'hidden'));
All I have done is change the type to hidden. But now, the data returned from debugging the data from the form looks like this:
'Week' => array(
'Week' => '1 2 3 4 5'
So my question is, what is the difference in the way data is handled between a normal "input" field and a "hidden" field. Why does that difference occur, and why is it important? And for this particular issue, how do I get hidden data to behave in the same way as normal input data?
As you said in the comments, Week is a multiple choice input, so that's why the difference in value handling. If the input is something that allows multiple choice (like multiple select or checkboxes), then it's logical all values comes as an array. But the hidden type is basically a hidden text input (ok, maybe not, but I'm simplifying). Look, if you put
echo $this->Form->input('Week', array('type'=>'text', 'value'=>array('1','2','3','4','5')));
you'll get the same string as you have with the input field, so it isn't a matter of if it's hidden or not.
Now, for your case, you can add the week values as an string and change the way you handle the value on post, as a string instead of an array.
But, if by some reason that's not a possibility (too much code to change, etc), then keep the same code you have now, and hide it with css. Something like
echo $this->Form->input('Week', array('value'=>array('1','2','3','4','5'),
'div'=>array('style'=>'display:none')));
should do the trick (maybe a few more styles needs to be added, try with just that first).
I don't like the css solution that much, browsers can interpret css different ways and etc etc... But it fits your needs and you don't have to change anything else.
EDIT
On second though, it is possible to achieve the same array output with just hidden fields. It isn't pretty though. You have to change the name of every input to tell php it's an array, like this
echo $this->Form->input('Week1', array('type'=>'hidden', 'value'=>1, 'name'=>'data[Week][Week][]'));
echo $this->Form->input('Week2', array('type'=>'hidden', 'value'=>3, 'name'=>'data[Week][Week][]'));
echo $this->Form->input('Week3', array('type'=>'hidden', 'value'=>4, 'name'=>'data[Week][Week][]'));
The important part is the name option for every input, see the array reference? Specially the last empty []. This is actually how cakephp sends all data as an array. Have you tried to create a form without the FormHelper in cake? You wouldn't get the post data in $this->request->data['form_name'] unless you set the input names for all your form fields.
Cake sends all post data inside the $data array by naming all the inputs with <input name="data[ModelName][field_name]" and for multiple inputs is <input name="data[ModelName][field_name][]" (with the empty array at the end, so it gets numerically indexed). You can see that using any html inspector on any other form created with the formhelper in cake.
I hope it's clear... I'll stick with the css solution here. The multiple hidden inputs seems messier.
Essentially at the moment I have a text file written in the script of another language. It will be stored as an element, and called to be served up as text when needed.
I want to manipulate this file with the following:
Replace all the existing variables with a php array value storing that variable name. So if the variable looks like #foo, it becomes $variables['foo'].
Create an array out of the entire file, making each line a row in the array.
The result is something like
return array(
'first line of code which has a variable called ' . $variables['foo'] . 'in it',
'second line...'
);
What would the simplest method be to go about this, and is there a way to cache the process, or should I perhaps just save and store the new file some where I can access it?
Thank you!
Use str_replace() (documentation: php.net) to replace all occurences
Use explode() in the following way: How to put string in array, split by new line?
Caching.
I suggest you use View caching, a built in CakePHP helper.
To distinguish between the different caches for the different variable names, make sure you call the controller function with a parameter that discriminates among the variable names. E.g.:
public function generate_script($varname){
// some code
}
If the discriminating variable is not known on calling the controller method (but is e.g. determined inside the controller method), you should look into the Cache API
Hi I am having an incredibly hard time with what should be a simple issue.
As of CodeIgniter 1.7, '$this->input->post();' supported arrays but I cannot for get the values into the array for some reason. I have 7 check boxes that store into an array 'services[]' as you can see by this example view:
<?php $servicesdata = array (
'name' => 'services[]',
'value' => 'in_home_care',
);
echo form_checkbox($servicesdata, set_checkbox('services[]', 'in_home_care', FALSE)); ?>
I'm quite certain this is the correct fashion because the forms do validate nicely if something goes wrong. Now I start to have issues when storing the values. I have 7 columns that need to have some sort of value... at this point I don't care but ideally it would be a boolean (a binary would work okay too). Here is what I have so far in my controller that everyone claims should work but just does not:
$c = new Client($servicesdata);
$c->first_name = $this->input->post('first_name', TRUE);
$c->in_home_care = $this->input->post('services[in_home_care]');
You can see the string I put for an example that works perfectly and inserts into a VARCHAR type while the array won't go into the database whatsoever. I feel as if that I am missing something here - namely the 'value' in the array but I'm just not sure where to go from here. Any help would be much appreciated because the only method I can get to work sacrifices my checkbox validation! :(
If your checkbox is unchecked, the value of the checkbox will not be entered into the $_POST array. This is probably where you are having problems.
There are two ways around this. Either have one box checked by default, or use HTML like the following (which may or may not be best practice/valid, but has worked for me in the past).
<input type="hidden" name="services" value="foo" />
<input type="checkbox" name="services" value="in_home_care" />
In the event that the box is not checked, the value "foo" for the name value "services" will be passed to the $_POST array.
First of all, there is some redundancy in your form (I think). You can set the attributes of your checkbox in the array, including whether it is checked or not:
$servicesdata = array (
'name' => 'services[]',
'value' => 'in_home_care',
'checked' => FALSE,
);
echo form_checkbox($servicesdata);
secondly, because you're naming it in an array, the object needs to be accessed after being assigned to another variable:
$checkbox_array = $this->input->post('services');
$service_type=$checkbox_array[0];//will give you 'in_home_care', [1] would be next in array and so on
A way to store those choices in the db is to create a string out of them and store that.
$this->input->post('colors') ? $colors=implode('-',$this->input->post('colors')) : $colors='';
That checks to see if anything was actually checked in the colors array. If so, create a string out of the array values seperated by dashes. Else assign empty to colors.
Then after you read the db string back:
$profile_data['colors']=explode('-',$row->colors);
Then you can feed those values back into the form inputs.