How to post form with WWW::Mechanize (ajax) - ajax

I posted a few days ago about posting a form by changing the page size. Can someone help with the steps. I am including the dump of the form and the code I'm using to post it. Here is the code to get the first page, which defaults to a page size of 30 players, and then from there I am going to post the form to change page size to 500.
my $mech = WWW::Mechanize->new();
my $url = "https://www.fangraphs.com/projections.aspx?pos=all&stats=bat&type=steamer&team=0&lg=all&players=0";
$mech->get($url);
print Dumper($mech->forms());
$VAR1 = bless( {
'default_charset' => 'UTF-8',
'enctype' => 'application/x-www-form-urlencoded',
'accept_charset' => 'UNKNOWN',
'action' => bless( do{\(my $o = 'https://www.fangraphs.com/projections.aspx?pos=all&stats=bat&type=steamer&team=0&lg=all&players=0')}, 'URI::https' ),
'method' => 'POST',
'attr' => {
'id' => 'form1',
'method' => 'post'
},
'inputs' => [
bless( {
'readonly' => 1,
'/' => '/',
'value_name' => '',
'value' => '',
'name' => 'RadScriptManager1_TSM',
'id' => 'RadScriptManager1_TSM',
'type' => 'hidden'
}, 'HTML::Form::TextInput' ),
bless( {
'/' => '/',
'value' => '30',
'name' => 'ProjectionBoard1$dg1$ctl00$ctl02$ctl00$PageSizeComboBox',
'readonly' => 'readonly',
'value_name' => '',
'type' => 'text',
'class' => 'rcbInput radPreventDecorate',
'id' => 'ProjectionBoard1_dg1_ctl00_ctl02_ctl00_PageSizeComboBox_Input'
}, 'HTML::Form::TextInput' ),
bless( {
'tabindex' => '-1',
'class' => 'rcbActionButton',
'type' => 'button'
}, 'HTML::Form::SubmitInput' ),
bless( {
'readonly' => 1,
'/' => '/',
'value_name' => '',
'name' => 'ProjectionBoard1_dg1_ctl00_ctl03_ctl01_PageSizeComboBox_ClientState',
'id' => 'ProjectionBoard1_dg1_ctl00_ctl03_ctl01_PageSizeComboBox_ClientState',
'type' => 'hidden'
}, 'HTML::Form::TextInput' ),
]
}, 'HTML::Form' );
I then try to submit the form with the following:
$mech->submit_form(
form_id => 'form1',
with_fields => {
name => 'ProjectionBoard1$dg1$ctl00$ctl03$ctl01$PageSizeComboBox',
id => 'ProjectionBoard1_dg1_ctl00_ctl03_ctl01_PageSizeComboBox_Input',
value => 500,
},
);
but the response I get from the script is 'There is no form with the requested fields '

try something like that:
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();
my $url = "https://www.fangraphs.com/projections.aspx?pos=all&stats=bat&type=steamer&team=0&lg=all&players=0";
$mech->get($url);
$mech->set_fields(
'RadScriptManager1_TSM' => '',
'ProjectionBoard1$dg1$ctl00$ctl02$ctl00$PageSizeComboBox' => '30'
'ProjectionBoard1_dg1_ctl00_ctl03_ctl01_PageSizeComboBox_ClientState' => ''
);
$mech->submit;
set the form fields using the syntax 'FIELD_NAME' => 'VALUE' and then submit

Related

How to add spinner to select list using ajax programmatically?

I want to add spinner to select list using ajax. I have tried below code for throbber but it's not working for select list.
$form['select choice'] = [
'#type' => 'select',
'#title' => t('Choice'),
'#attributes' => [
'class' => ['js-dop-chosen'],
],
'#options' => [
'---' => t('None'),
t('choice 1'),
t('choice 2'),
],
'#ajax' => [
'event' => 'change',
'wrapper' => 'form-wrapper',
'callback' => [$this, 'ajaxCallback'],
'progress' => [
'type' => 'throbber',
'message' => 'fetching content.....',
],
],
];

Yii2: Remove controller from URL

