Validate custom xprofile field on edit profile page in BuddyPress - validation

OK so I’m struggling with this one for hours now and can’t get it to work, but it feels like there is a strait forward simple and elegant solution. All I want to do is to validate the value of a custom xprofile email field when a member is editing its info. Trying to mimic how xprofile_screen_edit_profile() works with returning error before saving, I tried to add_action/add_filter/do_action/apply_filter to xprofile_updated_profile, xprofile_screen_edit_profile, bp_actions, bp_screens, xprofile_data_value_before_save and more, but I failed every time probably because I don’t know how to properly use them. All I want to do is simple as:
function my_validate_email () {
if (!empty($field_id->value) && !is_email($field_id->value))
bp_core_add_message( __( 'That email address is invalid. Check the formatting and try again.', 'buddypress' ), 'error' );
//and redirect back to editing, same like for the required fields
}
add_action( 'bp_hook_here', 'my_validate_email' );
Please help with the correct way of doing this, possibly without using additional plugin
Thanks so much

Is this an additional email field ?
And not the user's email related to registration?
If it is not an additional email field, then use WP hooks.
If it is, take a look at bp-xprofile-classes, function save(), ~Line 1032
And do something like this (untested):
function my_validate_email ($this ) {
// figure out if $this is an array or object and adjust accordingly...
if ( $this['field_id'] == $the_id_of_your_email_field ) {
if (!empty($this['value']) && !is_email($this['value'])) {
$this['field_id'] = 0;
bp_core_add_message( __( 'That email address is invalid. Check the formatting and try again.', 'buddypress' ), 'error' );
}
}
return $this;
}
add_action( 'xprofile_data_before_save', 'my_validate_email', 1, 1 );

Related

Validation error on custom fields woo-commerce checkout [duplicate]

