HTML::FormHandler validate select - perl

I'm using HTML::FormHandler to create some forms, and I'd like to be able to validate any select fields on the form by making sure that whatever value submitted was actually a value given to the user. Right now this is how I have my select field set up:
has_field 'choice' => (
type => 'Select',
label => 'Choice',
options => [{value=>"1",label=>"One"},{value=>"2"=>label=>"Two"}],
empty_select => '---Choose an Option---',
apply => [{
check => ['1','2'],
message => 'Must be a value in the list.',
}],
);
Right now this works, but I was wondering if there was a more elegant way to do it? Since HTML::FormHandler already knows what options it has for the field, is there any way to just tell HTML::FormHandler to validate that the what the user choice is in fact one of those options? I've looked through the documentation and can't seem to find it anywhere, but it seems like something that would make sense to have for a field with predefined values. Thanks!

According to the code for HTML::FormHandler::Field::Select this check is already done. Have you tried it?

Related

Custom date field in woocommerce only years?

I have created a custom field in woocommerce where i want shopowners choose a year of publishing (books in this case).
So far i have:
//Custom Product Date Field
woocommerce_wp_text_input(
array(
'id' => '_custom_product_date_field',
'placeholder' => 'Publicatiedatum',
'label' => __('Publicatiedatum:', 'woocommerce'),
'type' => 'date',
'date-type' => 'years'
)
);
How can i set the date-type to years, as the last key => value (date-type:years) is not working?
I was curious since I was looking for details on using the date field in a woocommerce custom field. I didn't need year only specifically but it was an interesting rabbit hole to run down.
Trying to understand a bit more how woocommerce_wp_text_input creates these fields it started to become more apparent that supplying the type attribute simply passes it on to the HTML attributes. That said, these are standard HTML elements, not a wrapper of sorts that produces more fancy fields utilizing things like jQuery which seems like what the OP was expecting.
Looking over the specs for the date-related HTML text input fields it becomes apparent that there is not an <input> of type="year" available in the spec. We are limited to the type's specified in the specs.
I was able to successfully create a month input with the following:
woocommerce_wp_text_input(
[
'id' => '_my_month',
'label' => __('My month', 'woocommerce'),
'value' => get_post_meta($post->ID, '_my_month', true),
'type' => 'month',
'custom_attributes' => [
'min' => '2020-01',
],
]
);
So understandably, from what I can see, doing what you are asking isn't possible unless the HTML specifications add a year input or woocommerce provides a bandaid when specifying type as year. Hopefully this better explains how woocommerce_wp_text_input expects the data to be formatted and what is really supported.
As an aside that might assist in completing the requirements of the original question through alternate means, you could attempt to implement a jQuery UI picker which supports year only. I however feel that the jQuery UI picker using year only is a bit clumsy being it provides a popup to simply choose a date from a dropdown AND the left/right pagination of the popup still pages through months while showing only years. You might as well just use a dropdown or a number field with the date min/max values you require, both have example code that can be seen in other answers on that aforementioned answer I linked.

How to pass params in url to a backpack create form

