I'm testing a form through the SubmitForm() function because the form uses javascript to cycle through each individual item.
example:
$I->submitForm('#form', array(
'feet' => '1',
'inches' => '2',
), 'submit');
This works fine but I'm having trouble with a file upload input.
$I->submitForm('#form', array(
'feet' => '1',
'inches' => '2',
'file' => ???
), 'submit');
I tried sending an array to mimic the $_FILES array but that obviously isn't the right way to do it. Is this possible?
I've encountered this issue as well and the only way I can see around it is to manually fill the fields and the click the submit button.
For example
$I->fillField(['name' => 'name'], 'Test');
$I->attachFile('input[name=photo]', 'test.jpg');
$I->click('#formId button[type=submit]');
$I->seeCurrentRouteIs('route.index');
$I->see('Model has been updated.');
You can store any test files in the Codeception tests/_data folder.
This does work, but sadly doesn't help me in my current situation as I have a form which dynamically populates various select elements so I need to submitForm as I can't manually selectOption as the options are populated depending on other form completions.
I realise this is old and already marked as answer but it doesn't answer the specific question, which is still a problem for #alexleonard, who posted the accepted answer.
You can user attachFile in conjunction with submitForm. You just have to call is first. For example:
$I->attachFile('#form input[type=file]', <pathtofile> );
$I->submitForm('#form', array(
'feet' => '1',
'inches' => '2',
), 'submit');
Related
I added some custom field so that i can get more information when someone registers into the website but i want to alter one field on the form. I created a module which has a hook_form_alter function so that i can alter the field_first_name of the registration form. here is the code
function user_registration_form_alter(&$form, &$form_state, $form_id){
if ($form_id == 'user_register_form'){
$form['field_first_name'] = array(
'#type' => 'textfield',
'#title' => t('First Name'),
'#description' => t('Enter your first name.'),
'#maxlength' => 255,
'#required' => TRUE,
);
}
if (isset($form['actions']['submit'])) {
$form['actions']['submit']['#submit'][] ='user_registration_form_submit';
}
}
I also created a the function which handles the form submission.
function user_registration_form_submit(&$form, &$form_state)
{
$fe_id = db_insert('field_revision_field_first_name')
->fields(array(
'field_first_name_value' => $form_state['value']['field_first_name'],
))->execute();
drupal_set_message(t('your form entry has been added'));
}
My problem is that when i submit the form and i check the user details. i find that the first name details does not exist in the database or when i login as the administrator and click on the 'people' link. i find that all information is are submitted except the first name field which i am trying to alter. I also tried to submit the form without the form submit function but it still doesn't work.
and i get the following error message if i add the form_submit function
Notice: Undefined index: value in user_registration_form_submit() (line 37 of /var/www/html/lite/sites/all/modules/user_regestration/user_registration.module).
PDOException: SQLSTATE[HY000]: General error: 1364 Field 'entity_id'
doesn't have a default value: INSERT INTO
{field_revision_field_first_name} (field_first_name_value) VALUES
(:db_insert_placeholder_0); Array ( [:db_insert_placeholder_0] => ) in
user_registration_form_submit() (line 38 of
/var/www/html/lite/sites/all/modules/user_regestration/user_registration.module).
THIS IS LINE 37 AND 38 OF MY CODE
'field_first_name_value' => $form_state['value']['field_first_name'],
))->execute();
I am creating the module on localhost first before i push it to the live website
First, it's values not value.
Why don't you use a module providing this functionality, like profile2 ?
The user is not created in your submithandler, so either you save it:
function foo_user_register_form_submit($form, &$form_state){
$edit = array(
'name' => $form_state['values']['name'],
'pass' => user_password(),
'mail' => $form_state['values']['mail'],
'init' => $form_state['values']['mail'],
'status' => 1,
'access' => REQUEST_TIME,
'field_first_name' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_first_name']))),
);
user_save(drupal_anonymous_user(), $edit);
}
For more details see the docs.
Or you could try to add a custom submit handler, which would be fired after the user is created, in form_alter:
$form['#submit'][] = 'your_custom_submit_handler_function';
In there the user should already be created. so:
function your_custom_submit_handler_function(&$form, &$form_state)
{
$fe_id = db_insert('field_revision_field_first_name')
->fields(array(
'field_first_name_value' => $form_state['values']['field_first_name'],
))->execute();
drupal_set_message(t('your form entry has been added'));
}
But keep in mind, that this custom inserting maybe needs some more
code, here you only add the revision entry ..
You're not passing in an entity_id, and your database schema isn't defining a default one. The database expects one, and so it is failing, and it is failing on insert - which is your lines 37 and 38. It has absolutely nothing to do with your first name part.
Basically, Drupal (technically MySQL, or whatever your database backend is) doesn't know what entity you're trying to associate that field with.
Please look at the table that you're actually putting the information into and ensure that the schema either has a default set
Something like:
db_insert('field_revision_field_first_name')
->fields(array(
'field_first_name_value' => $form_state['values']['field_first_name'],
'entity_id' => 1,
))->execute();
Would probably work better for you. There may be other required fields. I recommend taking a look at your database schema.
You also shouldn't have to define a custom form for this - you can add fields to users, and also there is profile2 as another poster mentioned.
I am using Symfony framework and I want to create a form without the submit button.
I explain myself better: I have a simple form, like the following one:
$form = $this->createFormBuilder($defaultData);
$form->add($fieldname, 'choice', array(
'choices' => array(
'3' => 'label_3',
'2' => 'label_2',
'1' => 'label_1',
'0' => 'label_0',
),
'expanded' => false,
'label' => 'choice',
));
$form->add('send', 'submit', array(
'label' => 'send'
));
Is there a way to avoid having a "submit" button?
The resulting behavior I want to enforce is:
- The User selects the desired choice
- On click, data is immediately submitted and handled by the controller
I searched in the Symfony manual, but I failed finding something that could fit.
Can anyone help me? (Is i even possible to have such a behavior?)
Thanks in advance.
You have to do this via JavaScript:
$('#formFieldId').change(function(){
$(this).closest('form').trigger('submit');
});
you should try with javascript like this :
jQuery().ready( function() {
// for example your fieldname id is "field"
jQuery( "#field").change( function(){
jQuery('form').submit();
});
});
you don t have to put you submit button in your form by the way, the standard way is to include the html for the button within the twig file
CHtml::form();
echo CHtml::activeDropDownList($model,'imei', $model->getCategories(),
array('prompt' => 'Select Employe',
'submit'=>'/mobitracker/index.php?r=details/pathmap',
'params'=>array('imei'=>'js: $(this).val()'),
));
CHtml::endForm();
When a user selects a item am submitting it to another page and processing there.
but now I need another data also to be sent, i.e date am using DJui datepicker widget
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'name' => 'date_from',
'value' => $fromDateValue,
'htmlOptions' => array(
'size' => '10', // textField size
'maxlength' => '10', // textField maxlength
),
));
So once a user selects a employee and then selects a date I need data to be submitted.
How can I accomplish this using yii?
Thanks in advance.
Note: Am using PHP yii framework
<inputid="date_from" type="text" name="date_from" class="hasDatepicker">
The widget just simply generates a text box with the given settings including the name. You can totally take the name and process with it as usually by $_POST. Of cause you have to put that field into your form
...
$model->attributes=$_POST['Employee']; // access your form as you should have to do already
$model->date_from = $_POST['date_from']; // access date value
I've implemented checkboxes into my jFormer form. What returns in the email is "array" and not the value that is set.
// Add components to the form
$multipleChoiceComponentForm->addJFormComponentArray(array(
new JFormComponentMultipleChoice('brochure', 'Multiple choice checkboxes:',
array(
array('label' => 'Send Me a brochure', 'value' => 'Yes'),
),
array()
),
I cannot figure out why its returning "Array" in the email
...the lack of detailed documentation is also not helping. I tried contacting the guy working on the github stuff for jformer, but no answer.
Also, whenever I add
new JFormComponentAddress('currentaddress', 'Current Address:', array(
'validationOptions' => array(),
)),
the email never process...nor does it give an error...the button just says "processing"
anyone can help?
The Docs on jFormer are up at
http://www.jformer.com/documentation/
in jFormer, if the multiplechoice component is of the type 'checkbox' it always returns an array, if you have a single checkbox, try using the key
$formValues->brochure[0]
for the jFormComponentAddress, try just submitting it without the validations options.
I need to pass the value of an element to an $ajax->link without using a form/submit structure. (because I have a dynamically set number of clickable links through which I am triggering the action)
I used to do this in Ruby using the Prototype javascript function $F like this:
<%= link_to_remote "#{item.to_s}",
:url => { :action => :add_mpc },
:with => "'category=' + $F('mpc_category')" -%>
But this does not seem to work in Cakephp:
<?php echo $ajax->link(substr($vehicle['vehicles']['year'], -2),
array('action' => 'add_mpc', 'category' => '$F("mpc_category")'),
array('update' => 'results', 'position' => 'top')); ?>
PHP sees $F as a variable instead of a call to javascript. I'm not too familiar with Javascript, but is there another way to pass the value of the 'mpc_category' input element to the controller through this link? I have been looking for a couple days and can't find anyone dealing with this specific issue. Thanks for any assistance.
Edit: fixed syntax in php statement.
haven't really used cake, but I have used rails. The js part should be entirely a string. Probably something like this:
<?php echo $ajax->link(substr($vehicle['vehicles']['year'], -2),
array('action' => 'add_mpc', 'category' => "$F('mpc_category')"),
array('update' => 'results', 'position' => 'top')); ?>
I'm assuming that it tacks "$key=$value" onto the params in the ajax link. Also note you were missing a comma at the end of that second line.
After working on this for a couple days now, the best I have come up with is this:
<?php echo $ajax->link($year,
array( 'action' => 'add_row',
'category' => $category,
'product' => $product.$newpart,
array( 'update' => $summary['summary']['id']." ".$vehicle['vehicles'],
'with' => "$('app_select').serialize()")); ?>
the 'with' => "$('app_select').serialize()" being the part that grabs the values out of the form without having to submit the form.
I only needed one of the form elements, however, so this is not ideal as it passes the entire serialized form to the controller.
I would still like to be able to do this with any element, regardless of it is in a form or not, but this method doesn't seem to do that. Perhaps someone more familiar with prototype could shed some light on that.