Drupal registration - add validation to every field - php

I would like to add a validation function to every form field on the Drupal registation page, including the custom Profile fields I've added.
Is there a simple way to do this without manually attaching it to each field? Would like it to support future profile fields that may be added without having to manually update the code.

Depending on the complexity of the validation, the Validation API module may be able to do what you need. Be aware that the module is no longer being actively maintained though.
Depending on your needs, the Clientside Validation module might also work, though the rules wouldn't be applied to users with Javascript disabled.

I'm assuming you're on D6.
You can kinda see what you need to validate here:
http://api.drupal.org/api/drupal/modules--profile--profile.module/function/profile_form_profile/6
If you implemented something with hook_elements you could validate anything the profile module creates. It shouldn't be too hard if you've got some good developer skills.
http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_elements/6

You can access the form with a hook_form_alter, and add one of more additional form validators.
Make sure you don't overwrite your default validator, but just add an extra one.
$form['#validate'][] = 'my_custom_validate';
This extra validate handler could loop over the profile fields and check for empty entries for example, or something else.

Related

Joomla form variable handling

I have a situation where I need to create a shortcut for a specific filter in a Joomla component.
The problem is that I cannot unset it, as I do not know if Joomla sets the form fields to its own session handler, request handler or some kind of custom handler. There also does not appear to be any documentation on this specific case.
The full situation is that I have a link that will auto filter in the same view as another link (in the components sidebar). One view will be just a specific filter and the other is standard. So I need it when you click into the filtered view it will reset the current filters to make sure everything displays as it should, and vice-versa so clicking back will again reset the filters.
I have tried a number of approaches for this, and although I can consistently force it to filter but it will not reset the form when I re-enter the last page with any technique I have tried so far and of course I want to avoid bypassing Joomla's default functions.
if(JRequest::getVar('filter_group_id',false)==10){
JRequest::setVar('last_filter',true);
EthicstoolHelper::addSubmenu('supervisors');
}else{
if(JRequest::getVar('last_filter',false)===true){
JRequest::setVar('last_filter',false);
JRequest::setVar('filter_group_id',false)
}
EthicstoolHelper::addSubmenu('users');
}
This is the most recent think I have tried, as you can see I try to reset the value to false in a hope that Joomla will read it as not being set, as JRequest has no built in unset method.
I don't have enough rep to comment yet, so I'm guessing a bit as to what the problem could be. Assuming that you are using a model to set the state of the filters, you can look at overriding the populateState method.
Another options is to fiddle with the context property in the model. For example, you could change the context if you have your special filters enabled if you are using things like $app->getUserStateFromRequest(). If you can post a bit more information about the design of your component (controllers and models), I can help more.

Joomla Virtuemart 2 plgVmOnSelectPayment()

