I just discorvered that Form::model binding existed and I'm delighted with (it's wonderful). I tried it with text, email, and even select, every single time it worked.
My question is, will it work with a <select multiple>? If it does, how am I supposed to use it and what is the correct way to save an array in the database? (This may be awful but I concatenate all the options of the array with a separator and save it as text, I'm sure it's not the correct way to do it).
just like this :
Form::select('menus[]', $menus, null, array(
'multiple' => true,
'class' => 'form-control'
));
take a note :
param 1 : should be your field name (if want multiple add array tag aftrer field name e.g :menus[])
param 2 : list menu (array) e.g : array('value1' => 'text1', 'value2' => 'text2')
param 3 : selected values. (should be null because Form::model will perform automatically matching values from database. and make sure the field name has same with key data result from database)
param 4 : is property for element <select> you can add class, id, and etc..
Related
I have 4 fields
"name" => "required",
"parent_id" => "",
"url" => "required",
"url_id" => "required",
The fields name, url and url_id should be required
But now i wan't to change only the parent_id and i don't want to change the name url and url_id so i dont change these.
I want the 3 fields that are required not too be required if i only change the parent_id
Edit:
in my frontend there are 2 ways to update these 4 fields,
is in a form where i can change all the required fields.
is where i only want to change the parent_id but then i get into the error that name url and url_id are required when i want to update
Despite selected answer could work as expected, it's not the prettier solution.
You can use the validation rule provided by laravel required_without
https://laravel.com/docs/5.8/validation#rule-required-without
So if you want those fields to be required when parent_id is not present you can simply do:
"name" => "required_without:parent_id",
"parent_id" => "",
"url" => "required_without:parent_id",
"url_id" => "required_without:parent_id",
i believe you get these values from a form so you can store this field before applying required to other fields
if parent_id has a value return nothing but if it's empty return required
$condition = $_POST('parent_id') ? "" : "required"
then
"name" => $condition ,
"url" => $condition ,
"url_id" => $condition ,
Hope that's what you are looking for
Well, there are no conditionally required in MySQL however what you can do is to set default values to required fields so that when you do not what to set them the default values got set by default.
ALTER TABLE x MODIFY COLUMN col NOT NULL DEFAULT '[]';
Another option is to rollback required field so that it can be null.
ALTER TABLE x MODIFY COLUMN col NULL;
We are working on a website based on SilverStripe and this site is connected with a SugarCRM database.
We have created a Form with a CheckboxSet with multiple values and store it a variable called $data['Interessen']
$set_entry_parameters = array(
"session" => $session_id,
"module_name" => "Contacts",
"name_value_list" => array(
array(
"name" => "interessen_c",
"value" => $data['Interessen']['fotografie']
),
array(
"name" => "interessen_c",
"value" => $data['Interessen']['dance']
)
)
);
Now the last array with "interessen_c" overwrites the previous values. We want to add more than one value at one time.
How is this possible?
If the contents of $data['Interessen'] can only have values from a fixed list of possibilities, I'd recommend making the field interessen_c into type multienum ("Multi-Selection Dropdown" field).
For that field create a list of all available items in Sugar (e.g. in Studio or creating the app_list_strings entry manually via code).
Sugar will then support multiple values in this field and display them nicely.
If your program writes the data by communicating with the Sugar REST API you can then just pass the $data['Interessen'] array as the value for interessen_c and Sugar will know what to do with it.
If your program writes the data directly to the interessen_c field in the database, then the field contents must adhere to the following format:
^value1^,^value2^,^value3^
So with ^ around each value and all items being separated by ,
Here an example of how to convert the array values to such a string in PHP:
$interessen = array();
foreach ($data['Interessen'] as $value) {
// add value surrounded by ^ to array
$interessen[] = "^$value^";
}
// transform values in array to string with items being separated by ,
$interessen = implode(',', $interessen);
Side-Note:
From within Sugar one can use encodeMultienumValue($arr) and unencodeMultienum($string) to convert from array to db-string format and back.
Both functions are defined in include/utils.php
I am using Yii and I have a dropdown using the following example:
$form->dropDownList($model,'sex',array('1'=>'men','2'=>'women'), array('options' => array('2'=>array('selected'=>true))));
Here I am able to choose which option is selected. If I set two as shown in the example above the selected option is women as expected.
I am not able to statically set the selected option as I need to use a variable. I have $selectedId which equal 2, but when doing for example:
array('options' => array("$selectedId"=>array('selected'=>true))));
or doing like this:
array('options' => array($selectedId=>array('selected'=>true))));
I am getting no errors, but the dropdown does not have the expected selected option. Is it possible to use a variable when defining an array key?
Update
True string:
CHtml::dropDownList('package','',CHtml::listData(Services::model()->findAll(array('condition'=>'is_internet = 1','params'=>array())), 'id', 'name'),array('id'=>'package'))
You can set it by setting the second parameter:
CHtml::dropDownList('package',$selectedId,CHtml::listData(Services::model()->findAll(array('condition'=>'is_internet = 1','params'=>array())), 'id', 'name'),array('id'=>'package'))
I want the user to be able to choose a restaurant from a dropdown list. I'm achieving this by the simple:
echo $this->Form->input(
'Restaurant', array('multiple'=>false, array('empty' => true)));
The issue I have is: some restaurants have duplicate names, and I need a way for the user to know which is which. I'd like to have the ID and/or address within the select options like:
<li value='62'>McDonalds (1234 Happy St) - #62</li>
<li value='63'>McDonalds (9876 French Fry Ln) - #63</li>
...etc
Is there a way to do this? I'm obviously capable of doing it w/ normal HTML, but... would be nice to stay in CakePHP.
Thanks ahead of time for any thoughts/suggestions/direction!
When you load your restaurants you're actually getting an array like this
array (
ID => NAME,
ID => NAME
)
Basically, it's an associated array with the ID as the key and the display field as the value. So as long as you modify that array via the find operation or via normal PHP array iteration, you can achieve your goal.
EDIT
So your answer is CakePHP VirtualFields
In your model you define it as
var $virtualFields = array(
'rest_unique_name' => 'CONCAT(Restaurant.first_name, " ", Restaurant.address)'
);
In your controller you do this
$opts = array(
'fields' => array('id', 'rest_unique_name')
);
$restaurants = $this->Restaurant->find('list', $opts);
I am working with a hook_form_alter on a CCK type (for you drupal-ers). I have a field that is normally a select list in my node form. However, in this instance, I want to hide the select list, and populate its value in the form with an SQL query.
Everything was going nicely. I could see that my desired value was showing up in the HTML source, so I knew my query was executing properly. However, when I submit the form, it only inserts the first character of the value. A few of my tests were values of 566, 784, 1004 - the column values were 5,7,1, respectively.
At first I thought it had to be the DB column attributes, but when I removed my form_alter that makes the field hidden and select the value manually, the correct value is inserted?!?
<?php
function addSR_form_service_request_node_form_alter(&$form, $form_state) {
if (arg(0) == 'user' && is_numeric(arg(1))) {
$account = arg(1);
$club = 2589;
$form['field_sr_account'] = array( '#type' => 'hidden',
'#value' => $club
);
}
}
?>
Can anyone see why only the first character would be inserted??
Note: I have tried deleting and recreating the column, using #value & #default_value, and it is still submitting only the first character of the integer. Also, I eliminated the submit handler as a possible cause by removing it, which still resulted in only one character being submitted
More Updates - Still Searching!
Okay, some good questions. Allow me to answer them:
The DB column type is integer(4)
The HTML the hook produces is :
input type="hidden" name="field_sr_account" id="edit-field-sr-account" value="2589"
Latest Update: I think the issue has been narrowed to the structure of the array. When I do var_dump on this field after the form alter has been processed, this is what I get..
[43] => Array
(
[#type] => hidden
[#default_value] => 2589
[#post] => Array
(
)
[#programmed] =>
[#tree] =>
[#parents] => Array
(
[0] => field_sr_account
)
[#array_parents] => Array
(
[0] => field_sr_account
)
[#weight] => 0.016
[#processed] => 1
[#description] =>
[#attributes] => Array
(
)
[#required] =>
[#input] => 1
[#process] => Array
(
[0] => form_expand_ahah
)
[#name] => field_sr_account
[#id] => edit-field-sr-account
[#value] => 2589
[#defaults_loaded] => 1
[#sorted] => 1
)
What is the structure of the field that I can set the form value to. It's gotta be something like what abhaga is suggesting..
Since the field you are trying to change was originally using a select widget, CCK will be looking for $form_state['values']['field_sr_account'][0]['value']. By setting the field to a #hidden type and setting #value, you will get its value in $form_state['values']['field_sr_account']. CCK will try to access the first element of that and end up with the first character of the value.
Updated: The easiest way to achieve what you need would be to do something:
function addSR_form_service_request_node_form_alter(&$form, $form_state) {
if (arg(0) == 'user' && is_numeric(arg(1))) {
$account = arg(1);
$club = 2589;
// Use this property to store the value to restore back
$form['#field_sr_account'] = $club;
$form['field_sr_account'] = array( '#type' => 'hidden','#value' => $club);
}
}
/*in your submit handler, restore the value in the proper format*/
$form_state['values']['field_sr_account'] = array('0' => array('value' => $form['#field_sr_account']));
Old Answer
One way of accomplishing what you are
trying to do is to copy the whole
$form['field_sr_account'] into
$form['#field_sr_account'] and then
provide the value through the SQL
query in the right format in the
submit handler itself.
Ok take a look at http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html#hidden versus http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html#value
It is also recommended you use value instead of hidden. You can find this info on http://api.drupal.org/api/drupal/developer--topics--forms_api.html/6
Also, type hidden is not allowed to have properties your assigning to it so this may be causing a problem. Any usage problems you may be having with the forms API should be answer in those resources as I"m still a little unclear on what you're trying to accomplish... specifically with the submit button.
Old answer:
Ok if I understand this correctly
$club is not being set correctly. If
the first result from your query is
the number your looking for then this
should work.
Try calling
<?php print_r(db_fetch_array($result)) ?>
to get a look at everything returned
from the query.
I'm a little unclear as to what is
being set incorrectly. If it's
#value inside your associated array
then the culprit must be the query.
If #value is being set correctly and
whatever your doing with it later may
be the culprit (not shown here). If
its the values in your $form_state I
don't see that your using $club here
at all.
Also, in your addSR_submit_function
you don't seem to be using the $form
variable, or using $club for anything
except for setting the message which
appears at the top of the page your on
when it's called.
I may need some further clarification
as to what exactly is going wrong.
Also, when you're calling
drupal_set_message function, are you
just doing this for debugging
purposes?
Shouldn't you check
drupal_set_message($form_state['values']['field_sr_account']);
instead of
drupal_set_message($club);
in addSR_submit_function ?
OK, just a quess: not sure what type db_result returns for your query, may be it has something to do with types conversions? So this is to make sure value is int.
'#value' => (int)$club
cinqoTimo, Out of curiosity what kind of CCK field is this? Is it a Integer, Decimal, Float? and do you have any special parameters on that field not normally on by default? What is the column type in the db?
Can you post the html output of the form. That might give a clue as to what might be going on.
Are you using any javascript to edit any values for this field?
Have you tried outputting the value results from addSR_form_service_request_node_submit hook? Any difference there.
Sorry for all the questions. Just thinking out loud as it seems you have covered most of your bases.