How to select a option from drop down using select_list comment in capybara page-object - pageobjects

For HTML like this:
<select class="intl_drop" name="select_locale">
<option value="0">- Select One -</option>
<option value="1">United States - English</option>
<option value="2">United States - Español</option>
<option value="3">Canada - English</option>
when I try to select the option using
select_list(:select_lang, :class => "intl_drop", :index => 1)
I'm getting the error message
Unable to locate element{"method":"xpath","selector":".//select[#class=' intl_drop'][2]"} (Selenium::WebDriver::Error::NoSuchElementError)
It works fine when I do not use the index value.

If I understood the question the problem is that this works:
select_list(:select_lang, :class => "intl_drop")
but this does not work:
select_list(:select_lang, :class => "intl_drop", :index => 1)
The problem could be that there is only one select list with class intl_drop.
:index => 1 will select the second element. :index => 0 will select the first one.

Why not:
select_list(:select_lang, name="select_locale")
and based off of what you're statement you want to select the object by index or value?
select_lang_element.select_value(1)
or
select_lang_element[1].click
these both work for me. A long winded way that is deprecated is
select_lang_element.option[:value, '1'].click
hope this helps.

Related

Laravel 5, Form::select - Dropdown select 'other'

im working on a Laravel 5 Site and implemented a Form with a DropDown menu. All works fine but the user should be able to select the option "Other" and write his own Text.
I've googled and found some good ideas but i can't implement it.
here's what i got:
<div class="form-group">
{!! Form::label('special_status', 'Erasmus, Nebenhöhrer:') !!}
{!! Form::select('special_status',$status, null, ['id' => 'special_status', 'class' => 'form-control', 'dropdown-menu']) !!}
</div>
with the
$status = array (
'Erasmus' => 'Erasmus',
'NebenhörerIn'=> 'NebenhörerIn',
'Sonstiges' => 'Sonstiges',
);
An example how it should be:
http://jsfiddle.net/CxV9c/4/
And here's what i find on StOverf:
Laravel 4 - Assign OnChange to Form::select
Running Laravel 5.2.22 - Homestead
Includes:
bootstrap.min.css
jquery.min.js
bootstrap.min.js
Thx,
Pat
Actually, you can just put a script tag in your blade view, for example:
<script>
$(function(){
$("input[type=text]").hide();
$('#visit').on('change', function () {
var v = this.value == 4 ? 'show' : 'hide';
$.fn[v].call($("input[type=text]"));
});
});
</script>
Just put this snippet at the bottom of your blade view. Also, make sure you've added the jQuery library vefore this script runs. This is the simplest way but there are other ways to add one or more script tags in a blade view. A working example here.

Symfony Forms - entity field with query_builder, no choices_as_values?

I am trying to achieve something very simple but not sure if this is supported, possible or unexpected behaviour. I have a Symfony entity field which loads some data based on the selection of another field. The data is loaded ok but I want the option name and value to be the same. At the moment, it is populating the names ok, but I want the values to be the same as the names (the choices_as_values option in a Symfony choice field). Is that possible in an entity field.
Here is an example code:
$builder
->addEventListener(
Form\FormEvents::POST_SET_DATA,
function (Form\FormEvent $event) {
$attributeData = $event->getData();
$event->getForm()->add(
'group',
'entity',
array(
'class' => 'AppBundle:CategoryAttributeData',
'placeholder' => 'Select a data group',
'label' => 'Attribute Group',
'choice_label' => 'content',
'required' => false,
'query_builder' => function (Repository\CategoryAttributeData $repository) use ($attributeData) {
$queryBuilder = $repository->createQueryBuilder('u')
->select('u')
->where('u.type = :type')
->andWhere('u.group IS NULL')
->setParameter('type', $attributeData->getType())
;
return $queryBuilder;
}
)
);
}
)
;
The output is:
<select id="attribute_data_group" name="attribute_data[group]" class="form-control">
<option value="">Select a data group</option>
<option value="1">Cars</option>
<option value="2">Electronics</option>
<option value="3">Furniture</option>
</select>
What I am trying to achieve is:
<select id="attribute_data_group" name="attribute_data[group]" class="form-control">
<option value="">Select a data group</option>
<option value="Cars">Cars</option>
<option value="Electronics">Electronics</option>
<option value="Furniture">Furniture</option>
</select>
Since this field is populated via an event listener (because it depends on another field value) I cannot add a view transformer in here.
Any suggestions?
This is actually more complicated than it would seem on the surface. The Entity form type assumes that the "values" of the choice selector are the unique identifier for an entity, this allows the form type (and its associated transformers) to find and transform the passed values from and to the relevant entities. So the first question is - can you uniquely identify your entity by the string "Cars", or "Electronics"? If you can't, then the next question is how were you planning on converting that string into the entity?
If you can, then things are relatively easier. Effectively you need to provide a different ChoiceList implementation for the choice field type - this is effectively what the Entity type already does. I'm not familiar with the correct way to do this and I believe the method changed between Symfony 2.6 and 2.7, but I would look into classes like Symfony\Bridge\Doctrine\Form\Type\EntityType as well as the base Symfony\Component\Form\Extension\Core\Type\ChoiceType.
Short version: it's not wholly straightforward but definitely possible.
It can be done with the option 'choice_value', for example:
'choice_value' => function (?Team $team) {
return $team ? $team->getBesoccerId() : '';
}
It's 100% compatible with the 'query_builder' option. Continuing the same example:
->add('team', EntityType::class, array(
'class' => Team::class,
'label' => 'Equipo',
'query_builder' => function (EntityRepository $er) use ($lang) {
return $er->createQueryBuilder('t')
->andWhere('t.besoccerId IS NOT NULL')
->andWhere('i18n.cod_lang = :cod_lang')
->setParameter('cod_lang', $lang)
->leftJoin('t.translations', 'i18n')
->orderBy('t.sort', 'ASC');
},
'choice_label' => 'name',
'choice_value' => function (?Team $team) {
return $team ? $team->getBesoccerId() : '';
}
))
You can find more information in the documentation.

