Smarty Variable - Hyphen in Array Key - smarty

Trying to display a Smarty variable with a hyphen in the key. Nothing I can do to change the fact that it has a hyphen in the key.
For example, a phone number may be stored within the $form array as:
phone-1-1 => Array (9)
name => "phone-1-1"
value => "(555) 555-5555"
type => "text"
frozen => false
required => false
error => null
id => "phone-1-1"
label => "<label for="phone-1-1">Phone Number (..."
html => "<input maxlength="32" size="20" name=..."
Trying to print the smarty variable using:
{$form.phone-1-1.label}
fails because of the hyphens.
Any ideas how I get around that?

The only workaround you can use is:
{assign var="mykey" value="phone-1-1"}
{$form.$mykey.label}
The bult-in Smarty function {assign} let you create variables directly in the template.
http://www.smarty.net/docs/en/language.function.assign.tpl (for Smarty 3)
http://www.smarty.net/docsv2/en/language.custom.functions.tpl (for Smarty 2)

Related

How to correcly use HTML tags in translation Laravel?

I have a code <span>{{ trans('lang.color.' . $bet->color) }}</span></div> which displays the bet amount for a specific color.
My lang file:
'color' => [
'red' => 'red',
'zero' => 'green',
'black' => 'black',
],
Which is responsible for the fact that if the bet was placed on red, the site will say: set to red.
How can I correctly display HTML code in color variables? For example, if i write 'red' => '<div style="font-color:#FF0000">red</div>' site does not convert the text to HTML, and writes the div with text. How to make read text file HTML?
My laravel version: 5.1.10.
Displaying Unescaped Data
By default, Blade {{ }} statements are automatically sent through PHP's htmlentities function to prevent XSS attacks. If you do not want your data to be escaped, you may use the following syntax:
{!! trans('lang.color.' . $bet->color) !!}
Note: Be very careful when echoing content that is supplied by users of your application. Always use the double curly brace syntax to escape any HTML entities in the content.

How to locate, using Xpath, an input field without using its id

I'm working with a web page with the following HTML where I want to identify the first <input> field inside the <span> using a text_field from within a page-object.
<div id="131:"> Please enter your name:
<span class="autocompspan " style="position:static;">
<input style="position: static;" class="autocompinput yui-ac-input" id="132:" name="132:"
onfocus="juic.fire("132:","_focus",event);"
onchange="juic.fire("132:","_despatchChange",event);"
onblur="juic.fire("132:","_blur",event);" size="60"
onkeydown="juic.fire("132:","_onkeydown",event);"
onkeyup="juic.fire("132:","_onkeyup",event);" aria-disabled="false" value=""
role="combobox" aria-autocomplete="list" aria-owns="132:_divList"
aria-activedescendant="132:_divAD" findtype="proxy" delimchar="" hideusername="false"
fetchusername="false" autocomplete="off" type="text">
<input value="" id="132:_hidden" name="132:_hidden" type="hidden">
</span>
</div>
If I use :id => '132:' to identify the field things work fine. I.e. text_field(:target_user_name, :id => '132:' ) works.
The issue is that this HTML is generated by the underlying app (SAP) which does not always generated the same value for the <input> field id so using the id cannot be relied upon to consistently identify the element.
So, given the above HTML what other ways might I go about reliably finding this <input> field.
I've tried the following, none of which work. Most of them time out waiting for the element to be located.
text_field(:target_user_name, :xpath => "//*[#class='autocompinput yui-ac-input' and #role = 'combobox']" )
text_field(:target_user_name, :xpath => "//*[#class='autocompinput' and #role = 'combobox']" )
text_field(:target_user_name, :xpath => "//span/input[#class='autocompinput yui-ac-input' and #role = 'combobox']" )
text_field(:target_user_name, :xpath => "//input[#class='autocompinput yui-ac-input' and #role = 'combobox']" )
text_field(:target_user_name, :class => 'autocompinput yui-ac-input')
Any thoughts?
When an element does not have unique identifiable attributes, you should look at the elements around it. In this case, there is user visible text that helps a user identify the purpose of the field. That same text can be used to identify the element in Watir.
As the surrounding div only contains the labelling text, you can search for that div by its text and get the only text field in it:
browser.div(text: 'Please enter your name:').text_field
As a page-object accessor:
text_field(:target_user_name) { div_element(text: 'Please enter your name:').text_field_element }
Firstly, Watir is designed to make it so that you shouldn't have to use XPATH.
It depends on how many different elements/ids are on the page, but I've found that using regular expressions often works well with dynamically generated ids. So either grab the id and use it elsewhere:
id = browser.text_field(id: /\d\d\d/).tr(':', '')
or just use it directly:
text_field(:target_user_name, id: /\d\d\d:/)
In this particular case you can check the first input field after 'Please enter your name:' text using below xpath:
//div[text()='Please enter your name:']//following::input[1]
In general if you encounter fields that does not have a unique identifier you can rely on static text or fields and then use xpath functions such as following, preceding etc.

How to find a field using placeholder text in Watir?

