Form Level Validation in Zend Framework - php

I am using the Zend MVC framework along with an ORM layer generated with Propel, and I'm trying to figure out the best way to catch exceptions from a Propel object's save() function, and throw them to the Zend Form as errors.
Not all of the exceptions that come out of the Propel object have a way to identify which field caused the error, so I'm wondering if there is a way to add generic error messages to the entire form, rather than being forced to attach each error message to a particular form element.
For example, I have a save() call wrapped in a try/catch block, and I can add the exception->getMessage() to a form element's errors:
try {
$obj->save();
echo 'object saved successfully';
} catch (Exception $e) {
$form->name->addErrorMessage($e->getCode()." - ".$e->getMessage());
$form->name->markAsError();
$form->populate($formData);
}
but I would like to be able to do something like this:
try {
$obj->save();
echo 'object saved successfully';
} catch (Exception $e) {
$form->addErrorMessage($e->getCode()." - ".$e->getMessage());
$form->markAsError();
$form->populate($formData);
}
I hope that makes sense, thanks for the help,
Dave

Are you thinking about the errors from the database, or from the Propel validation layer (which isn't developed that much, and not used by default in the save() step)?
If you want to use the database errors, keep in mind that they will only return the first error (so the user has to submit four times if they entered three errors). Also, getting the field name out of the error message can be hard. Keep in mind that some keys cover multiple fields ("the combination of name and first_name must be unique").
This is why for example Symfony adds validation in the form layer. There, you can check all fields at once, and return multiple errors. But maybe you already do this, and only want this as a final check?

Related

How to display notice or success message during the import by Convert Adapter?

How to display notice or success message during the import by the Convert Adapter ?
I can only display error by
Mage::throwException($message)
.
Class responsible for this is Mage_Adminhtml_Block_System_Convert_Profile_Run.
Magento actually have some kind a session message stack. (Something quite similar to the message bags you can find, for example on Symphony).
Those are quite easy to use, just get the session associated to the area you are in and use on of the functions addError, addWarning, addNotice or addSuccess.
Since you are on the data flow module, the session you are looking to is dataflow/session. Take care to get this model via a singleton otherwise, you will end up with odd multiple sessions.
Mage::getSingleton('dataflow/session')->addSuccess($this->__('This will add a success message'));
The other are :
Mage::getSingleton('dataflow/session')->addNotice($this->__('This a notice'));
Mage::getSingleton('dataflow/session')->addWarning($this->__('That a warning'));
Mage::getSingleton('dataflow/session')->addError($this->__('And finally an error'));
And the reason you actually get an error message when you throw an exception, is because somewhere on Magento core, there is a
try {
// the code or class instantiation on which you throw your exception happens here
} catch (Mage_Core_Exception $e) {
Mage::getSingleton('dataflow/session')->addError($e->getMessage());
}

Laravel. Catch Exception thrown on App::make('foo')->bar()

I have a PriceController that updates the prices of my items. In my app, a Price is a set or PriceSegments (or Rules).
So my PriceController#update does:
foreach (Input::get('price_segment_id') as $price_segment_id)
{
try {
\App::make('BF\Controllers\PriceSegmentController')->update($price_segment_id);
} catch(BF\Exceptions\ValidationException $e) {
$errors[] = $e->get_errors();
}
}
And my PriceSegmentController#update does the update of each segment as follow:
$priceSegment = $this->repository->find($id);
if($priceSegment) {
// UPDATE $priceSegment with Input, etc.
$this->validator->validate($priceSegment->toArray());
$priceSegment->save();
}
If I do the try-catch in PriceSegmentController it works as expected, but I would like to do the try-catch un PriceController in order to collect all the error and being able to inform them all at once.
The problem is the Exception is not catched and I do not know why. I guess is something related with the behaviour of App::make('foo') but I did not figure it out.
Some help please?
Thanks
EDIT: The structure of my code is based on this example: http://www.sitepoint.com/data-validation-laravel-right-way/ and I am using my own Exceptions extending the Exception PHP class
I think the problem occurs because you are using the default validation mechanism in laravel as described here.
What you can do instead is to catch exceptions in PriceSegmentController and:
either wrap them with a custom exception type
or create a custom exception using exceptions you catch in that controller
and throw your custom exception further up in the food chain.
I hope it helps.

best way to handle errors in laravel

I am stuck a bit, I have a little form and auth system - Sentry.
1) Get data from Input and validate it.
It's okay, I get it, check with rules, set messageBag
<?php
$validation->getMessageBag()->setFormat(Config::get('project/config.errors_delimiters'));
?>
Then I return page withErrors method. And prints validation errors.
2) Next step will be to login users, for example. This mean, that I need to use a Sentry manual.
But now I have a problem: how to show errors or store errors.
I have:
Error messages via validation
Error messages via Sentry
Custom Errors
Success messages
Info messages
Which the best practice to store this messages? How to send it to a view? And how to parse it in a view? Also how to set delimiters style?
Of course I can create tons of flash, session, and other types of data and then send this into a view, but I think it is ridiculous. I think there is a simple method which can help me.
Very good question. This is how I handle it.
Because validation errors uses MessageBag, what I did is
$messages = new MessageBag;
and then if you run into any error, you could do
try {
...
} catch (Exception $e) {
$messages->add("error", $e->getMessage());
}
and if you have other errors returned you can merge it
$messages->merge($other_messages)
which includes validation messages $validation->messages().
Because messagebag accepts keys, you can add other type by $messages->add('info', 'ok')
And then in your template, or controller, you can get messages of that type by
$messages->get('error')
after you checked with
$messages->has('error')

