Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error catching w/ Laravel Validation #117

Open
rjhankison opened this issue Mar 4, 2019 · 4 comments
Open

Error catching w/ Laravel Validation #117

rjhankison opened this issue Mar 4, 2019 · 4 comments

Comments

@rjhankison
Copy link

rjhankison commented Mar 4, 2019

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
Copy link
Author

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
Copy link
Collaborator

sanmai commented Mar 5, 2019

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
Copy link
Author

rjhankison commented Mar 5, 2019 via email

@sanmai
Copy link
Collaborator

sanmai commented Mar 5, 2019

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:

if (preg_match('/\w+/', $filter, $matches)) {
$filterName = $matches[0];
$filterArgsRegex->matchAll($filter);
$matches = Liquid::arrayFlatten($filterArgsRegex->matches[1]);
$this->filters[] = array($filterName, $matches);
}

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants