Using Codeception with form input array - php

So I am trying to use Codeception on a form where I have multiple inputs that have a name like such.
<input type="text" name="flavours[]" >
I have tried
$I->fillField('flavors[]', 'Blue Razberry');
However Codeception returns
Couldn't fill field "flavors[]","Blue Razberry":
InvalidArgumentException: Unreachable field "" unable to find a field with that name.
Is it possible in Codeception? If not, is there another way?
Thanks in advance!

You can you submitForm method instead
$I->submitForm('#name_of_your_form', array(
'field' => 'value',
'field' => 'value'
));

Use it:
$I->fillField('flavors', 'Blue Razberry');

Related

Codeigniter form validation - how to reject default values

I have a form with default values set, e.g.:
<input type="text" id="mail" name="mail" value="<?php echo set_value('mail', 'jdoe#example.com'); ?>" />
I validate the form using Codeigniter's form_validation class. For this field:
$config = array(
array(
'field' => 'user',
'label' => 'Naam',
'rules' => 'trim|required|valid_email'
));
Here's my problem. When the user leaves the field untouched and submits it's default value 'jdoe#example.com', I want to reject this value. I know I can use a callback, but that would mean I have to write a callback for every field of the form. Not nice!
I've looked into the validation rules. 'differs' looked promising, but it only checks whether one field is different from another field.
Now what's the best way to deal with default values in Codeigniter?
Sounds like you should be using a placeholder instead of a default value. Otherwise what you are asking for doesn't make much sense.
<input type="text" id="mail" name="mail" value="<?php echo set_value('mail'); ?>" placeholder="jdoe#example.com" />
Otherwise you could try the built in validation rule "in_list" e.g. in_list[red,blue,green]
$config = array(
array(
'field' => 'user',
'label' => 'Naam',
'rules' => 'trim|required|valid_email|in_list[jdoe#example.com]'
));
EDIT: Actually you would want the opposite of the in_list rule, i think that would require a callback, but you would only need to write one callback to use for each field.

Symfony2 add extra selectbox

I added extra select box (choice type) and mapped=>false see the code below.
However when I submit the form. It returned the error msg "This value is not valid."
$form->add('extraField' ,'choice', array(
'required' => false,
'choices' => $arrayChoices,
'mapped'=>false,
'data' =>$id
));
What did I do wrong here?
Ok, after this doc : http://symfony.com/doc/current/reference/forms/types/choice.html#choices
I don't see 'data' option. Try to remove it please. (and , what is the use of this field [data]? )
Are you sure that $id is valid array key for $arrayChoices?
Also, when you submit the form, the request must contain a valid key inside of $arrayChoices

How do I set the 'value' in my Zend form checkbox?

The Zend Form is proving to be a bit tricky for me, even as much as I am working with it lately...
I have this form and I am attempting to dynamically create the several checkboxes for it. It is working out alright, except I cannot seem to get the 'value' attribute to change.
In my Zend form class I have this snippet...
// psychotic symptoms
$this->addElement('checkbox', 'psychoticsymptom', array(
'label' => 'psychoticsymptom',
'name' => 'psychoticsymptom',
));
In my view (phtml) I am calling it like this...
<div class="element">
<?php // Psychotic Symptoms
$Criteria = new Criteria();
$Criteria->add( DictionaryPeer::CATEGORY, 'MAR: Psychotic Symptoms' );
$Criteria->addAscendingOrderByColumn( 'Ordinal' );
$this->PsychoticSymptomsList = DictionaryPeer::doSelect( $Criteria );
foreach( $this->PsychoticSymptomsList as $Symptom ) {
$form->psychoticsymptom->setValue($Symptom->getDictionaryId());
$form->psychoticsymptom->setAttrib('name', $Symptom->getWord());
echo $Symptom->getDictionaryId(); // prove my id is coming through... (it is)
$form->psychoticsymptom->getDecorator('label')->setTag(null);
echo $form->psychoticsymptom->renderViewHelper();
$form->psychoticsymptom->setLabel($Symptom->getWord());
echo $form->psychoticsymptom->renderLabel();
echo '<br />';
}
?>
</div>
Everything seems to be working fine, except the value attribute on each checkbox is rendering a value of '1'. I have tried moving the 'setValue' line to several different positions, as to set the value before the form element renders but I am having no luck getting this to work. It's worth any effort to me because I need to do the same type of operation in many areas of my application. I would have done this a bit different too, but I am re-factoring another application and am trying to keep some things unchanged (like the database for instance).
Any help is much appriciated
Thanks
you can try to overwrite the "checkedValue" and "uncheckedValue". check this reference
$this->addElement('checkbox', 'psychoticsymptom', array(
'label' => 'psychoticsymptom',
'name' => 'psychoticsymptom',
'checkedValue' => 'checked Value',
'uncheckedValue' => 'unchecked Value'
));
You seem to only have one psychoticsymptom element "checkbox" which your adding (changing) the value too for each $this->PsychoticSymptomsList.
Maybe you would be better off using a multicheckbox element instead.

