"Repeated" group in Forms for password validation - forms

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.

Related

Thousand separator in symfony input form

I have in my symfony5 form input
->add('required_amount', IntegerType::class, [
'label' => 'Požadovaná částka',
'grouping' => true,
]);
when run form the thousand sepator isn dispaly on typing number
whatis wrong in my code, may I use number format in input in form in symfony
I copy your code and runs just fine to my 4.4.4 symfony project. If you submit this form the output will be 1,000,000 or 1.000.000 depending on your locale value default set in .env file. The grouping property is in IntegerType , MoneyType and NumberType and do the same in all this classes , so this is not your problem. If you describe something in client side (as you type the numbers to add the separator) you need to go with javascript not php, but i don't thinks this is the case.

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.

HTML::FormHandler validate select

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?

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?

CakePHP Form in an Element Causing Errors

I think this may be an easy solution, but I've spent an hour now investigating to no avail.
I have a registration form in an element that is being used in views belonging to different controllers. Using the "url" attribute, I've told it to submit to /users/register, but for some reason, the fields aren't submitting to the database.
Instead, there are errors for "undefined index" and a MySQL error for an undefined secondary key that I set (it's empty because it's not being submitted). Strangely, the form works fine if I include the element somewhere in the users views. Does anyone know why this is happening?
UPDATE - Here's the relevant code, sorry:
<?php
echo $form->create(array(
'id' => 'signupform',
'url' => array(
'controller' => 'users',
'action' => 'register')));
?>
The form fields are all correct, since the element works in the user controller's views anywhere. Do I need to include any other information in the creation of the form to point it more directly?
Specify the model 'User' as the first parameter to the $form->create() method.
<?php
echo $form->create('User', array(
'id' => 'signupform',
'url' => array(
'controller' => 'users',
'action' => 'register')
)
);
?>
This will ensure that the form fields are named as data[User][field_name], and prevent you from seeing that undefined index error.
Hope this works for you!
Without seeing the code, it sounds like Cake is magically assuming that the Model is the one for the controller that controls the current view. When that controller is Users, it works correctly. When it is, say, Articles, it will be trying (and failing) to fit the form fields to the Article model.
Without seeing any code, it is impossible to offer any more help.
EDIT:
If the form contains mixed models, e.g. User and Article, you must prefix the fieldnames like this:
$form->input('User.username');
$form->input('Article.title');
etc.
If you don't, the controller will assume they all belong to its own model.