I would like add my own regular expression for validating the phone number. In my class-wc-validation.php I have changed the regular expression to my requirement.
public static function is_phone( $phone ) {
//if ( strlen( trim( preg_replace( '/[\s\#0-9_\-\+\(\)]/', '', $phone ) ) ) > 0 )
if ( strlen( trim( preg_replace( '/^[6789]\d{9}$/', '', $phone ) ) ) > 0 )
return false;
return true;
}
But the validation is not happening. What am I missing?
Was facing the same issue and followed what others had said here, but Woocommerce only sets the errors on validation after woocommerce_checkout_process hook.
But, in the latest Woocommerce 3.0 (not sure if this is in the 2.x version), we can use the woocommerce_after_checkout_validation hook and then look into the $data param if you are using the standard checkout fields or use $_POST if you have custom fields that aren't added in the standard Woocommerce way. An example of the code is:
public function validate($data,$errors) {
// Do your data processing here and in case of an
// error add it to the errors array like:
$errors->add( 'validation', __( 'Please input that correctly.' ));
}
add_action('woocommerce_after_checkout_validation', 'validate',10,2);
Hope that helps!
I have not seen your code that hooks these up to the woocommerce checkout flow.
please check their documentation on
woocommerce_checkout_process and woocommerce_checkout_order_processed
But in your case, I highly suggest that you hook it up on woocommerce_checkout_process
so put these codes below on your functions.php on your theme, or you create your own woocommerce plugins, and put it in the bootstrap code.
add_action('woocommerce_checkout_process', 'is_phone');
function is_phone() {
$phone_number = $_POST['---your-phone-field-name---'];
// your function's body above, and if error, call this wc_add_notice
wc_add_notice( __( 'Your phone number is wrong.' ), 'error' );
}
You should not edit plugin files, because if you update plugin all the
customization will be lost, rather you can use hook to achieve your
goal. You can use using woocommerce_checkout_process hook to do
this.
Here is the code:
add_action('woocommerce_checkout_process', 'wh_phoneValidateCheckoutFields');
function wh_phoneValidateCheckoutFields() {
$billing_phone = filter_input(INPUT_POST, 'billing_phone');
if (strlen(trim(preg_replace('/^[6789]\d{9}$/', '', $billing_phone))) > 0) {
wc_add_notice(__('Invalid <strong>Phone Number</strong>, please check your input.'), 'error');
}
}
Code goes in functions.php file of your active child theme (or theme). Or also in any plugin PHP files.
Please Note: By default WooCommerce use billing_phone field to take phone number, but if you have customized it then you can replace billing_phone with your field name.
Hope this helps!
In your question you're saying that validation rule is not working and I guess it's written in a wrong way. You can test it in online with regexp tools, e.g. Regex101 or others.
To answer more general on this topic, changing validation rules safely can be done this way:
Make a copy of class-wc-validation.php to your theme directory in your_theme_path/woocommerce/includes/class-wc-validation.php and make customization to the validation rules.
Then you should make a validation rule for the phone filed in checkout.js otherwise your field always will have green border despite it's invalid.
So my solution was to add custom regular expression validator to checkout.js about line 192:
if ( $parent.is( '.validate-phone' ) ) {
if ( $this.val() ) {
var pattern = new RegExp(/^([0-9\s\/\+\-\#\_\(\)]*)$/);
if ( ! pattern.test( $this.val() ) ) {
$parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-phone' );
validated = false;
}
}
}
And include your customized .js file (in functions.php)
add_action( 'wp_enqueue_scripts', 'my_checkoutjs_enqueue_scripts', 100 );
function gv_checkoutjs_enqueue_scripts() {
if ( is_checkout() ) {
wp_deregister_script( 'wc-checkout' );
wp_enqueue_script( 'wc-checkout', get_template_directory_uri() . '/js/modified_checkout.js', array( 'jquery', 'woocommerce', 'wc-country-select', 'wc-address-i18n' ) );
}}
Hope this helps!

Laravel 5 and Socialite - New Redirect After Login

Another newb question here, but hopefully someone can shed some light:
I am using Socialite with Laravel 5, and I want to be able to redirect the user to a page on the site after they have logged in. The problem is that using
return redirect('any-path-I-put-here');
simply redirects back to 'social-site/login?code=afkjadfkjdslkfjdlkfj...' (where 'social-site' is whatever site is being used i.e. facebook, twitter, google, etc.)
So, what appears to me to be happening is that the redirect() function in the Socialite/Contracts/Provider interface is overriding any redirect that I attempt after the fact.
Just for clarification, my routes are set up properly. I have tried every version of 'redirect' you can imagine ('to', 'back', 'intended', Redirect::, etc.), and the method is being called from my Auth Controller (though I have tried it elsewhere as well).
The question is, how do I override that redirect() once I am done storing and logging in the user with socialite? Any help is appreciated! Thank you in advance.
The code that contains the redirect in question is:
public function socialRedirect( $route, $status, $greeting, $user )
{
$this->auth->login( $user, true );
if( $status == 'new_user' ) {
// This is a new member. Make sure they see the welcome modal on redirect
\Session::flash( 'new_registration', true );
return redirect()->to( $route );// This is just the most recent attempt. It originated with return redirect($route);, and has been attempted every other way you can imagine as well (as mentioned above). Hardcoding (i.e., 'home') returns the exact same result. The socialite redirect always overrides anything that is put here.
}
else {
return redirect()->to( $route )->with( [ 'greeting' => $greeting ] );
}
}
... The SocialAuth class that runs before this, however, is about 500 lines long, as it has to determine if the user exists, register new users if necessary, show forms for different scenarios, etc. Meanwhile, here is the function that sends the information through from the Social Auth class:
private function socialLogin( $socialUser, $goto, $provider, $status, $controller )
{
if( is_null( $goto ) ) {
$goto = 'backlot/' . $socialUser->profile->custom_url;
}
if( $status == 'new_user' ) {
return $controller->socialRedirect($goto, $status, null, $socialUser);
}
else {
// This is an existing member. Show them the welcome back status message.
$message = 'You have successfully logged in with your ' .
ucfirst( $provider ) . ' credentials.';
$greeting =
flash()->success( 'Welcome back, ' . $socialUser->username . '. ' . $message );
return $controller->socialRedirect($goto, $status, $greeting, $socialUser);
}
}
I managed to workaround this problem, but I am unsure if this is the best way to fix it. Similar to what is stated in question, I got authenticated callback from the social media, but I was unable to redirect current response to another url.
Based on the callback request params, I was able to create and authenticate the user within my Laravel app. It worked good so far but the problems occured after this step when I tried to do a return redirect()->route('dashboard');. I tried all the flavours of redirect() helper and Redirect facade but nothing helped.
The blank page just stared at my face for over 2 days, before I checked this question. The behaviour was very similar. I got redirect from social-media to my app but could not further redirect in the same response cycle.
At this moment (when the callback was recieved by the app and user was authenticated), if I refreshed the page manually (F5), I got redirected to the intended page. My interpretation is similar to what's stated in this question earlier. The redirect from social-media callback was dominating the redirects I was triggering in my controller (May be redirect within Laravel app got suppressed because the redirect from social-media was still not complete). It's just my interpretation. Experts can throw more light if they think otherwise or have a better explaination.
To fix this I issued a raw http redirect using header("Location /dashboard"); and applied auth middleware to this route. This way I could mock the refresh functionality ,redirect to dashboard (or intended url) and check for authentication in my DashboardController.
Once again, this is not a perfect solution and I am investigating the actual root of the problem, but this might help you to move ahead if you are facing similar problem.
I believe you are overthinking this. Using Socialite is pretty straight forward:
Set up config/services.php. For facebook I have this:
'facebook' => [
'client_id' => 'your_fb_id',
'client_secret' => 'your_fb_secret',
'redirect' => '>ABSOLUTE< url to redirect after login', //like: 'http://stuff'
],
Then set up two routes, one for login and one for callback (after login).
In the login controller method:
return \Socialize::with('facebook')->redirect();
Then in the callback function
$fb_user = \Socialize::with('facebook')->user();
// check if user exists, create it and whatnot
//dd($fb_user);
return redirect()->route('some.route');
It should be pretty much similar for all other providers.
We are using the Socialite login in our UserController as a trait. We simply overrode the AuthenticatesSocialiteLogin::loginSuccess() in our controller.
use Broco\SocialiteLogin\Auth\AuthenticatesSocialiteLogin;
class UserController extends BaseController
{
use AuthenticatesSocialiteLogin;
public function loginSuccess($user)
{
return redirect()->intended(url('/#login-success'));
}
....

Codeigniter Grocery Crud update field?

$crud->set_rules('user_password', 'Password', 'trim|required|matches[konfirmpass]');
$crud->set_rules('konfirmpass', 'Konfirmasi Password', 'trim|required');
$crud->callback_edit_field('user_password',array($this,'_user_edit'));
$crud->callback_add_field('user_password',array($this,'_user_edit'));
callback function:
function _user_edit(){
return '<input type="password" name="user_password"/> Confirmation password* : <input type="password" name="konfirmpass"/>';
}
My question is how to update if only "password" not blank?
I've installed CI 2.0.3 and GC 1.1.4 to test because at a glance your code looked fine. As it turns out, it is and your code works. I modified the out of the box employees_management method in the examples controller with GC. Added a user_password column to the database and added your code to the controller.
The code both ensures that the password fields match and that they're not empty when submit.
Empty results in "The Password field is required"
Mismatched results in "The Password field does not match the konfirmpass field."
Perhaps if this isn't working for you, you should post your entire method instead of just the rules and callbacks so we can see if there are any other problems.
Edit
To edit the field, only if the password has been edited you need to add
$crud->callback_before_update( array( $this,'update_password' ) );
function update_password( $post ) {
if( empty( $post['user_password'] ) ) {
unset($post['user_password'], $post['konfirmpass']);
}
return $post;
}
This however may mean that you need to remove the validation for empty password depending on which order the callbacks run (if they're before or after the form validation runs). If they run before the form validation, you'll need to also need to run a call to callback_before_insert() and add your validation rules within the two callbacks. Insert obviously will need the required rule, and update won't.
Edit 2, Clarification of Edit 1
Having looked into it, the validation runs before the callbacks, so you can't set validation rules in the callback functions. To achieve this you'll need to use a function called getState() which allows you to add logic based on the action being performed by the CRUD.
In this case, we only want to make the password field required when we are adding a row, and not required when updating.
So in addition to the above callback update_password(), you will need to wrap your form validation rules in a state check.
if( $crud->getState() == 'insert_validation' ) {
$crud->set_rules('user_password', 'Password', 'trim|required|matches[konfirmpass]');
$crud->set_rules('konfirmpass', 'Konfirmasi Password', 'trim|required');
}
This will add the validation options if the CRUD is inserting.

How to avoid errors in saving data - Cakephp

I'm using Cakephp and trying to put in a method to make sure our reservation system doesn't let two users book the same appointment. Ex. User 1 opens the appointment, and User 2 opens it simultaneously. User 1 books the appointment. User 2 tries to book it but the system checks and sees it is no longer available.
I imagine this would take place in validation, or in a beforeSave(), but can't figure out how to do it.
Right now I made a function in the model to call from the controller. In the controller I have:
if ($this->Timeslot->checkIfNotAvailable()) {
$this->Session->setFlash('This timeslot is no longer available');
$this->redirect(array('controller' => 'users', 'action' => 'partner_homepage'));
}
and in the model I have this function:
function checkIfNotAvailable($data) {
$this->recursive = -1;
$timeslot = $this->find('all', array(
'conditions' => array(
'Timeslot.id' => $this->data['Timeslot']['id'])
)
);
if ($timeslot['student_id'] == 0) {
//They can reserve it, do not spring a flag
return false;
} else {
//Throw a flag!
return true;
}
}
I think I'm mixed up using custom validation when it's not called for. And it's not working obviously. Any suggestions?
Thanks!
If what you have is working, you can stick with it, you could also try creating a beforeValidate() call back function in your Model.
class YourModel extends AppModel {
function beforeValidate(){
if( !$this->checkIfNotAvailable( $this->data ) ) {
unset($this->data['YourModel']['time_slot']);
}
return true; //this is required, otherwise validation will always fail
}
}
This way you remove the time_slot before it goes to validation and it will drop a validation error at that point, kicking the user back to the edit page and getting them to pick a different time slot, ideally the updated data entry page will no longer have the used time slot available.

Accessing data in kohana validation

i'll try and be as clear as possible.
I'm working on some form validation using the wonderful kohana framework. However i have come at a crossroads and not sure whether the way i have taken is a wise choice.
Basically, i have a date selector using several select boxes (i toyed with the idea of using javascript date pickers but the select boxes proved to be more suitable for my purpose) and a date field in a database. I wanted to concatenate these select boxes into the date field so it can be checked to make sure its valid.
protected $_rules = array(
'mydate' => array(
'not_empty' => NULL,
'date' => NULL,
),
);
Now to me, it makes most sense to include the validation in the model, since that's where the data layer is in the MVC pattern, so i decided to create some class attributes named $_rules, $_filters and $_callbacks, each set as protected and with my basic rules applied. And then a function in the model that sets up a validation object using these attributes and returning it to whatever controller is calling it, then the controller can just run the validation and the job is done.
My problem comes when i want to concat these select boxes, to me it makes most sense to make a custom filter and pass in the post data, but with the filters rules and callbacks being attributes, i can't add any variables to them. My current solution is to manually add the extra filter in when the validation setup function is being run something similar to this:
public function setupValid($post) {
$this->_filters['mydatefield'] = array(
'MyClass::MyConcat' => array($post);
);
//creates a validation object and adds all the filters rules and callbacks
}
But i don't feel this is the cleanest solution, i'm probably nit picking as the solution works the way i require it to. However i'm not sure whether a filter was ever intended to do such a thing as this, or whether this should be a callback as the callback has access to the array by default, but then again callbacks are called last, which would mean i couldn't apply any rules like, 'not_empty' (not important in this case since they are pre populated select boxes, but might be in another case)
So i guess my question is, am i using filters as they were intended to be used?
I hope i've managed to explain this clearly.
Thanks
you need to keep in mind that you should only validate fields inside the $_rules that are very important to your database or business logic.
so for example if you would try to setup other form somewhere else in your app or you would provide a restfull api for your app, validation of the field 'day_field_(that_doesnt_exists_in_the_database_and_is_used_to_privide_a_better_ux_in_this_one_form)' => array('not_empty' => NULL) will give you a hard time to do that.
so i suggest you to keep your $_rules like they are now and provide some logic to your values() method:
// MODEL:
public function values($values)
{
if ( ! empty($values['day']) && ! empty($values['month']) && ! empty($values['year']))
{
$values['mydate'] = $values['year'].'-'.$values['month'].'-'.$values['day'];
}
return parent::values($values);
}
// CONTROLLER:
if ($orm->values($form['form_array'])->check())
{
$orm->save();
}
else
{
$this->template->errors = $orm->validate()->errors('validation');
}

Resources