CodeIgniter: Validate form with multidimensional POST data

So the framework is CodeIgniter 2.0.2. I have a form that has groups of fields that correspond to rows in a database. The names of the fields are in the format:
opt[0][foo]
opt[0][bar]
opt[1][foo]
opt[1][bar]
etc...
The index (1,2,etc...) does not correspond to row IDs in the database, it is simply a way to split up the groups of fields. There may be gaps in the index as users are able to add and remove an arbitrary number of the field groups. All groups are identical, that is, they contain exactly the same set of fields with the same second level names.
I want to be able to use CodeIgniter's validation library to validate the form and (p)re-populate as necessary. I've found plenty of posts (in addition to the excellent CI user guide) on the pre-populating and I know how to get the working with the re-populating in general. However, this is the first time I've had to try it with the indexed field names as above. I've tried the below and it doesn't work:
array(
'field' => 'opt[][foo]',
'label' => 'Foo',
'rules' => 'required'
)
I'm guessing I was just hoping for too much and CodeIgniter doesn't support what I need it to do. Extending the existing form validation library is an option so if anyone has been in the same situation and can provide some tips that would be very welcome.
UPDATE:
Just a little extra info, I've also tried validating a specifically indexed field (see below) and that also didn't work... As I understand it multidimensional validation should work in the specific case:
array(
'field' => 'opt[0][foo]',
'label' => 'Foo',
'rules' => 'required'
)
The following controller code works for me on CI 2.0.2
public function test() {
$this->load->library('form_validation');
$this->load->helper('form');
$this->form_validation->set_rules('test[test1][test2]', 'Test', 'required|valid_email');
$this->form_validation->run();
echo validation_errors();
echo form_open($this->uri->uri_string());
echo form_input('test[test1][test2]', set_value('test[test1][test2]'));
echo form_submit();
echo form_close();
}
You can use this to loop through the opt variable and set validation rules for each input.
if(!empty($opt))
{
foreach($opt as $id => $value)
{
$this->form_validation->set_rules('opt[' . $id . '][foo]', 'Foo', 'required');
$this->form_validation->set_rules('opt[' . $id . '][bar]', 'Bar', 'required');
}
}
You should take a look at the callback functions for the validating class - this should help you accomplish what you need for validation.

How to Zend_Dojo_Form_Element_FilteringSelect onchange submit

Well the title pretty much says it all. I had:
$strata = new Zend_Form_Element_Select('strata');
$strata->setLabel('Select a strata: ')->setMultiOptions($this->stratalist)->setAttrib('onChange', 'this.form.submit()');
Then I need to use some fancy dojo form elements in other forms. So I decided to make them all look the same and did this:
$strata = new Zend_Dojo_Form_Element_FilteringSelect('strata');
$strata->setLabel('Select a strata: ')->setMultiOptions($this->stratalist)->setAttrib('onChange', 'this.form.submit()');
It shows up and looks fine, but the form is not submitted when I change the FilteringSelect. If I look at the HTML that is rendered, sure enough:
<select name="strata" id="strata" onChange="this.form.submit()">
I suspect that Dojo elements cannot or do not work like this. So how do I make this form submit when I change the FilteringSelect?
Here it is:
When defining the form, give it an id:
$this->setName('StrataSelect');
or
$this->setAttrib('id', 'StrataSelect');
Then the onChange event uses getElementById:
$strata = new Zend_Dojo_Form_Element_FilteringSelect('strata');
$strata->setLabel('Select a strata: ')->setMultiOptions($this->stratalist)->setAttrib('onChange', "document.dojo.byId('StrataSelect').submit();");
or
$strata->setLabel('Select a strata: ')->setMultiOptions($this->stratalist)->setAttrib('onChange', "document.getElementById('StrataSelect').submit();");
Why this now works and none of the "old school" submit() calls probably has something to do with dojo handling the onchange event. So submit or this.form are not objects, methods, etc etc etc.
I don't want to put any javascript this form depends on into the view. I want this form to be "portable". So therefore I don't want to use dojo.connect
There are probably better ways to do this. So I'll leave this unanswered for now.
Do you have parseOnLoad enabled? If you're building the form in php you can do this:
$form = new Zend_Form_Dojo();
$form->addElement(
'FilteringSelect',
'myId',
array(
'label' => 'Prerequisite:',
'autocomplete' => true,
'jsId' => 'myJsId',
),
array(), //attributes
array( //your select values
'id1' => 'name1',
'id2' => 'name2',
'id3' => 'name3',
)
);
you might need to set a few attributes on your $form.
try this:
$form->setAttribs( array('jsId'=>'MyFormName') );
Then in your onClick:
MyFormName.submit()
If your form passes validation (presuming you have some), it should submit.

Categories