Call to a member function on a non-object error symfony2 - forms

This is an odd one. I have this method, that is the callback for the FormEvents::POST_SET_DATA event in an EventSubscriber of a form in Symfony2:
public function preSetData(FormEvent $event)
{
$data = $event->getData();
$form = $event->getForm();
$resource_provider = $data->getResourceProvider();
$type = $resource_provider->getType(); //PROBLEM HERE
\Doctrine\Common\Util\Debug::dump(is_object($resource_provider));
}
It has more code but the error is firing in the line with the comment PROBLEM HERE.
The issue is that the dump there prints bool(true), so it is an object, and have the data inside, but trying to call getType(), that exist, fails with this error:
Error: Call to a member function getType() on a non-object
To add more confusion, if I dump $resource_provider->getType() I get the expected content dumped, but the error remains.
Does someone knows or has a clue on what can be happening here?
EDIT
Can't believe it! I found the answer, but doesn't makes sense at all! I had to wrap the problematic code with a conditional like this:
if(is_a($resource_provider, 'My\Bundle\Path\Entity\ResourceProvider')){}
and it worked! If someone can tell me what is the sense of that, I will happily select his/her answer as correct.

The problem is that $resource_provider is probably null as error says $resource_provider is not an object.
When printing the value of $resource_provider and skipping the script, it shows the right object of class 'My\Bundle\Path\Entity\ResourceProvider' :
echo get_class($resource_provider); exit; // Display My\Bundle\Path\Entity\ResourceProvider and skip the script
When adding a condition to test is the object is of the expected class, the script doesn't crash anymore :
if(is_a($resource_provider, 'My\Bundle\Path\Entity\ResourceProvider')){}
In addition, when skipping script when it's not an object, $resource_provider is null and the script skipped :
if(is_a($resource_provider, 'My\Bundle\Path\Entity\ResourceProvider')){
} else {
var_dump($resource_provider); exit;
}
$resource_provider is an object and null in the same "execution" of the script.
So we can deduce that the EventSubcriber is loaded more than one time, perhaps before the form binding and after form binding for example and the first time with the expected object, and a second (or more) time with no value, as null, perhaps because the $resource_provider is not filled when submitting form or something else.

Related

Swift not allowing if statement because it dose not recognize an variable that is defined in the if statement

I am trying to get the input of a text field so that if the user is the one that is allowed in then their user credentials can be matched to a folder that is in the cloud but swift is giving the error "expected declaration" but there is no variable that is undefined. if anyone can help me then that would be greatly appreciated.
fist section of code second section of code
It's giving you the expected declaration error in your first screenshot because your if statement is not in the body of a function. You have to put your if statement in the body of a function.
func checkUserCredentials(){
if username_Input.text == "tyler" && password == ("1234"){
//do something
}
}

Perl Error: Can' t call method 'Mangle' on an undefined value

I am trying to execute a perl script in that we have statement like this-
# Calculate password using PwdMangler
$PwdMangler = Win32::OLE->new('PWDMangler.Mangler');
unless($password = $PwdMangler->Mangle($user , $password_plain))
{
print $log_file "Error occured in PwdMangler\n";
exit (1);
} # End Unless
but this perl is giving an error like this-
Can't call method 'Mangle' on an undefined value.
So, i tried to print $user and $password_plain values and i am able to capture these values.
Any suggestion for this error.
From CPAN: The new() class method starts a new instance of an OLE Automation object. It returns a reference to this object or undef if the creation failed.
You should check if the PWDMangler.Mangler is a proper program id.
P.S.: Don't want to look dumb, but are you sure that PWDMangler should not be PWDManager?

Codeigniter callback on empty fields?

I'm having problems with callback on empty fields.
For some reason i need to compare value of field with some other parameters and to make it "required" only if some other conditions are fulfilled buy checking it from callback function.
Unfortunately, if field is empty, callback function is not called.
DO you have any idea how to overcome this issue?
This "bug" is actually a "feature" of the Form Validation. If the input is empty and the required rule is not set, then it skips all other rules (even non-callback functions). You can extend (or hack) the validation to continue through all functions even if the input is blank.
In system/libraries/Form_validation.php at about line 486, you will see the comment:
// If the field is blank, but NOT required, no further tests are necessary
You will then see two lines with a return; shortly after that comment. The lines for me are at line 498 and line 536 (about 12 and 50 lines after the comment). If you comment out those two lines, then the validation will continue through all function even if the input is blank.
That being said, you can extend the Form Validation by creating a file at application/libraries/Form_validation.php and overriding the _execute function:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation
{
protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
{
...
}
}
Just copy the entire function to the new file and comment out the two returns described above.
you can define validation rule like this
you can set with a condition
if($this->input->post('first_field_name')){
$this->form_validation->set_rules('2ndfield','Field Name','required|callback_myfunction');
}

Symfony2 error message in the input field

I would like to display error message in the field it is for. I know how to put the error message into the field, but I want to find a dynamic way to check after re-submittion, if the inserted value is not the error message itself. I use doctrine annotations.
For example if the field is "title", the error message would be "The title must be filled!".
So the title field is not empty anymore, I click submit again, and it is valid now. I don't want to check every single field like
if $entity->getTitle() == "The title must be filled" ...
I've managed to do this with not displayed error divs in the twig, and jquery, but I want to know if there is a better way to do this from the controller? Thanks
You're asking how to correctly do something the wrong way... If the input value is not the value you want processed, it should have never been the value to begin with. That being said, I'm sure you have your reasons...
You need to listen to FormEvents::BIND_CLIENT_DATA and clear the form data if it matches your error string.
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('title');
$builder->get('title')->addEventListener(
FormEvents::BIND_CLIENT_DATA,
function(FilterDataEvent $event)
{
if ('The title must be filled' == $event->getData()) {
$event->setData('');
}
},
);
If you want to apply this behavior globally you need to attach this listener using a form type extension that extends 'field'. You will also need to introspect all of the possible validation error messages for the current field using the validator and pass these through the translator, then compare the results with the event data.

Perl: how to validate successful call to "XML::eXistDB::RPC"

i am writing a small perl app using the eXist database, and i am wondering is:
how can i see that my call
my $eXist = XML::eXistDB::RPC->new( destination=>$eXist_db, repository=>$bank, user=>"admin", password=>"pass" ) ;
is successful or not ?
thanx
When object initialisation fails, it will be messaged through Log::Report, so hook into that.
This only happens if the programmer to neglected to set either rpc or destination parameter. The new constructor will always return an object instance.
According to the docs:
All methods return a LIST, where the
first scalar is a return code (RC).
When that code is 0, all went well.
Otherwise, the code represent the
transport error or the exception
(refusal) as reported by the server
logic. In either case, the second
scalar in the returned list contains
the error message. For instance,
Maybe this applies also for the constructor, try:
my ($rc,$eXist) = XML::eXistDB::RPC->new( destination=>$eXist_db, repository=>$bank, user=>"admin", password=>"pass" );
now, if $rc != 0 there was an error.