Retrieving values of Form::select

I have a select box with an array of data to populate it like this:
{!! Form::select('language',$languageArray,'null', ['id'=>'language','multiple',]) !!}
I am passing $languageArray with view , and is simply an array of values like ['A','B','C']...
Now while fetching the selected values i am getting numeric value of selected options. Is there a way to change the values from numeric indexes to Text written in option. i did it using an associative array as second argument like this:
['english' => 'English',
'french' => 'French',
'so on ...' => 'So On ..']
But it creates a long list and view seems to be overloaded with data is there a better way for achieving below output ???
<select name="language">
<option value="english" ">English</option>
<option value="french">French</option>
....
I suggest you to use Config values ,
Create a file like languages.php in config folder.
<?php
return ['english' => 'English',
'french' => 'French',
'so on ...' => 'So On ..'
'german' => Lang::get('languages.german')
];
View :
{!! Form::select('language',Config::get('languages'),'null', ['id'=>'language','multiple',]) !!}
As you can see, in this way you can use config values in another view too and support multi language(its important too.. look at 'german')
Last Option is creating your own Form macro for example,
Form::macro('slugiySelect', function($name , $list , $selected = null , $options = [])
{
$options = [];
foreach($list as $row)
$options[Str::slug($row)] = $row;
return Form::select($name , $options , $selected , $options);
});
In this macro your options array is slugify with Laravel string function and uses as a key , I kept value as yours. You can do it with your way.

Codeigniter PHP getting option value

I've got a complicated re-population system that works, but it's cumbersome.
I've got a dropdown menu for a country of birth, and when the users submit the form it gets thrown in a database. The dropdown menu then gets repopulated with whatever is in the database if there is something.
I get the value from the database like this:
$birthplacecountry = (!set_select('birthplacecountry') && $birth_citizenship) ? $birth_citizenship[0]['birthplacecountry'] : (set_select('birthplacecountry') ? set_select('birthplacecountry') : '');
That works fine, as I've then got my options looking like this:
<option value="Afganistan" <?php if ($birthplacecountry == 'Afganistan') echo 'selected="selected"';?>>Afghanistan</option>
<option value="Albania" <?php if ($birthplacecountry == 'Albania') echo 'selected="selected"';?>>Albania</option>
<option value="Algeria" <?php if ($birthplacecountry == 'Algeria') echo 'selected="selected"';?>>Algeria</option>
<option value="American Samoa" <?php if ($birthplacecountry == 'American Samoa') echo 'selected="selected"';?>>American Samoa</option>
However, it's annoying having to compare $birthplacecountry to the name of the value manually. Is there a general way to compare something to the options value?
Some sort of functionality that works like: if ($birthplacecountry == this.option.value)
In codeIgniter, you should use Form helper to produce a select/dropdown, i.e.
$options = array(
'Afganistan' => 'Afganistan',
'Albania' => 'Albania',
);
echo form_dropdown('birthplacecountry', $options, 'Albania');
In this example, a dropdown with name birthplacecountry will be created and selected option will be Albania, the seconf one. The third parameter sets the selected option, so, when you want to repopulate the form with selected option, you can do something like this
echo form_dropdown('birthplacecountry', $options, set_select('birthplacecountry', $this->input->post('birthplacecountry') );

sfWidgetFormDoctrineChoice adding extra choice field

I'm using sfWidgetFormDoctrineChoice and I'm reading options from table. Is it easy way to add one option and place as first which is not in this table?
By option I mean html option:
<select>
<option val="new"></option>
</select>
You could always localize the choices, then prepend the resulting array with the value you would like to use:
$choice = new sfWidgetFormDoctrineChoice(array('model' => 'MODEL', 'order_by' => array('name', 'asc')));
$choices = $choice->getChoices();
array_unshift($choices, array('key' => 'My Custom Value'));
$this->widgetSchema['widget_name'] = new sfWidgetFormChoice(array('choices' => $choices));
$this->validatorSchema['widget_name'] = new sfValidatorChoice(array('choices' => array_keys($choices));