Add specific validation rules for a custom field

  • Posts: 8
  • Thank you received: 0
12 years 11 months ago #33707

I would like to add a custom date field on the checkout page. I need to do some custom validation on this date field. Customers should select a date in the future (which is already possible) but some dates are not allowed. Is it possible to add some specific validation code (even if it means that this code is lost when upgrading hikashop)

Please Log in or Create an account to join the conversation.

  • Posts: 82909
  • Thank you received: 13379
  • MODERATOR
12 years 11 months ago #33733

You would have to edit the file administrator/components/classes/field.php and change the function display of the class hikashopDate

Please Log in or Create an account to join the conversation.

  • Posts: 8
  • Thank you received: 0
12 years 11 months ago #33838

Thanks.

Just another remark about the existing validation on 'future' or 'past' dates. When selecting a date from the date control, the validation is done correctly but when a user types a date without using the date control, no validation is done. Is that a (known) bug or am I doing something wrong ?

Please Log in or Create an account to join the conversation.

  • Posts: 82909
  • Thank you received: 13379
  • MODERATOR
12 years 11 months ago #33856

It's not a known bug. That feature has been added only recently and no one noticed that so far.
Maybe it's because the date entered is not a valid date and thus the Date js function is not able to understand it and the the test is just skipped ?

Please Log in or Create an account to join the conversation.

  • Posts: 8
  • Thank you received: 0
12 years 11 months ago #33901

I even have the same issue when entering a date manually in the same format as it is displayed when selecting a date from the control. Would it be possible that I disable the textfield so that users should use the control to select a valid date ?

Please Log in or Create an account to join the conversation.

  • Posts: 82909
  • Thank you received: 13379
  • MODERATOR
12 years 11 months ago #33910

Could you add the line:
$size .= ' onChange="'.$this->prefix.$field->field_namekey.$this->suffix.'_checkDate();"';

after the line:
$field->field_options.='", onClose : '.$this->prefix.$field->field_namekey.$this->suffix.'_checkDate, //';

in the file administrator/components/classes/field.php and try again ?

Please Log in or Create an account to join the conversation.

  • Posts: 8
  • Thank you received: 0
12 years 11 months ago #33923

Thanks a lot, the validation on the date field is also executed now when entering a date manually. There is only one issue when people entering dates in an incorrect format (not yyyy-mm-dd). In that case, the validation does not work properly but I can add an additional test in the javascript to check the format with a regular expression.

Please Log in or Create an account to join the conversation.

  • Posts: 82909
  • Thank you received: 13379
  • MODERATOR
12 years 11 months ago #33956

Yes, we'll add a check for that on our end too. Thank you for your feedback.

Please Log in or Create an account to join the conversation.

  • Posts: 8
  • Thank you received: 0
12 years 11 months ago #33989

In which version will this bugfix be included and when will it be released. I just started with the Hikashop starter edition and have done an evaluation of the functionality. I'm happy with it and I will sell the business edition in the coming days.

Please Log in or Create an account to join the conversation.

  • Posts: 82909
  • Thank you received: 13379
  • MODERATOR
12 years 11 months ago #34034

It will be in all versions of course.
It will be released with next version in several weeks.