I am using the advanced template.
I created all my actions on the SiteController, so all my urls are domain.com/site/something, and I need to remove the word "site" from the url so it will be domain.com/something.
I tried the following rules based on this question
'urlManager' => [
'class' => 'yii\web\UrlManager',
'showScriptName' => false,
'enablePrettyUrl' => true,
'rules' => array(
'/<action:\w+>/<id:\d+>' => 'site/<action>',
'/<action:\w+>' => 'site/<action>',
'/noticia/<slug>' => 'site/noticia',
),
],
also tried this based on this other question:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'showScriptName' => false,
'enablePrettyUrl' => true,
'baseUrl' => 'http://localhost/websites/transcita/app/frontend/web',
'rules' => array(
[
'pattern' => '<action:\w+>',
'route' => 'site/<action>'
],
[
'pattern' => '<action:\w+>/<id:\d+>',
'route' => 'site/<action>'
],
'/noticia/<slug>' => 'site/noticia',
),
],
but neither is not working. I get a 404 when I type domain.com/something.
I also tried without the first / and it didn't work either.
Any thoughts?
Another way:
'rules' => [
'<alias:\w+>' => 'site/<alias>',
],
Try with:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => [
// ...
// last rule
'<action:(.*)>' => 'site/<action>',
],
],

How to configure an entity as part of the second-level cache in DoctrineORMModule?

I was trying to configure memcached in DoctrineORM module of my Zendframework 2 project to perform caching queries, results and metadata follow this tutorial https://github.com/doctrine/DoctrineORMModule/blob/master/docs/cache.md
In module.config.php
<?php
return array(
'service_manager' => array(
'invokables' => array(
// ...
),
'factories' => array(
'doctrine.cache.my_memcached' => function ($sm) {
$cache = new \Doctrine\Common\Cache\MemcachedCache();
$memcache = new Memcached();
$memcache->addServer('localhost', 11211);
$cache->setMemcached($memcache);
return $cache;
},
'MemcachedFactory' => \Application\Service\Cache\MemcachedFactory::class,
)
),
'doctrine' => array(
'cache' => array(
'memcached' => array(
'namespace' => '_Doctrine',
'instance' => 'MemcachedFactory',
),
),
'configuration' => array(
'orm_default' => array(
'query_cache' => 'memcached',
'result_cache' => 'memcached',
'metadata_cache' => 'memcached',
'hydration_cache' => 'memcached',
'second_level_cache' => [
'enabled' => true,
'default_lifetime' => 200,
'default_lock_lifetime' => 500,
'file_lock_region_directory' => './data/cache',
'regions' => [
'My\FirstRegion\Name' => [
'lifetime' => 800,
'lock_lifetime' => 1000
],
'My\SecondRegion\Name' => [
'lifetime' => 10,
'lock_lifetime' => 20
],
],
],
),
),
'driver' => array(
'_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'my_memcached',
'paths' => array(__DIR__ . '/../src/Application/Entity')
),
'orm_default' => array(
'drivers' => array(
'Application\Entity' => '_driver'
),
),
),
),
);
and after I create a method as the following:
public function findById($id) {
$qb = $this->entityManager->createQueryBuilder();
$qb->select("u")
->from(User::class, "u")
->where($qb->expr()->eq("u.id", ":id"))
->setParameter("id", (int) $id);
return $qb->getQuery()->setCacheable(true)->getOneOrNullResult();
}
and after, I get a Doctrine\ORM\Cache\CacheException
with message:
Entity "Application\Entity\User" not configured as part of the second-level cache.
Anyone who can suggest for me to solved this problem?
Add this to the USER class:
* #ORM\Cache(usage="NONSTRICT_READ_WRITE")

How to always show single validation message in ZF2 validators?

