Why does Perl's CGI::FormBuilder complain about 'No options specified for select'? - perl

I get this error from my CGI script:
my_circle.pl: [FormBuilder] Warning: metro: No options specified for 'select' field at /home/ecoopr/ecoopr.com/CPAN/CGI/FormBuilder.pm line 1407, referer: http://kkarnam.ecoopr.dyndns.org:880/home.pl
Can you suggest me what might be the problem?

As the error message says, you are probably trying to construct a select form widget without specifying any options.
Find out what is triggering that part of CGI::FormBuilder. You can use something like Carp::Always to turn all errors and warnings into stack traces so you can work back to the line of code that started the problem.
The relevant code is the anonymous hash construction in the prepare method, which looks like its expecting you to provide some options for select:
1406 # Create a struct for each field
1407 $tmplvar{field}{"$field"} = {
1408 %$field, # gets invalid/missing/required
1409 field => $field->tag,
1410 value => $value[0],
1411 values => \#value,
1412 options => [$field->options],
1413 label => $field->label,
1414 type => $field->type,
1415 comment => $field->comment,
1416 nameopts => $field->nameopts,
1417 cleanopts => $field->cleanopts,
1418 };

Make sure there are some options defined for selects.
For example, consider this form field definition:
$form->field(
name => 'dept_id',
label => 'Dept',
type => 'select',
options => \#dept_options,
required => 1,
);
If the #dept_options array is empty, CGI::FormBuilder will issue the warning.

Related

How do I create a Catalyst::Model::DBIC::Schema from a DBIC::Schema?