If you want it now, you can just add the code:
if(!selectedDate){
selObj.value=\'\';
alert(\''.JText::_('INCORRECT_DATE_FORMAT',true).'\');
return false;
}

after the line:
var selectedDate = new Date(selObj.value);

in that same file and it will display an error message when the date doesn't have the correct format.
Also, the line below will have to be added to your translation file:
INCORRECT_DATE_FORMAT="Incorrect date format"

Please Log in or Create an account to join the conversation.

  • Posts: 8
  • Thank you received: 0
12 years 10 months ago #34103

When adding the code for checking the date format, I always get the following error :
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in .../public_html/administrator/components/com_hikashop/classes/field.php on line 1219

For the rest everything works fine in Firefox and Google Chrome but I tested also on Internet Explorer 7. In this browser, the check on future or past dates doesn't work at all. When selecting a date in the control or typing a date manually, an 'Error on page' occurs.

Please Log in or Create an account to join the conversation.

  • Posts: 82909
  • Thank you received: 13379
  • MODERATOR
12 years 10 months ago #34141

And what do you have on line 1219 ?

Please Log in or Create an account to join the conversation.

  • Posts: 8
  • Thank you received: 0
12 years 10 months ago #34154

I tried again and now after my upgrade to the Business edition,I don't have the error anymore. It was probably due to a mistake in the translation file. So, now I don't have the error anymore but, I never get the message 'Incorrect date format', even when I type a date in a format like 15/12/2011. When I try it with the regular expression it works (except in Internet Explorer).

Besides, I also have a question about the checkout process. In my current checkout process, I see the different parts (as defined in the configuration) and everything works fine but at the end of the checkout process, users have to click the 'next' button. After clicking 'next', a page is displayed that tells them that the order was already submitted. I wonder if there is a possibility to add some confirmation page in between so that users really now that they will already submit the order when pressing the 'next' button. Another option would be that I can change the 'Next' button on the bottom of the checkout page into a button with label 'Submit order'. In that case, no intermediate confirmation is necessary.

Please Log in or Create an account to join the conversation.

  • Posts: 82909
  • Thank you received: 13379
  • MODERATOR
12 years 10 months ago #34183

If you want it to work for IE you will need to use a specific format of date:
stackoverflow.com/questions/3046459/ie-d...ate-with-time-string

Otherwise, it would mean to parse manually the date (really complicated) or use an external JS library like dateJS (easier but still requires work for the library to be integrated into the system).

For the incorrect date, that will require quite some changes. Here is the Display function code which works properly on our end:

function display($field,$value,$map,$inside,$options='',$test=false){
if(empty($field->field_options)) $field->field_options = "%Y-%m-%d";
$format = $field->field_options;
$size = $options . empty($field->field_options) ? '' : ' size="'.$field->field_options.'"';
if(!empty($field->field_options)){
JHTML::_('behavior.mootools');
$processing='';
switch($field->field_options){
case 'future':
$check = 'today>selectedDate';
$message = JText::_('SELECT_DATE_IN_FUTURE',true);
break;
case 'past':
$check = 'today<selectedDate';
$message = JText::_('SELECT_DATE_IN_PAST',true);
break;
}
$js = 'function '.$this->prefix.$field->field_namekey.$this->suffix.'_checkDate(nohide)
{
var selObj = document.getElementById(\''.$this->prefix.$field->field_namekey.$this->suffix.'\');
if(selObj.value==\'\'){
return true;
}
var timestamp=Date.parse(selObj.value);
var today=new Date();
if(isNaN(timestamp)!=false){
selObj.value=\'\';
alert(\''.JText::_('INCORRECT_DATE_FORMAT',true).'\');
return false;
}
var selectedDate = new Date(timestamp);

'.$processing.'
if('.$check.'){
selObj.value=\'\';
alert(\''.$message.'\');
}else{
if(!nohide) this.hide();
}
}';
$document = & JFactory::getDocument();
$document->addScriptDeclaration($js);
$size .= ' onChange="'.$this->prefix.$field->field_namekey.$this->suffix.'_checkDate(1);"';
}

return JHTML::_('calendar', $value, $map,$this->prefix.$field->field_namekey.$this->suffix,$format,$size);
}


You can add a confirmation step. Just configure your checkout workflow in the Checkout tab of the configuration for that.
You can also change the next button for a specific step: www.hikashop.com/en/support/forum/4-how-...bel-next-button.html

Please Log in or Create an account to join the conversation.

  • Posts: 8
  • Thank you received: 0
12 years 10 months ago #34331

Thanks for the information.

The code that you have sent works fine in Google Chrome as well as in Firefox but the date control doesn't close anymore after selecting the date. When clicking the x button, nothing happens. It's not blocking as it's still possible to click the next button but it's not optimal and in Internet Explorer, the control doesn't work at all. Here, the user is really blocked because the 'incorrect_date_format' error pops up every time you select a date and the close button doesn't work. Because of this, I'm not sure if this code should be implemented as a global solution in a next release.

Please Log in or Create an account to join the conversation.

  • Posts: 82909
  • Thank you received: 13379
  • MODERATOR
12 years 10 months ago #34366

I'm working on Chrome and the close button is working fine on my end. It's working on IE too.

For the incorrect date format on IE, as I said, you would have to use a specific date format if you want it to work: %Y/%m/%d
You can configure it in the format option of your custom field.

Please Log in or Create an account to join the conversation.

Time to create page: 0.140 seconds
Powered by Kunena Forum