I'm using backpack 3.3 on a laravel 5.5 installation.
I want to prefill two create form fields with two URL passed values.
Say you create a basic backpack crud, named Image.
Normally you can go to domain/admin/image/create to view and fill in the form.
But if I add one or two params in the url, I get a 404.
I guess I should manage the routes file (admin.php)
I tried this way:
Route::group(['prefix' => 'image/{k}/{cid}'], function()
{
CRUD::resource('image', 'ImageCrudController');
});
but still get a 404.
Any suggestions?
Thanks in advance.
Almost all field types have a default option. So when you define them, you can specify a default. And you can always pass a GET parameter for that default. So you can have something like this in your EntityCrudController:
$this->crud->addField[ // Text
'name' => 'title',
'label' => "Title",
'type' => 'text',
'default' => \Request::has('title')?\Request::has('title'):false, // default value
]);
You'd then be able to send you users to yourapp/admin/entity/create?title=your+default+value and have that default value show up in the field.
Hope it helps. Cheers!
it works for me easier
http://127.0.0.1:8000/admin/resultado/create?competencia=2
$this->crud->modifyField("competencia_id",[
'label' => "Competencia",
"default"=>$this->crud->request->query->get('competencia'),
.....

"Repeated" group in Forms for password validation

I am using the 'repeated' field to validate a password in the form.
According to the documentation, the 'repeated' field group is supposed
to, render two identical fields (which it is doing) and submit only if
the values from both the fields match or it should throw a validation error. The
second part somehow does not seem to work. I tried using the example
code as is but, the form submits with no issue or error even if the
passwords do not match. Did anyone else come across the same issue or
is there something I am doing wrong?
I did not add a validation file nor do I use annotations. From what I understand from the documentation, the repeated filed checks the values from first_name and second_name fields automatically and submits the form only when both the values are equal which it is not doing. This is the code I am using,
->add('password', 'repeated',
array(
'type' => 'password',
)
)
This renders two fields 'First' and 'Second' and passes an array with the 'First' and 'Second' values on submit.
the correct syntax would be
->add('tmpPassword', 'repeated', array('type' => 'password'));
if you use the the same key for the field as for the type it can not assign the value correct.
It turns out it was a stupid mistake on my part. I was not validating the form in the controller. Once, I do that it works fine.

Add a dropdown list as custom field in magento

I added custom fields as described in magento add custom input field to customer account form in admin
But I want a select list, not only a text input one. I don't know which kind of parameter I have to set and how to tell the list of possible values.
Please help :)
Thanks,
Plantex
Where you might do something like:
$setup->addAttribute('customer', 'custom_attribute', array(
'type' => 'text',
'label' => 'Customer Custom Attribute',
));
Use these values instead:
$setup->addAttribute('customer', 'custom_attribute', array(
'type' => 'int',
'label' => 'Customer Custom Attribute',
'input' => 'select',
'source' => 'eav/entity_attribute_source_boolean',
));
The type is int because you will typically be storing the index of the value chosen, not the value itself. The input is select so the admin renderer knows which control to use. The source shown here is a common example, it provides an array of "Yes" and "No" values with numeric indexes.
There are many source models already in the Magento code that you can use and you can create your own too, look at any existing one to see how it returns an array. If you make your own and if it uses text indexes instead of numeric then the type will have to be changed back to text.
Try adding this at your module setup file
'value' => array('notate_to_zero'=>array(0=>'Bleu',0=>'Rouge',0=>'Vert',0=>'Violet',0=>'Noir',0=>'Orange'))
),
or look at this --> http://inchoo.net/ecommerce/magento/how-to-create-custom-attribute-source-type/

How do I get a regular Checkbox in a Zend Form?

I have a form in Zend_Form that needs some checkboxes and I'd like them to be regular old checkboxes. You know, you give em a name and a value. If they are checked your post data contains name=>value.
Zend_Form is generating two inputs fields. One, the checkbox with a value=1 and the second a hidden input with a value=2. Both have the same name. I understand in theory how Zend expects the checkbox to work, but that's not how I expect it to work and it's not how I want it to work. How do I get my old fashion HTML checkbox back?
I have tried using $this->createElement, $this->addElement and creating a Zend_Form_Element_Checkbox manually. None allow me to set the checkbox's value and all generate the hidden input.
The final and REALLY correct answer is to add an option to the element :
$this->addElement('checkbox', 'my_element', array(
'label' => 'My Element Label',
'name' => 'my_element_name',
'disableHidden' => true
));
Zend_Form_Element_MultiCheckbox is what you're looking for.
The standard Checkbox element is meant to represent "yes/no" scenarios.
You could extend Zend library and add your own custom form element to render it just like you expect it. I did it for having a date field and it worked just fine.
I wonder why that does not work for you. You can set the values to anything you want (setCheckedValue() and setUncheckedValue()). So the only difference to normal checkbox is
if (null == $this->_getParam('checkbox', null)) {
//vs.
if ($unchecked == $this->_getParam('checkbox')) {
What exactly are you trying to do?