kalimatas / php-liquid

A PHP port of Ruby's Liquid Templates
http://www.delacap.com/artikel/Liquid-Templates/
MIT License
168 stars 57 forks source link

Error catching w/ Laravel Validation #117

Open rjhankison opened 5 years ago

rjhankison commented 5 years ago

Hi again,

Thanks very much for the help with the white space control. Our team really appreciates it. This package has been a huge hit for our customers.

Another question -- we're using a custom validation rule in our controllers to try to catch errors our customers may have added. Here's what we have:

     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value=null)
    {
        if ($value) {
            try {
                $template = new \Liquid\Template();
                $template->parse($value);
                return true;
            } catch (\Exception $e) {
                $this->e = $e;
                $this->attribute = $attribute;
                return false;
            } catch (\Liquid\Exception\WrongArgumentException $e) {
                $this->e = $e;
                $this->attribute = $attribute;
                return false;
            } catch (\Liquid\Exception\RenderException $e) {
                $this->e = $e;
                $this->attribute = $attribute;
                return false;
            } catch (\Liquid\Exception\ParseException $e) {
                $this->e = $e;
                $this->attribute = $attribute;
                return false;
            }
        }
    }

However, this doesn't seem to catch errors like poorly formatted date filters. For example, {{deal.invoice_created_at | date: %d.%m.%y}} passes, even though it's missing the quotes in the second parameter. Is there a way to catch these errors? This is less of an issue now because it's no longer throwing an exception on view if the filter is malformatted, but it'd be great if I could create a validation rule to catch those errors upfront.

Thanks again! Really appreciate your attentions. :)

Best, -Rich

rjhankison commented 5 years ago

Here's a bit of a better example:

If I format the string like this {{deal.invoice_created_at | date: "%d.%m.%y}}, it throws an error like this.. I'd love to catch these types of errors if possible.

Thanks!

sanmai commented 5 years ago

That's interesting find. I'll see what I can do but I don't think it'll happen this week. If you in a hurry, consider contributing to the project. I'm sure I'll find the time to review a PR.

rjhankison commented 5 years ago

Cool! I'll try to work that in this week. Any pointers on where to get started?

sanmai commented 5 years ago

First I'd added a test for the type of Liquid\Exception you think this should be. (ParseException seems like the best fit.) Just for the time being you can add it to the StandardFiltersTest.

Next I'd be looking around these lines:

https://github.com/kalimatas/php-liquid/blob/ce53ad8318dae9b5acab8b849659a240a4b98dca/src/Liquid/Variable.php#L59-L64

This appears to be where filter arguments are parsed out, but I may be wrong assuming the very this place needs some work.