Total lack of documentation makes this difficult..
Essentially, I have
package MyApp::Schema;
sub new_schema {
__PACKAGE__->connect(...)
}
Then I have
package MyCatApp::Model::MyApp;
use Moose;
extends 'Catalyst::Model::DBIC::Schema';
## what here;
_PACKAGE__->make_immutable;
How do I make this work? If I have this in my config...
<Model::MyApp>
schema_class MyApp::Schema
traits Caching
user_defined_schema_accessor foo
</Model::MyApp>
I get the following error:
BEGIN failed--compilation aborted at MyCatapp.psgi line 4, line 1.
Error while loading MyCatapp.psgi: Couldn't instantiate component "Either ->config->{connect_info} must be defined for MyCatApp::Model::MyApp or MyApp::Schema must have connect info defined on it.
But adding sub connect_info {} in MyApp::Schema does not change anything. That error is generated on line 480 of this file.
That method seems to be looking at $schema_class->storage->connect_info which I believe is an method on an instantiated object, and not a function in the package. However, if I try to set
__PACKAGE__->config('schema_class', MyApp::Schema->new_schema)
I then get...
Error while loading : Couldn't instantiate component "MyCatApp::Model::MyApp", "Attribute (schema_class) does not pass the type constraint because: Validation failed for 'Catalyst::Model::DBIC::Schema::Types::SchemaClass' with value MyApp::Schema=HASH(0xb4a5ff0) at /usr/local/lib/perl/5.18.2/Moose/Exception.pm line 37
So, how do I go about doing this...
So.. I'm not sure this is right so I won't mark it as accepted but I was pretty damn close.. This line is dead on..
__PACKAGE__->config('schema_class', MyApp::Schema->new_schema)
And the code is in place for that to work, as per the link above on line 480 of Catalyst::Model::DBIC::Schema. However, the type is wrong..
The type of schema_class is SchemaClass. And, SchemaClass is defined here
subtype SchemaClass,
as LoadableClass,
where { $_->isa('DBIx::Class::Schema') };
Now, SchemaClass is a subtype of a MooseX::Types::LoadableClass which essentially just takes a package name and requires it..
The problem is LoadableClass doesn't take an instance of DBIC::Schema. Anyway.. I was just changed it too..
has schema_class => (
is => 'ro',
isa => 'Object',
required => 1
);
Now I can stuff an instantiated schema into schema_class and pass the conditional if($schema_class->storage && $schema_class->storage->connect_info).
I filed a bug on this here
You could instantiate the connection info in your MyApp::Schema by in it doing:
__PACKAGE__->connection(#your_connection_details);
This will set up a Storage on the class itself, not a schema object. This will then pass the Catalyst::Model::DBIC::Schema check and work successfully. I think this achieves what you're after without changing the underlying classes (though might depend on the details not included).
From the documentation you referenced, your MyCatApp::Model::MyApp would look like this:
__PACKAGE__->config(
schema_class => 'MyApp::Schema',
connect_info => {
dsn => 'dbi:Pg:dbname=mypgdb',
user => 'postgres',
password => '',
pg_enable_utf8 => 1,
on_connect_do => [
'some SQL statement',
'another SQL statement',
],
}
);
And in your controller, you would:
my $db = $c->model('MyApp');

WolframAlpha API: how to specify multiple pods?

Currently I am specifying multiple pods in the following way:
my $q = $wa->query(
'input' => $input,
'format' => 'plaintext',
'includepodid' => ['Input', 'Result', 'WeatherForecast:WeatherData', 'ForecastCharts:WeatherData'],
'reinterpret' => 'true',
);
However it then returns empty result – number of pods is undefined, so as are other fields. Setting only a single pod id with:
'includepodid' => 'Result'
does work so the code is overall correct. Using [ 'Result' ] yields the same incorrect behavior.
It's maybe a little late for you but for anyone else experiencing this: You mustn't set the includepodid parameter with delimiters you have to set it multiple times meaning instead of this:
...&includepodid=ID1,ID2
do this:
...&includepodid=ID1&includepodid=ID2
Worked for me though..

phpseclib createKey() using own primes

Is it possible to generate private and public key in PKCS#1 format using phpseclib and my own primes? I mean I already have p and q and I want to generate both keys. I am trying to do something like this:
$p = new Math_BigInteger(...);
$q = new Math_BigInteger(...);
$custom_primes = serialize(array('primes'=>array(1=>$p,2=>$q)));
extract($rsa->createKey(512,10,$custom_primes));
But this gives me the fatal error:
Fatal error: Call to a member function divide() on a non-object in /volume1/web/phpseclib/Crypt/RSA.php on line 705
I checked this and it is trying to divide:
list($temp) = $lcm['top']->divide($lcm['bottom']);
Obviously I am not setting up lcm in my $custom_primes structure as I only have my two primes. So the question is: is it possible at all in phpseclib?
It looks like you're trying to use phpseclib's partial key functionality to achieve this. Problem is that expects more than just primes. See this, for example:
return array(
'privatekey' => '',
'publickey' => '',
'partialkey' => serialize(array(
'primes' => $primes,
'coefficients' => $coefficients,
'lcm' => $lcm,
'exponents' => $exponents
))
);
$lcm is defined, initially, like this:
$lcm = array(
'top' => $this->one->copy(),
'bottom' => false
);
So maybe try doing that as well. You can probably strip out all of the calculation functions
from phpseclib, do them yourself and then pass $partial into phpseclib and let it generate a key in whatever format you want it generated in.

In Perl, what's the meaning of this code " has 'absolute_E' => (is => 'rw',default => sub {0} );"

The following codes make me so confused, I can't find any related knlowledge about the syntax "has ,is ,default, lazy". Can anybody make a detailed explain for me, best wishes.
has 'absolute_E' => (is => 'rw', default => sub {0} );
has 'retract_speed_mm_min' => (is => 'lazy');
has 'retract_speed_mm_min' => (is => 'lazy');
Judging by this line, this is probably a Moo class. To confirm this, have a look near the top of the file, and you should see something like use Moo.
Moo is an object-oriented framework for Perl. I'll assume you understand OO concepts.
Some historical background: Perl 5 has built-in OO capabilities, however it can get a little cumbersome at times. Then Moose came around as an improved way of OOP in Perl. But Moose was also quite heavy, with a compile-time cost, so Moo (and also Mouse just before it) came after that as something of a lighter-weight subset of Moose.
has is for defining attributes in your class.
has 'absolute_E' => ( is => 'rw', default => sub {0} );
This defines an attribute named absolute_E.
is => 'rw' means it is readable and writable, which means you can do this:
my $value = $obj->absolute_E; # gets the value
$obj->absolute_E($value); # sets the value
When you instantiate the object, you can supply a value for the attribute:
my $obj = My::Class->new( absolute_E => 5 );
But if you don't supply anything then absolute_E is set to 0 by default.
This second attribute has a few more things:
has 'retract_speed_mm_min' => (is => 'lazy');
This is short form for:
has 'retract_speed_mm_min' => (
is => 'ro',
lazy => 1,
builder => '_build_retract_speed_mm_min'
);
This attribute is readonly which means you can't change its value after construction. But you can supply a value at construction as before.
The builder is another way of providing a default value. It requires the class to have a separate method named _build_retract_speed_mm_min that should return the default value.
lazy works with builder. It means that the attribute should not be set by the builder until it the attribute is used. The delay may be used because the builder depends on other attributes in order to build this attribute's value.
There's a lot more in Moo and Moose. I would suggest reading http://modernperlbooks.com/books/modern_perl_2014/07-object-oriented-perl.html and https://metacpan.org/pod/Moose::Manual and https://metacpan.org/pod/Moo.
That code basically equals
has ('absolute_E', 'is', 'rw', 'default', sub {0} );
has ('retract_speed_mm_min', 'is', 'lazy');
And has looks like a user-defined subroutine.
=> is almost the same as ,:
The => operator is a synonym for the comma except that it causes a word on its left to be interpreted as a string if it begins with a letter or underscore and is composed only of letters, digits and underscores.

Customize errors symfony

There are some "best practice" in Symfony to customize form errors?
For exemple, if i would to show "Campo obligatorio" when the field is required.
1)How can i do that better way and independent from what forms call it?
2)How can i customize message 'An object with the same "%namefield" already exist.' ?
Thanks
updated
sorry, but if i try to do 'invalid' how you said me... it print me the same error
$this->setValidator('urlres', new sfValidatorString(array(
'min_length' => 6,
), array(
'min_length' => 'URL must be longer',
'required' => 'Required field',
'invalid' => 'URL exist'
)));
prints me:
* An object with the same "urlres" already exist.
updated
Felix, your solution is fantastic but it prints me this error:
"urlres: that url already exists"
Are there some way to delete "field:" ??
Thanks
Maybe this form post helps you:
Put the code
sfValidatorBase::setDefaultMessage('required', 'Field required');
in the "configure" of you application configuration apps/youApp/config/yourAppConfiguration.class.php.
You should be able to set the default value for every error message type this way.
If you want to set certain error messages for certain fields, think about to create a form class that defines all this and let all other forms inherit from this one.
The subclasses then only specify which fields should be displayed (and maybe custom validation logic).
You can find an example how to do this in the Admin Generator chapter of the symfony book.
This is the cleanest approach IMHO.
Edit:
If you want leave fields blank, you have to add the required => false option:
'email' => new sfValidatorEmail(array('required' => false))
Regarding the error message: This sounds like the urlres is marked as unique in the database table and the value already exists. Maybe you should check the database schema definition.
Edit 2:
To test both, length and uniqueness, you should use sfValidatorAnd and sfValidatorDoctrineUnique:
$this->setValidator('urlres', new sfValidatorAnd(
array(
new sfValidatorString(
array( 'min_length' => 6, ),
array( 'required' => 'Required field',
'min_length' => 'URL must be at least %min_length% chars long.' )
),
new sfValidatorDoctrineUnique(
array( 'model' => 'yourModel',
'column' => 'theColumn',
'primary_key' => 'thePrimaryKeyColumn',
'throw_global_error' => false),
array('invalid' => "That URL already exists")
)
));
Also your use of the invalid error code in the string validator is not correct. You set the invalid message to
URL exists but how can a string validator know this? It only checks whether the given string meets the min_length, max_length criteria or not.
Btw I assumed that you use Doctrine but I think the same validators are available for Propel.
Edit 3:
Set the option 'throw_global_error' => false. But I am not sure if that works.
You can also have a look at the source code if it helps you.
Let me try to help you.
You can easily customize standard form errors in configure method of your form class. Here is an example:
1)
<?php
class myForm extends BaseMyForm
public function configure(){
parent::configure();
$this->setValidator(
'my_field' => new sfValidatorString(
array('min_length'=>3, 'max_length'=>32, 'required'=>true),
array('required' => 'Hey, this field is required!')
)
);
}
2) Just change a message that has a code 'invalid'.
All you need is just find a valid message code to customize particular default messages. More info - Symfony Forms in Action: Chapter 2 - Form Validation
Updated:
And if you don't want to customize error messages in all your form classes, just create your own base validator class:
abstract class myBaseValidator extends sfValidatorBase
and there redefine default 'required' and 'invalid' messages.
Are there some way to delete "field:"
??
Yes: throw_global_error => true