Conditional File Count Validator for a Zend_Form_File_Element - forms

In my project we have a Form where the user either needs to upload a file or specify some other information, for example in a text input.
I know that there is the possibility to attach a Count validator to the Form Element, and this works like this:
$upload->addValidator('Count', false, array('min' => 1, 'max' => 3));
This validator gets called, no matter if there are files uploaded or not. But as soon as i replace the validator with a custom version, and do not upload any files, the validator does not get called.
Looking at the isValid() method of Zend_File_Transfer_Adapter_Abstract, I can see the problem:
[...]
foreach($check as $key => $content) {
if (array_key_exists('validators', $content) &&
in_array('Zend_Validate_File_Count', $content['validators'])) {
$validator = $this->_validators['Zend_Validate_File_Count'];
$count = $content;
[...]
Seems like the Count Validator got some special treatment here. But how can I include my customized Validator, without overwriting Zend Files?
Because we do not extend the Zend_Form class, sadly I also cannot overwrite the isValid() method of the involved Form. Any Ideas left? Thanks for your time!

This would probably be easiest with a bit of ajax. That way you could evaluate the selection prior to posting the form.
To do it in PHP, an Element validator is probably not the way to go. It would be better to handle the selection with an if() statment.
pseudocode...
//if file element contains something and text doesn't
if($this->getRequest->getPost('file'=='' && 'text' != ''){
$file = $form->file->receive();
//do some stuff
//if text element contains something and file dosen't
}elseif ($this->getRequst->getPost('file' != '' && 'text' == '') {
if($form->isValid($this->_request->getPost('text')) {
//do some stuff
}
}
This is the idea, hope it helps.

Related

SugarCRM 6.5 CE: how to remove button in detailview according to a condition

I'm trying to remove buttons in detail view of a Lead if it is alredy converted.
I saw a similar question and it use javascript to hide buttons. I'm trying to obtain same result via php.
This is my view.detail.php in custom\modules\Leads\views\ folder
class LeadsViewDetail extends ViewDetail {
function __construct(){
parent::__construct();
}
function preDisplay() {
parent::preDisplay();
if($this->bean->converted==1) {
echo "hide";
foreach ($this->dv->defs['templateMeta']['form']['buttons'] as $key => $value) {
unset($this->dv->defs['templateMeta']['form']['buttons'][$key]);
}
} else {
echo "show";
}
}
}
Using this code, after a Quick Repair & Rebuilt, I see "hide" or "show" correctly according to the Lead status but buttons are not updated correctly.
If I open a converted Lead after QR&R, I will never see the buttons.
If I open a unconverted Lead after QR&R, I will see the buttons all times.
I'm stuck with this situation. Can anyone explain me where is the problem? How I can solve it?
Every help is very appreciated.
You can probably handle this without extending the ViewDetail by using Smarty logic ("customCode") in custom/modules/Leads/metadata/detailviewdefs.php. It looks like the Convert button is already only rendered when the user has Edit privileges, so it's not a big deal to add one more condition to it...
$viewdefs['Leads']['DetailView']['templateMeta']['form]['buttons'][] = array('customCode' => '
{if $bean->aclAccess("edit") && $bean->converted}
<input title="{$MOD.LBL_CONVERTLEAD_TITLE}"
accessKey="{$MOD.LBL_CONVERTLEAD_BUTTON_KEY}"
type="button"
class="button"
name="convert"
value="{$MOD.LBL_CONVERTLEAD}"
onClick="document.location=\'index.php?module=Leads&action=ConvertLead&record={$fields.id.value}\'" />
{/if}');
Alternatively, if you do have several conditions and they'd get too messy or difficult for Smarty logic to be reasonable, we can combine a small amount of Smarty Logic with the extended ViewDetail.
This except of custom/modules/Leads/metadata/detailviewdefs.php is actually the out-of-the-box file from SugarCRM CE 6.5.24, where it looks like they've actually tried to make this customization easier by supplying a Smarty var $DISABLE_CONVERT_ACTION. For reference, it simply needs the global config variable disable_convert_lead to be set and enabled, but I suspect that this was a relatively new feature not included in earlier versions. Still, it's a good example of using the View to set a simple Smarty variable that we can pivot on:
<?php
$viewdefs['Leads']['DetailView'] = array (
'templateMeta' => array (
'form' => array (
'buttons' => array (
'EDIT',
'DUPLICATE',
'DELETE',
array (
'customCode' => '{if $bean->aclAccess("edit") && !$DISABLE_CONVERT_ACTION}<input title="{$MOD.LBL_CONVERTLEAD_TITLE}" accessKey="{$MOD.LBL_CONVERTLEAD_BUTTON_KEY}" type="button" class="button" onClick="document.location=\'index.php?module=Leads&action=ConvertLead&record={$fields.id.value}\'" name="convert" value="{$MOD.LBL_CONVERTLEAD}">{/if}',
//Bug#51778: The custom code will be replaced with sugar_html. customCode will be deplicated.
'sugar_html' => array(
'type' => 'button',
'value' => '{$MOD.LBL_CONVERTLEAD}',
'htmlOptions' => array(
'title' => '{$MOD.LBL_CONVERTLEAD_TITLE}',
'accessKey' => '{$MOD.LBL_CONVERTLEAD_BUTTON_KEY}',
'class' => 'button',
'onClick' => 'document.location=\'index.php?module=Leads&action=ConvertLead&record={$fields.id.value}\'',
'name' => 'convert',
'id' => 'convert_lead_button',
),
'template' => '{if $bean->aclAccess("edit") && !$DISABLE_CONVERT_ACTION}[CONTENT]{/if}',
),
),
We can combine this $DISABLE_CONVERT_ACTION reference with a custom/modules/Leads/views/view.detail.php like the following to set it based on whatever condition we want:
<?php
require_once('modules/Leads/views/view.detail.php');
class CustomLeadsViewDetail extends LeadsViewDetail {
/*
* while we might normally like to call parent::display() in this method to
* best emulate what the parnts will do, we instead here copy-and-paste the
* parent methods' content because LeadsViewDetail::display() will set the
* DISABLE_CONVERT_ACTION Smarty var differently than we want.
*/
public function display(){
global $sugar_config;
// Example One: Disable Conversion when status is Converted
$disableConvert = ($this->bean->status == 'Converted');
// Example Two: Disable Conversion when there is at lead one related Call
// where the status is Held
$disableConvert = FALSE;
$this->bean->load_relationships('calls');
foreach($this->bean->calls->getBeans() as $call){
if($call->status == 'Held'){
$disableConvert = TRUE;
break; // exit foreach()
}
}
// Example Three: Disable Conversion if the User is in a specific Role, e.g.
// Interns who are great for data entry in Leads but shouldn't be making
// actual sales
global $current_user;
$disableConvert = $current_user->check_role_membership('No Lead Conversions');
// In any of the above examples, once we have $disableConvert set up
// as we want, let the Smarty template know.
$this->ss->assign("DISABLE_CONVERT_ACTION", $disableConvert);
// copied from ViewDetail::display();
if(empty($this->bean->id)) {
sugar_die($GLOBALS['app_strings']['ERROR_NO_RECORD']);
}
$this->dv->process();
echo $this->dv->display();
}
}

Codeigniter form validation callback rule issue

I am using Codeigniter 3.x form validation callback method in combination trim and required to validate a field.
The problem is, when I pipe them: trim|required|callback_some_method, the callback method seems to take precedence over trim and required and shows its error message.
Any ideas on this?
EDIT:
This is the rule:
$this->form_validation->set_rules('new_password', 'New Password', 'trim|required|min_length[8]|callback_password_check');
And this is the password_check method:
function password_check($pwd) {
$containsLetterUC = preg_match('/[A-Z]/', $pwd);
$containsLetterLC = preg_match('/[a-z]/', $pwd);
$containsDigit = preg_match('/\d/', $pwd);
$containsSpecial = preg_match('/[^a-zA-Z\d]/', $pwd);
if ( !($containsLetterUC && $containsLetterLC && $containsDigit && $containsSpecial) ) {
$this->form_validation->set_message('password_check', '{field} must contain UPPERCASE and lowercase letters, digits, and special characters.');
return FALSE;
}
return TRUE;
}
The method should return FALSE, but as long as required is before my custom rule and the field is empty, it should stop there with Required field message, NOT the custom method message.
Okay guys, I've managed to solve it by extending the Form_validation library, putting my callback method there and piping as the other rules (without callback_ prefix).
Unfortunately, as described in the code from CI, callbacks validation rules are always verified first, prior to ‘required’ for instance.
There is an official issue opened at CI : https://github.com/bcit-ci/CodeIgniter/issues/5077

cakephp form validation for counting multiple textareas as a group

Is it possible to validate a group of form textareas at once? I would like to check that at least 5 out of 15 text areas are notEmpty. Any suggestions on a method for doing this?
If you're going to down vote, explain why.
I've read http://book.cakephp.org/view/150/Custom-Validation-Rules#Adding-your-own-Validation-Methods-152 but it isn't clear to me how I would group multiple field items together and only check for a minimum of 5 notEmpty cases.
Edit: I'm using version 2.3.7
I don't really have any code to show because I'm just trying to do a data validation on a form with many textareas. My form isn't working right now due to other issues. If this was the only problem I could post all the code, but right now it would just confuse matters. I'm looking for a descriptive answer of how to validate a group of fields together.
Attach the validation rule to one textarea
You can do this by attaching the validation rule to any one of the text areas e.g.
class Foo extends AppModel {
public $validate = array(
'textarea_1' => array(
'atLeast5' => array(
'rule' => array('validate5Textareas'),
'message' => 'Please put text in at least 5 of the little boxes'
)
)
);
public function validate5Textareas() {
$filledTextAreas = 0;
// adapt this to match the names/logic of the real form
for ($i = 1; $i <= 15; $i++) {
if (!empty($this->data[$this->alias]['textarea_' . $i])) {
$filledTextAreas++;
}
}
return $filledTextAreas >= 5;
}
}
The $validate array defines a rule such that validate5Textareas is called if textarea_1 is in the data passed to save.
The function validate5Textareas will return true if 5 or more have text in them and false otherwise.

Form not passing through hidden field value correctly in Facebook, but ok elsewhere

I have a script that first checks if the form page is referred from a GET parameter
$page['is_referred'] = isset($_GET['rfr']) ? 1 : 0;
Then that set value is used as the value of a hidden field in a Symfony2 form
$form = $app['form.factory']->createBuilder('form', $data)
//....
->add('referred', 'hidden', array(
'data' => $page['is_referred'],
))
->getForm();
then
if ('POST' === $request->getMethod()) {
$form->bindRequest($request);
if ($form->isValid()) {
//...
$referred = $form->get('referred')->getData();
$msg_referer = "Thanks from Referrer";
$msg_noreferer = "Thanks";
if( 1 == $referred ){
$page['thanks'] = $msg_referer;
}else{
$page['thanks'] = $msg_noreferer;
}
//..
}
}
The hidden field is read okay on the main site when ?rfr is in the url, but when iframed in Facebook and ?rfr is present, the $msg_noreferer message is displayed instead. Any ideas as to why this happens?
EDIT: I should also mention that the field value is correctly set in Facebook when I check the source, but it just doesn't seem to post the data through correctly
I should also mention that the field value is correctly set in Facebook when I check the source, but it just doesn't seem to post the data through correctly
I’d say, the value is posted alright, but the form validation does not accept it, because the hidden field does not get added to the form on validation.
Have you checked this, if the code adding the hidden field to the form is actually being executed when handling the POST request received from the browser …?

Automatically set author and email in tt_news

I know I can configure tt_news to automatically set author and email to a given value, like this:
TCAdefaults.tt_news.author = full name
TCAdefaults.tt_news.author_email = name#domain.tld
But could I retrieve the name an email from the info of the currently logged BE user instead?
To meake it possible to fill the values on every change (if empty), you need to register a hook somewhere ie, in your own extension
typo3conf/ext/yourext/hooks/class.tx_ttnews_hooks.php
class tx_ttnews_hooks {
// hook for prefilling TCA values
function getSingleField_preProcess($table, $field, &$row, $altName, $palette, $extra, $pal, $pObj) {
switch($field) {
case 'author_email':
if($row[$field] == '') {
$row[$field] = $GLOBALS['BE_USER']->user['email'];
}
break;
case 'author':
if($row[$field] == '') {
$row[$field] = $GLOBALS['BE_USER']->user['realName'];
}
break;
}
}
}
and then add this into typo3conf/ext/yourext/ext_localconf.php:
$TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_tceforms.php']['getSingleFieldClass'][]
= 'EXT:yourext/hooks/class.tx_ttnews_hooks.php:tx_ttnews_hooks';
There is an extension that does what you want. I haven't used it myself, but the description sounds pretty promising.
As another solution, you may write a little bit of php code that adds a dynamic user ts. I only found an example in German, but maybe that's helpful anyway.