I have two search fields and I want to find the second one to set some text.
I have tried using div however it always finds the first search field.
Does anyone one have a suggestion on how to find the second object or use the unique placeholder text "Search..." ?
HTML:
input type="text" name="searchString" id="searchString" projects="" for="" placeholder="Search" class="form-control"
input type="text" name="searchString" id="searchString" placeholder="Search..." class="form-control"
Ruby - Watir:
#b.link(:text => "http://ml-test.mytest.com/Client/Profile/ab295b41-2c5e-4100-bdee-e757405238bb").click
#b.text_field{div(:class => "col-sm-4 col-md-3", :placeholder => "Search...")}.set "Automation (Test)"
It looks like the problem might simply be a typo in the code. Notice in the line:
#b.text_field{div(:class => "col-sm-4 col-md-3", :placeholder => "Search...")}.set "Automation (Test)"
That a block, seen by the {}, is being passed to the text_field method. The text_field method does not do anything with blocks, as a result the code is really just doing:
#b.text_field.set "Automation (Test)"
As there is no locator supplied to the text field, it will input the first text field on the page.
To locate the text field based on the placeholder attribute (or any other locator), it needs to be passed as an argument instead of a block:
#b.text_field(:placeholder => "Search...").set "Automation (Test)"
You had included a div as well. Assuming that it is an ancestor element need to find the text field, it should be:
#b.div(:class => "col-sm-4 col-md-3").text_field(:placeholder => "Search...").set "Automation (Test)"

Unexpected subpatterns captured by preg_match

I am doing some regular expressions in php and matching using the preg_match();
I have a text that might look something like this:
$imy = "...without sophisticated apparatus<div class="caption"><div class="caption-inner">
<img src="http://dev.mysite.org/Heatmap.png" alt="" title="" class="image-thumbnail" />
Caption text</div></div>Some more text...
<img src="http://dev.mysite.org/Heatmap.png" alt="" title="" class="image-thumbnail" />blablah..."
and my goal is to pick out either the "img" tag enclosed in the "div" tags(including the "div" tags) or just the "img" if it is not enclosed in divs. I also in each case want to capture the address contained in the src attribute of the "img" tag.
This is the pattern I use:
$imagepattern = '/<div class="caption-inner[^>]+>.*<img\b[^>]*\bsrc="([^">]*)"[^>]*>.*<\/div>(<\/div>)?|<img\b[^>]*\bsrc="([^">]*)"[^>]*>/Us';
and it works great for "div" enclosed images, but for the divless images I get weird results for the captured subpattern.
I iteratively call preg_match and remove the match from the subject string before resending it to preg_match. My call to preg_match looks like this:
preg_match($imagepattern,$imy,$image,PREG_OFFSET_CAPTURE)
What I get in my image array when matching against a divless image tag looks like this:
$image = [0] => Array
(
[0] => <img src="http://dev.molmeth.org/Heatmap.png" alt="" title="" class="image-thumbnail" />
[1] => 1
)
[1] => Array
(
[0] =>
[1] => -1
)
[2] => Array
(
[0] =>
[1] => -1
)
[3] => Array
(
[0] => http://dev.mysite.org/Heatmap.png
[1] => 11
)
How can the $image array have the '2' and '3' keys? Don't I only have one subpattern? Is this somehow because of the 'or' condition in the pattern?
in your preg_match expression you have 3 capture groups.
the whole expression matches because of the or (since you search div included images OR divless images)
for divless images, only capture group 3 will be filled data and capture groups 1 & 2 will be empty.

get form values other than by name in codeigniter

hi i am using codeigniter . i have a form , there i add hidden fields dynamically . so every hidden field is <input type='hidden' name='hidden' value="+$(this).attr('title')+"> so the name is equal .
the problem is when i submit the form and try to get my hiden field values i can only get one hidden field value , because the names are same
i print my form values
print_r($this->input->post());
i have 2 hidden fields but i get only one
Array
(
[hidden] => march
[textbox] => march
[mysubmit] => Submit
)
i can change the name dynamically of hidden field when creating , but then i don't know exactly the name of my hidden field ,
how can i get hidden field values with same name ?? is there any way to get form values other than by name ?? i tried and can not find an answer , please help .............
You'll need to use brackets in your name attributes:
<input type='hidden' name='hidden[]'>
<!-- ^^^^ -->
This will allow PHP to accept multiple inputs with the same name as an array of values, so in this case, $_POST['hidden'] will return an array of strings.
By default they are indexed starting at 0, so $_POST['hidden'][0] will get you the first one, $_POST['hidden'][1] will get you the second, etc., however - you can explicitly index them if it's easier for you, either with numbers or strings.
<input type='hidden' name='hidden[first]'>
<input type='hidden' name='hidden[second]'>
Or:
<input type='hidden' name='hidden[0]'>
<input type='hidden' name='hidden[1]'>
You can nest these as deep as you want like hidden[first][1][], and they will be treated similarly to a PHP array when you get the $_POST values, but you need the brackets in the HTML.
Without brackets, only the last field's value will be available in the $_POST array. This is a PHP feature, Codeigniter can't do anything about it.

Resources