How can I add a form or HTML output to plgVmOnSelectPayment()?
I see no documentation about that.
I don't know this event Virtuemart payment select works.
I want add after selection a form with input fields.
Can anyone help ?
The plgVmOnSelectPayment() from what I've reversed engineered doesn't give any output to the HTML, but you can modify the cart content with that event.
I was myself in need of adding a form after a particular payment method was selected (to guarantee some additional data being supplied). Depending on where you want it you can either edit the View for the cart and add an if-statement where you see if the particular payment method is chosen. And then later access this information from plgVmConfirmedOrder() (you can access it through the $_POST global.
(This is an untested theory)
If you want to add the information to the field you can access the display form and add information there through the plgVmDisplayListFEPayment() event. Look in the vmPSPlugin class to see how they manage it there. What I did was simply copy that to get an image into that form, but you should be able to add other things there as well. And then you might be able to access the information there through plgVmOnSelectPayment() and using the $_POST global.
As a fair warning. All this I've gotten through reverse engineering VirtueMart. So it's neither pictue perfect nor might it be best practice. But the VirtueMart documentation leave not many other options :(. So if someone have a better way, I'm very interested in knowing that as well!

Drupal, Ubercart - Add custom field to checkout form

Need help of Drupal experts.
My purpose (with Drupal 7 and Ubercart 3 in hands) is to add a custom field to the Checkout page, that should be dynamic (values from ajax request) and it content depends on user's delivery city input.
With "Extra Fields Pane" module I've successfully created field with some placeholder value. Than, I use JS methods to append values to that field. The problem has appeared when I submit form with that dynamic-added selected value - I have an "invalid selection" error for that field. When non-added-by-js value (placeholder) selected - everything works as expected.
Can you please hint me solution to that problem?
I found one here https://stackoverflow.com/a/5159013/837255 and it seems to be a common approach, but here other issue begins.
In %my module% in a hook I can't access field to do manipulations on it.
Example of how I need to make changes in created by module 'ajax_field_name':
$form['panes']['delivery']['ajax_field_name']['#ajax'] = ....
But my *cking pane has no any $form['panes']['delivery']['ajax_field_name']. When I do var_dump($form['panes']['delivery']) I see that 'ajax_field_name' located somewhere in $form['panes']['delivery']['address']['#uc_addresses_address'] OBJECT behind a private property.
function uc_nova_poshta_form_alter(&$form, &$form_state, $form_id){
if ($form_id == 'uc_cart_checkout_form'){
$obj = $form['panes']['delivery']['address']['#uc_addresses_address'];
// addressBook is a private property
$obj->addressBook;
}
}
In fact, this code does what I need -
$form['panes']['delivery']['address']['ajax_field_name2'] = array(
'#type' => 'select',
...
}
creates a custom field, in a right place, with access to it, BUT only in that form and this field does not affect any further activity (order review, admin pages, etc.). Also in $form['panes']['delivery'] this field ('ajax_field_name2') is located separately from created with module 'ajax_field_name'.
I guess the reason of this behavior are some modules e.g. uc_addresses (am I right?), but even when I disabled most suspicious of them - the problem is still there.
Is there a possibility to find out how to get access to 'ajax_field_name' created field?
Thank you.
This may not completely solve your problem (if you still have it as this seems a rather old question...), but one problem is you are using the wrong hooks. You should use hook_uc_checkout_pane_alter and target uc_checkout_pane_delivery to accomplish this. I learned this the hard way trying to set the default country selected to something other than US; things will break badly if you manipulate them via hook_form_alter and it's variants.
I'm working through something similar right now. I will update this answer with further information as I uncover it.

Can I use dynamically created form fields with the Security Component in CakePHP 1.3?

Using CakePHP 1.3, I have a (working) form that has dynamically created form fields (via Javascript). Everything works great, multiple models are saved via saveAll(), and it's just beautiful.
But, I get black-holed to a 404 whenever I enable the Security component (hoping to get some of the auto-magic CSRF protection).
I understand that this may be (probably is!) caused by the dynamically created form fields, as mentioned in the docs.
Is there a way to get them to play nicely together?
You can't have your Cake and eat it, too. (Cha-ching!)
CSRF protection means precisely that only a certain list of form fields is allowed to be submitted. This list is decided upon and fixed at the time the form is created. You can't be CSRF protected and dynamically alter the fields in the form.
There are two solutions:
If the number and names of the dynamically created fields are limited, create them all in the form and hide them using CSS, then show them using Javascript. This way you're not dynamically creating the fields, but are only dynamically showing them.
If that doesn't work, you can either whitelist the fields using the $disabledFields option (again, only if their names are known in advance) or disable CSRF altogether with the $validatePost option.

Symfony Form Framework - Best way to implement change password functionality?

I'm trying to get my head around the form framework in Symfony 1.4. I've read the incredibly detailed section in the 'More with symfony' book, but I'm still a but unsure how to implement a simple 'Change password' functionality.
The requirements are pretty basic,
There'll be two fields, new_password, and confirm_new_password. Both will be input fields.
The new_password field will be validated to be a string between 6 and 30 characters containing both letters and numbers.
The confirm_new_password field will be validated to match the new_password field exactly.
Now, presently I implemented this by,
Adding 2 new fields to my form.
Adding a string validator to the new_password field to check the string length.
Adding a string validator to the confirm_new_password field to make sure it was filled in.
And then validating the new password is valid and matches the confirm password in a custom post validator. I did this because I didn't want to validate the confirm_new_password field until the new_password field was valid.
Now to the point of my question. After reading the article mentioned above, I'm starting to think I should contain the two fields in either a single widget or in a sub form as they rely upon each other heavily, and one is useless without the other.
I was wondering what peoples thoughts were on this, and if someone had implemented one, how they did it?
Thanks
Note: There is no current_password field as this is for my admin area.
You can use the sfDoctrineGuardPlugin.
This plugin provides convenient forms, model and controllers for your website user management.
You can extends this plugin, customize to fit your needs, this is a very commonly used plugin (the most used).
I created some kind of password validator for symfony 1.2 (I have not tested it for 1.4). You can have a look at it here.
The basic features are:
Validates the password against a regular expression or multiple regular expressions.
Compares whether the values in both fields are the same.
If a password is already set, the password fields don't have to be filled in order to validate (convenient if the password change fields are integrated in a profile form where the fields can be empty if one does not want to change the password).
To use it, you have to set up two input fields and add the validator as post validator for these two fields.
It is not exactly what you want but maybe it gives you a start.
I have not heard of anything already built to handle this for you.
Custom widget and validator
You could create a custom widget to encapsulate the two form fields as long as you're alright with achieving this with only one label associated with your fields. If you wanted to create a custom validator for this it would have to be a post validator (see sfValidatorSchemaCompare for an example) as field validators only receive one value to validate. With that approach you would end up gaining:
defining one less widget in your form
moving the post validator functionality into a separate class
and a loss of:
a label for your confirm password field
Embedded form
An embedded form seems wrong to me for this as the password/confirm fields aren't a subsection of the parent element.
Inheritance
Another approach that may work for you is to create an sfPasswordEditForm class that your forms can extend that defines your two password fields and post validator. You can then use the moveField method off of your widgetSchema to place the fields in the order you want them in your custom form:
$this->widgetSchema->moveField('password', 'after', 'username');
$this->widgetSchema->moveField('password_again', 'after', 'password');
A few people suggested looking at the sfGuardPlugin. After doing so I noticed it implements the change password feature pretty much exactly how I'm currently implementing it.
Thanks for the answers!

Categories