sfWidgetFormJQueryDate doesn't pass validation in Symfony 1.4 - php

I'm trying to add sfWidgetFormJQueryDate to my project, but it doesn't want to pass validation, it shows "Date Invalid" when I'm trying to filter.
Here's the code:
$this->setWidgets(array(
'date' => new sfWidgetFormDateRange(
array(
'from_date' => new sfWidgetFormJQueryDate(array(
'config' => '{buttonText: "Choose Date"}',
'date_widget' => new sfWidgetFormDate(array('format' => '%year%-%month%-%day%'))
)
),
'to_date' => new sfWidgetFormJQueryDate(array(
'config' => '{buttonText: "Choose Date"}',
'date_widget' => new sfWidgetFormDate(array('format' => '%year%-%month%-%day%'))
)
))),
// ...
$this->setValidators(array(
'date' => new sfValidatorDateRange(
array('required' => false,
'from_date' => new sfValidatorDate(
array('required' => false)
),
'to_date' => new sfValidatorDate(
array('required' => false)
))),
I am probably missing something.

I found the answer, it's a bug in javascript of sfWidgetFormJQueryDate.
sfWidgetFormJQueryDate.class.php, line 106. Need to change
jQuery("#%s option").attr("disabled", '');
to
jQuery("#%s option").attr("disabled", false);
Because wrong code causes all options of day be disabled.

Related

Google Calendar API - Create Events to Send Updates

I am trying to add a few 'flags' to my Google Calendar Event. I am using the PHP API. Most specifically I am trying to set the ['sendUpdates'=>'all'] flag, so that whenever the event is modified, all those on the attendees list will get notified. I have tried to add this parameter upon inserting/creating the event. I have also tried using patch. But it does not seem to be working.
Here is some sample code:
$opts = [
'start' => [
'date' => '2021-10-11',
'timeZone' => 'US/Pacific'
],
'end' => [
'date' => '2021-10-11',
'timeZone' => 'US/Pacific'
],
'summary' => 'TEST EVENT',
'description' => 'Test description',
'attendees' => [['email'=>'test#test.com']],
'guestsCanModify' => false,
'guestsCanInviteOthers' => true,
'guestsCanSeeOtherGuests' => true,
'reminders' => [
'useDefault' => true,
],
'sendUpdates' => 'all',
];
$event = new \Google_Service_Calendar_Event($opts);
$new_event = $service->events->insert($calendar_id, $event);
Then after this didn't work, I just tried to 'patch' the event with the following:
$service->events->patch($calendar_id, $new_event['id'], $new_event, ['sendUpdates'=>'all']);
None of this is working to properly set the 'sendUpdates' flag. It DOES create the event. Pretty much ran out of options on how to fix this. Documentation is pretty unclear about how to do this, and can't find much on Stack Overflow or anywhere else.
Anyone have any suggestions?
You are very close to setting the sendUpdates parameter to all. Beforehand you have to keep in mind that sendUpdates is a parameter, not a property. Therefore you should set it in the method, not in the request body. So you only have to modify the script to look like this:
$opts = [
'start' => [
'date' => '2021-10-11',
'timeZone' => 'US/Pacific'
],
'end' => [
'date' => '2021-10-11',
'timeZone' => 'US/Pacific'
],
'summary' => 'TEST EVENT',
'description' => 'Test description',
'attendees' => [['email'=>'test#test.com']],
'guestsCanModify' => false,
'guestsCanInviteOthers' => true,
'guestsCanSeeOtherGuests' => true,
'reminders' => [
'useDefault' => true,
],
];
$optionalParameters = array(
"sendUpdates" => "all"
);
$event = new \Google_Service_Calendar_Event($opts);
$new_event = $service->events->insert($calendar_id, $event, $optionalParameters);

PHP Zend (ZF1) cant change StringLength messages in Zend_Filter_Input

I have a problem with changing messages for StringLength in Zend_Filter_Input. Code:
$filters = array(
'nazwa' => 'StringTrim',
'haslo' => 'StringTrim'
);
$validators = array(
'nazwa' => array(
'allowEmpty' => false,
'presence' => 'required',
new Zend_Validate_StringLength(array('min' => 5, 'max' => 30)),
array('Regex', array('pattern' => '/^[\w]+$/'))
),
'haslo' => array(
'allowEmpty' => false,
'presence' => 'required',
new Zend_Validate_StringLength(array('min' => 5, 'max' => 30))
)
);
$data = array(
'nazwa' => $formData['nazwa'],
'haslo' => $formData['haslo']
);
$options = array(
'notEmptyMessage' => "Pole '%field%' jest wymagane"
);
$input = new Zend_Filter_Input($filters, $validators, $data, $options);
I think I just tried everything from translating, adding options, messages in $validators and it is still default message. Please let me know how to change those default error messages (like TOO_SHORT, TOO_LONG) for new ones.
Try to set message for validator failure.
$validator = new Zend_Validate_StringLength();
$validator->setMessage(
'Your cusom message about short string',
Zend_Validate_StringLength::TOO_SHORT
);
Then add $validator to your $validators.
More information here.

sfValidator Choice is invalid symfony 1.4

i'm writing an form code with datas of another form. I'm getting an error from the validators with "choices", the erros says: dia_semana [Invalid.] id_programa [Invalid.] but i don't know how to solve this.
This is my code:
public function configure()
{
$this->setWidgets(array(
$this->validatorSchema->setOption('allow_extra_fields', true);
$this->validatorSchema->setOption('filter_extra_fields', true);
$this->setWidgets(array(
'dia_semana' => new sfWidgetFormChoice(array('label' => 'Data da Semana','choices' => array("" => "", "segunda" => "Segunda-Feira","terca" => "Terca-Feira"))),
'id_programa' => new sfWidgetFormChoice(array('label' => 'Programa',
'choices' => Doctrine_Core::getTable('tbprogramas_tv')->getProgramas())),
));
$this->setValidators(array(
'dia_semana' => new sfValidatorChoice(array(
'choices' => array("" => "","segunda" => "Segunda-Feira", "terca" => "Terca-Feira"), 'required' => false)),
'id_programa' => new sfValidatorChoice(array(
'choices' => array_keys(Doctrine_Core::getTable('tbprogramas_tv')->getProgramas()),
'required' => false)),
));
}
Someone can help me?
sfValidatorChoice uses only values of array as valid values, so instead of ['key' => 'value'] pairs you have to provide only ['key']:
public function configure()
{
$this->setWidgets(array(
$this->validatorSchema->setOption('allow_extra_fields', true);
$this->validatorSchema->setOption('filter_extra_fields', true);
$dias = array("" => "", "segunda" => "Segunda-Feira","terca" => "Terca-Feira");
$programas = Doctrine_Core::getTable('tbprogramas_tv')->getProgramas();
$this->setWidgets(array(
'dia_semana' => new sfWidgetFormChoice(array('label' => 'Data da Semana','choices' => $dias)),
'id_programa' => new sfWidgetFormChoice(array('label' => 'Programa',
'choices' => $programas)),
));
$this->setValidators(array(
'dia_semana' => new sfValidatorChoice(array(
'choices' => array_keys($dias), 'required' => false)),
'id_programa' => new sfValidatorChoice(array(
'choices' => array_keys($programas)),
'required' => false)),
));
}

How to validate multiply select using Zend Framework 2

I am trying to validate a multiply select using input filter, but every time I see a error. The error is "notInArray":"The input was not found in the haystack".(I use ajax but it doesn`t metter).
I will show part of my code to be more clear.
in Controller:
if ($request->isPost()) {
$post = $request->getPost();
$form = new \Settings\Form\AddUserForm($roles);//
$form->get('positions')
->setOptions(
array('value_options'=> $post['positions']));
//.... more code...
When I put print_r($post['positions']); I see:
array(0 => 118, 1 => 119)
in ..../form/UserForm.php I create the multiply element
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'attributes' => array(
'multiple' => 'multiple',
'id' => 'choosed_positions',
),
'required' => false,
'name' => 'positions',
));
and in the validation file the code is:
$inputFilter->add($factory->createInput(array(
'name' => 'positions',
'required' => false,
'validators' => array(
array(
'name' => 'InArray',
'options' => array(
'haystack' => array(118,119),
'messages' => array(
'notInArray' => 'Please select your position !'
),
),
),
),
What can be the reason every time to see this error, and how I can fix it?
By default selects have attached InArray validator in Zend Framework 2.
If you are adding new one - you will have two.
You should disable default one as follow:
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'options' => array(
'disable_inarray_validator' => true, // <-- disable
),
'attributes' => array(
'multiple' => 'multiple',
'id' => 'choosed_positions',
),
'required' => false,
'name' => 'positions',
));
And you should get rid of the additional error message.
Please let us know if that would helped you.

uploading a picture using forms in symfony

I need to upload images using symfony, but ive not been able to do it using my form...
A simplified model is:
Offer:
columns:
id:
name:
pic:
flyer:
description:
.
.
.
relations:
Picture:
local: pic
foreign: id
type: one
Picture_2:
class: Picture
local: flyer
foreign: id
type: one
Picture:
columns:
id:
path:
name:
.
.
.
Now, im using a form that extends OfferForm, since I need my form to have file widgets instead of a choice widget for the fields 'pic' and 'flyer'. In the saving process, then, i need to create two instances of 'Picture' to create the two Picture objects associated to this Offer.
I have not managed to find good and complete documentation on uploading files... or at least not to my case... every tutorial or article magically uses the $form->save() method, and all goes fine!, but i have had multiple errors while doing this...
this is my form class:
class myOfferForm extends OfferForm {
protected $statusChoices = array(
'A' => 'Active',
'E' => 'Expired',
);
protected $validStatus = array('A','E');
public function configure() {
parent::configure();
$this->setWidgets(array(
'id' => new sfWidgetFormInputHidden(),
'name' => new sfWidgetFormInputText(),
'picFile' => new sfWidgetFormInputFileEditable(array(
'file_src' => $this->getObject()->getPicFileSrc(),
'is_image' => true,
'with_delete' => !is_null($this->getObject()->getPicFileSrc())
)),
'flyerFile' => new sfWidgetFormInputFileEditable(array(
'file_src' => $this->getObject()->getFlyerFileSrc(),
'is_image' => true,
'with_delete' => !is_null($this->getObject()->getFlyerFileSrc())
)),
'from' => new sfWidgetFormDate(array(
'format' => '%day%/%month%/%year%',
'can_be_empty' => false,
'default' => date('Y/m/d')
)),
'to' => new sfWidgetFormDate(array(
'format' => '%day%/%month%/%year%',
'can_be_empty' => false,
'default' => date('Y/m/d')
)),
'description' => new sfWidgetFormTextarea(),
'status' => new sfWidgetFormChoice(array(
'choices' => $this->statusChoices)),
'products' => new sfWidgetFormDoctrineChoice(array(
'model' => 'Product',
'table_method' => 'getActivesOrderedByName',
'add_empty' => 'Check associated products',
'multiple' => true,
)
),
));
$this->widgetSchema->setLabels(array(
'id' => '',
'name' => 'Name *:',
'picFile' => 'Picture *:',
'flyerFile' => 'Flyer *:',
'from' => 'Valid From *:',
'to' => 'Valid To *:',
'description' => 'Description :',
'status' => 'Status *:',
'products' => 'Associated Products :',
));
$this->setValidators(array(
'id' => new sfValidatorChoice(array(
'choices' => array($this->getObject()->get('id')),
'empty_value' => $this->getObject()->get('id'),
'required' => false,
)),
'name' => new sfValidatorString(),
'picFile' => new sfValidatorFile(array(
'required' => false,
'mime_types' => 'web_images',
'path' => WebPromocion::getStaticDirPath().'/',
'validated_file_class' => 'OfferValidatedFile',
)),
'flyerFile' => new sfValidatorFile(array(
'required' => false,
'mime_types' => 'web_images',
'path' => WebPromocion::getStaticDirPath().'/',
'validated_file_class' => 'OfferValidatedFile',
)),
'from' => new sfValidatorDate(),
'to' => new sfValidatorDate(),
'description' => new sfValidatorString(),
'status' => new sfValidatorChoice(array(
'choices' => $this->validStatus,
'required' => true,
)),
'products' => new sfValidatorDoctrineChoice(array(
'required' => false,
'model' => 'Product',
'column' => 'id',
'multiple' => true, )),
));
$this->validatorSchema['fotoFile_delete'] = new sfValidatorPass();
$this->validatorSchema['flyerFile_delete'] = new sfValidatorPass();
$this->widgetSchema->setIdFormat('offer_form_%s');
$this->widgetSchema->setNameFormat('offer[%s]');
}
}
The OfferValidatedFile class:
class OfferValidatedFile extends sfValidatedFile {
/**
* Generates a random filename for the current file, in case
* it already exists.
*
* #return string A convenient name to represent the current file
*/
public function generateFilename()
{
$filename = $this->getOriginalName().$this->getExtension($this->getOriginalExtension());
if (file_exits(WebPromocion::getStaticDirSrc().$filename)) {
return sha1($this->getOriginalName().rand(11111, 99999)).$this->getExtension($this->getOriginalExtension());
} else {
return $filename;
}
}
}
And, in my action, I am doing, along with other stuff:
$this->form->save()
There is a problem. The Offer object does not have any existing Picture object to be associated with by the time the form is saved....
I think the main problem is that i want to use one form to handle submition of information related to two different objects.
So, what am I doing wrong? what am I not doing? Does someone knows a complete documentation on this subject that i could use? Is there a clean symfonian way to do this?
This documentation is for the version of symfony and doctrine you're using. Unfortunately, rather than outlining initial setup for you, they include an installer script. Note that this type of setup is outlined elsewhere in both 'A Gentle Introduction to Symfony' as well as in other parts of 'More with Symfony' for version 1.4. Fortunately there is also a fairly verbose look at refactoring the script's form class in the first link which I think would benefit you quite a bit as well - It explains the model's processing of the form (Where you appear to be having issues) quite well, which may make debugging a little less mystifying for you.
I recommend giving it a good read. I followed this for a project a few months ago and came out of it without any issues.

Categories