how to handle exceptions/errors in php?

when using 3rd part libraries they tend to throw exceptions to the browser and hence kill the script.
eg. if im using doctrine and insert a duplicate record to the database it will throw an exception.
i wonder, what is best practice for handling these exceptions.
should i always do a try...catch?
but doesn't that mean that i will have try...catch all over the script and for every single function/class i use? Or is it just for debugging?
i don't quite get the picture.
Cause if a record already exists in a database, i want to tell the user "Record already exists".
And if i code a library or a function, should i always use "throw new Expcetion($message, $code)" when i want to create an error?
Please shed a light on how one should create/handle exceptions/errors.
Thanks
The only way to catch these exceptions is to use a try catch block. Or if you don't want the exception to occur in the first place you need to do your due diligence and check if the record already exists before you try to insert the record.
If it feels like you're using this all over the place then maybe you need to create a method that takes care of this for you (Dont Repeat Yourself).
I don't know Doctrine, but regarding your concrete usage, maybe there is a way to determine if you are facing a duplicate entry, something like :
try {
/* Doctrine code here */
} catch (DuplicateEntryException $e) {
/* The record already exists */
} catch (Exception $e) {
/* Unexpected error handling */
}
Or maybe you have to check if the Exception code equals 1062, which is the MySQL error code for duplicate entries.
Any code that may throw an exception should be in a try/catch block. It is difficult in PHP because you cannot know which method throws an Exception.
You should also have a big try/catch block in your main PHP file that avoids displaying the stack trace to the user, and that logs the cause. Maybe you can use set_exception_handler for this.

Should my method throw an exception?

My PHP web application is divided into modules, and I use the data model and data mapper patterns. My mapper methods are static, and they exist specifically to interact with the database.
I have the following method:
ModuleMapper::getRecordCountByModuleIdAndSiteId($moduleId, $siteId)
This method is only meant for a set list of modules (I have a good reason). Would it be reasonable to throw an exception if this method is called for a module outside of that set list (eg. "Record count for this module cannot be retrieved.").
try {
$recordCount = ModuleMapper::getRecordCountByModuleIdAndSiteId($moduleId, $siteId);
}
catch (Exception $e) {
// handle exception
}
Another option would be to just return 0.
Depends on how you want to handle errors really. I use zend framework which automatically catches exceptions and forwards them to the error controller for pretty display and logging. Obviously that method will kill processing unless you catch it explicitly. But it simplifies error display and logging.
You could just as well return 0 and do an if statement to display an inline message, or catch the error and display an inline message as well.

Categories