Jump to content

Commerce > Custom Fields > Custom Input Validation?


IPCommerceFan

Recommended Posts

Hi guys,

I've finally had a chance to poke around 4.0 (Beta 6), and was wondering about the possibility of adding Custom Input Validation to custom fields in Commerce (specifically regex), similar to how it is available in custom fields for Pages?

e.g. - When creating a new custom field, select Text as the field type, and a new option for Custom Input Validation appears where you can enter the validation criteria.

It seems to me there should actually be parity across Commerce and Pages custom field type options, at least as far as things like this, field length, and text formatting are concerned.  I would love to have a custom commerce field automatically converted to upper case once submitted, as well as limited to a certain number of characters and the option of setting a default value!

Thanks!

Edited by Steven@wt
Link to comment
Share on other sites

  • 2 months later...

It looks like this was added to custom fields for support requests (or it was already there?), but this would still be an awesome addition to custom package fields!

P.S. -  So far so good testing 4.0.0 with a copy of my live site.  I *almost* switched to cs-cart because of missing features like this one, but the complete integration IPS offers is very hard to beat, even in this very early stage.

Link to comment
Share on other sites

  • 3 years later...
  • 1 month later...

I got this working by modifying \IPS\nexus\Package\CustomField (the actual php file that relates to that class)!

image.thumb.png.8639d82ff025fe08313075497d2c027b.pngimage.thumb.png.0c2e0f667139acfce9b9e60a0f4cbcfb.png

 

All I had to do was:

- borrow the $form->add statements for pf_max_input and pf_input_format from  \IPS\CustomField\CustomField
- comment out the "unset" statements for those fields
- Add the fields to the nexus_package_fields table in the database

It works like a charm!  Now to figure out how to do what I did in the form of a plugin or application since I don't really feel comfortable with the fact I edited the source files directly...

Still hoping IPS will roll this into the core product, but I'm very thankful for the fact the framework was intuitive enough to allow this workaround!

Edited by IPCommerceFan
Link to comment
Share on other sites

  • 5 months later...
  • 1 month later...

With InvisionCommunity 4.x.x maturing and talks of 5.x.x starting to come up, I'm starting to wonder whether Commerce will survive considering a "simple" feature like this hasn't been given any attention since 2015. 

It would go a long way for me, and my business, if topics like this could at least be given some token acknowledgement. 

I understand having limited engineering resources since I deal with this every day as well.  Having to triage what gets attention and what doesn't, inadvertently sending the wrong message, etc.

It would just be nice to get a "Thanks for the suggestion", or "we're considering it", or even a "no", so that we can stop wondering if posting feature requests are a worthwhile use of time, and perhaps consider other options.

Link to comment
Share on other sites

Setting aside the fact that only 2 people have responded to this topic (one of which was you, the original poster) - improvements and consolidation of code/consistency for custom fields features is something that has been discussed internally, more than once, but there is no current timeframe we can say to expect to see it.

Unfortunately, it's simply not feasible to respond to every single topic to say "we've read this but there's nothing to note or add at this time".

Link to comment
Share on other sites

  • 8 months later...

I needed this to be more robust, so I started poking around and discovered I could hook into \IPS\Helpers\Form\Text --> validate() and use custom php to do any kind of validation I want on a given custom commerce field! (custom Text field anyway, I haven't explored other types)

In case anyone is need of this and finds this thread during their search, this is where you can run custom validation:

//<?php

/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
	exit;
}

class hook491 extends _HOOK_CLASS_
{
	/**
	 * Validate
	 *
	 * @throws	\InvalidArgumentException
	 * @throws	\DomainException
	 * @return	TRUE
	 */
    
	public function validate()
	{
      	$dataToBeValidated = $this->value; //$this->value is anything that is entered into any field on the order form.
      
      	/* 
         * Run any if statement on your data to determine whether it should return false, some specific value, etc
         */
      
      	if ($dataToBeValidated > 0) { $validation == 1; } 
      
      	//"$this" doesn't know the id of the field, but it knows the name.  Here we will evaluate the value entered for the Nexus package custom field with an ID of 5.   ( nexus_packages_fields.cf_id in the database )
		if ( $this->name == 'nexus_pfield_5' and $validation == 1 and !preg_match('/[A-Z0-9]+/i', $dataToBeValidated) )  //we can add any number of conditions to decide whether the field should be validated or not
		{	
          	throw new \InvalidArgumentException( 'form_bad_value_nexus_pfield_5' ); //added langauge bit to dev\lang.php "'form_bad_value_nexus_pfield_5' => 'Custom validation failure message'"
		}
		return parent::validate();
	}

}

Hope this helps someone, and if there is a better way, I'm open to suggestions!

Edited by IPCommerceFan
Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...