Support the ongoing development of Laravel.io →
posted 9 years ago
IOC
Last updated 2 years ago.
0

Ok, so I enclosed my App::make() in var_dump() and got a response:

class Project\Frontend\Repo\Lead\EloquentLead#798 (1) {
  protected $lead =>
  class Lead#809 (21) {
    // bunch of info about the model etc relating to the interface and it's repo class
  }
}

One of the members in #laravel pointed out that this meant the interface was instantiated which is great but then I realised this isn't the interface that I'm having a problem with. So below is how I have things setup. The interface that's apparently not instantiated is ValidableInterface (last portion of code) and running var_dump(App::make()) for this interface returns the dreaded "Target not instantiated" error. Am I missing some glaring problem with my code?

My service provider

<?php namespace Project\Frontend\Service\Form;

use Illuminate\Support\ServiceProvider;
use Project\Frontend\Service\Form\Lead\LeadForm;
use Project\Frontend\Service\Form\Lead\LeadFormLaravelValidator;

class FormServiceProvider extends ServiceProvider {

    /**
     * Register the binding
     *
     * @return void
     */
    public function register()
    {
        $app = $this->app;

        $app->bind('Project\Frontend\Service\Form\Lead\LeadForm', function($app)
        {
            return new LeadForm(
                new LeadFormLaravelValidator( $app['validator'] ),
                $app->make('Project\Frontend\Repo\Lead\LeadInterface')
            );
        });
    }

}

My form class

<?php namespace Project\Frontend\Service\Form\Lead;

use Project\Backend\Service\Validation\ValidableInterface;
use Project\Frontend\Repo\Lead\LeadInterface;

class LeadForm {

    /**
     * Form Data
     *
     * @var array
     */
    protected $data;

    /**
     * Validator
     *
     * @var \Project\Backend\Service\Validation\ValidableInterface
     */
    protected $validator;

    /**
     * Lead repository
     *
     * @var \Project\Frontend\Repo\Lead\LeadInterface
     */
    protected $lead;

    public function __construct(ValidableInterface $validator, LeadInterface $lead)
    {
        $this->validator = $validator;
        $this->lead = $lead;
    }

My validation rules

<?php namespace Project\Frontend\Service\Form\Lead;

use Project\Backend\Service\Validation\AbstractLaravelValidator;

class LeadFormLaravelValidator extends AbstractLaravelValidator {

    /**
     * Validation rules
     *
     * @var Array
     */
    protected $rules = array(
        'name' 				=> 'required|regex:/^[a-zA-Z-\s]+$/',
        'email' 			=> 'email',
        'cell' 				=> 'required|numeric|digits_between:10,11',
    );

    /**
     * Validation messages
     *
     * @var Array
     */
    protected $messages = array(
		'regex' 				=> 'The :attribute may only contain letters, dashes and spaces.',
		'digits_between' 		=> 'The :attribute must be 10 numbers long.',
	);

}

My abstract validator

<?php namespace Project\Backend\Service\Validation;

use Illuminate\Validation\Factory;

abstract class AbstractLaravelValidator implements ValidableInterface {

    /**
     * Validator
     *
     * @var \Illuminate\Validation\Factory
     */
    protected $validator;

    /**
     * Validation data key => value array
     *
     * @var Array
     */
    protected $data = array();

    /**
     * Validation errors
     *
     * @var Array
     */
    protected $errors = array();

    /**
     * Validation rules
     *
     * @var Array
     */
    protected $rules = array();

    /**
     * Custom validation messages
     *
     * @var Array
     */
    protected $messages = array();

    public function __construct(Factory $validator)
    {
        $this->validator = $validator;
    }

    /**
     * Set data to validate
     *
     * @return \Project\Backend\Service\Validation\AbstractLaravelValidator
     */
    public function with(array $data)
    {
        $this->data = $data;

        return $this;
    }

    /**
     * Validation passes or fails
     *
     * @return Boolean
     */
    public function passes()
    {
        $validator = $this->validator->make($this->data, $this->rules, $this->messages);

        if( $validator->fails() )
        {
            $this->errors = $validator->messages();
            return false;
        }

        return true;
    }

    /**
     * Return errors, if any
     *
     * @return array
     */
    public function errors()
    {
        return $this->errors;
    }

}

My validator interface

<?php namespace Project\Backend\Service\Validation;

interface ValidableInterface {

    /**
     * Add data to validation against
     *
     * @param array
     * @return \Project\Backend\Service\Validation\ValidableInterface  $this
     */
    public function with(array $input);

    /**
     * Test if validation passes
     *
     * @return boolean
     */
    public function passes();

    /**
     * Retrieve validation errors
     *
     * @return array
     */
    public function errors();

}
Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

VenomRush venomrush Joined 27 May 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.