I have the following input:
private function addBirthdayElement()
{
return $this->add(
array(
'type' => 'DateSelect',
'name' => 'x_bdate',
'options' => [
'label' => 'astropay_birthday',
'label_attributes' => array(
'class' => 'astropay-label'
),
'create_empty_option' => true,
'render_delimiters' => false,
],
'attributes' => array(
'required' => true,
'class' => 'astropay-input',
)
)
);
}
It has the following filter:
public function addBirthdayFilter()
{
$time = new \DateTime('now');
$eighteenYearAgo = $time->modify(sprintf('-%d year', self::EIGHTEEN_YEARS))->format('Y-m-d');
$this->add(
[
'name' => 'x_bdate',
'required' => true,
'validators' => [
[
'name' => 'Between',
'break_chain_on_failure' => true,
'options' => [
'min' => 1900,
'max' => $eighteenYearAgo,
'messages' => [
Between::NOT_BETWEEN => 'astropay_invalid_birth_date_18',
Between::NOT_BETWEEN_STRICT => 'astropay_invalid_birth_date_18',
]
]
],
[
'name' => 'Date',
'break_chain_on_failure' => true,
'options' => [
'messages' => [
Date::INVALID => 'astropay_invalid_birth_date',
Date::FALSEFORMAT => 'astropay_invalid_birth_date',
Date::INVALID_DATE => 'astropay_invalid_birth_date',
],
]
],
],
]
);
return $this;
}
However, putting an empty date, I get the error message defined for:
Date::INVALID_DATE
But it's not the overridden one. The break_chain_on_failure works for the two validators I have defined, but the default Zend message is always there. For example I get this as an error in my form:
The input does not appear to be a valid date
astropay_invalid_birth_date_18
How can I display only the overidden error messages and 1 at a time?
You can use a message key in your validator configuration instead of a messages array to always show a single message per validator.
For example, replace this:
'options' => [
'messages' => [
Date::INVALID => 'astropay_invalid_birth_date',
Date::FALSEFORMAT => 'astropay_invalid_birth_date',
Date::INVALID_DATE => 'astropay_invalid_birth_date',
],
]
with this one:
'options' => [
'message' => 'Invalid birth date given!',
]

Elastica filter not working

I am testing out Elastica and Elastic Search. I am trying to add a filter to my query that only returns results by city location. It is returning empty. I've tried filter by username, and so on and it always returns empty, so it would seem my understanding isn't quite correct. Here's my code to analyse, map, then search with a filter
$elasticaIndex = $elasticaClient->getIndex('users');
// Create the index new
$elasticaIndex->create(
array(
'analysis' => array(
'analyzer' => array(
'indexAnalyzer' => array(
'type' => 'custom',
'tokenizer' => 'standard',
'filter' => array('lowercase', 'lb_ngram')
),
'searchAnalyzer' => array(
'type' => 'custom',
'tokenizer' => 'standard',
'filter' => array('standard', 'lowercase', 'lb_ngram')
)
),
'filter' => array(
'lb_ngram' => array(
"max_gram" => 10,
"min_gram" => 1,
"type" => "nGram"
)
)
)
), true
);
//Create a type
$elasticaType = $elasticaIndex->getType('profile');
// Set mapping
$mapping->setProperties(array(
//'id' => array('type' => 'integer', 'include_in_all' => FALSE),
'firstName' => array('type' => 'string', 'include_in_all' => TRUE),
'lastName' => array('type' => 'string', 'include_in_all' => TRUE),
'username' => array('type' => 'string', 'include_in_all' => TRUE),
'bio' => array('type' => 'string', 'include_in_all' => TRUE),
'thumbnail' => array('type' => 'string', 'include_in_all' => FALSE),
'location' => array('type' => 'string', 'include_in_all' => TRUE),
));
..... Then to search, I do the following
$elasticaQueryString = new Elastica\Query\QueryString();
//'And' or 'Or' default : 'Or'
$elasticaQueryString->setDefaultOperator('AND');
$elasticaQueryString->setQuery($term);
// Create the actual search object with some data.
$elasticaQuery = new Elastica\Query();
$elasticaQuery->setQuery($elasticaQueryString);
To add a filter
$elasticaFilterLocation = new \Elastica\Filter\Term();
//search 'location' = $region;
$elasticaFilterLocation->setTerm('location', $region);
$elasticaQuery->setFilter($elasticaFilterLocation);
$elasticaResultSet = $elasticaIndex->search($elasticaQuery);
$elasticaResults = $elasticaResultSet->getResults();
If I comment out the filter, I do get the expected results. What am I missing? Does it have to do with the analyzer or mapping?
I had the same issue, I fixed it by changing my analyzers for these fields in the mappings. Now I have to rebuild my indexes etc... Don't try to find a solution to change mapping or analyzer without rebuilding your index, you can't.
lc_analyzer' => array(
'type' => 'custom',
'tokenizer' => 'keyword',
'filter' => array('lowercase')
),
For mapping:
'location' => array('type' => 'string', 'analyzer' => 'lc_analyzer'),
Then, query like
$elasticaFilterBool = new \Elastica\Filter\Bool();
$filter1 = new \Elastica\Filter\Term();
$filter1->setTerm('location', array(strtolower($region)));
$elasticaFilterBool->addMust($filter1);
$elasticaQuery->setFilter($elasticaFilterBool);
I think your location is being tokenized on spaces and commas so it is killing the search. should solve it for you.

Resources