Skip to content

Commit

Permalink
New way to add validation rules to form elements and combine it with …
Browse files Browse the repository at this point in the history
…old method
  • Loading branch information
sleeping-owl committed Oct 20, 2014
1 parent 7fdd6f6 commit 1dfe0fb
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/SleepingOwl/Admin/Models/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,14 @@ public function addItem($item)
$this->items[] = $item;
}

public function getValidationRules()
{
$rules = [];
foreach ($this->items as $item)
{
$rules[$item->getName()] = $item->getValidationRules();
}
return $rules;
}

}
58 changes: 58 additions & 0 deletions src/SleepingOwl/Admin/Models/Form/FormItem/BaseFormItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,23 @@ abstract class BaseFormItem implements FormItemInterface
* @var Form
*/
protected $form;
/**
* @var string
*/
protected $name;
/**
* @var string
*/
protected $label;
/**
* @var array
*/
protected $validation = [];

/**
* @param null $name
* @param null $label
*/
function __construct($name = null, $label = null)
{
$this->formBuilder = Admin::instance()->formBuilder;
Expand All @@ -30,9 +44,53 @@ function __construct($name = null, $label = null)
}
}

/**
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* @return mixed
*/
protected function getValueFromForm()
{
return $this->form->getValueForName($this->name);
}

public function required($onlyOnCreate = false)
{
if ($onlyOnCreate)
{
$this->validationRule('required_only_on_create');
} else
{
$this->validationRule('required');
}
return $this;
}

public function unique()
{
$table = ModelItem::$current->getModelTable();
return $this->validationRule('unique:' . $table . ',' . $this->name);
}

public function validationRule($rule)
{
$rules = explode('|', $rule);
foreach ($rules as $rule)
{
$this->validation[] = $rule;
}
return $this;
}

public function getValidationRules()
{
return $this->validation;
}

}
7 changes: 7 additions & 0 deletions src/SleepingOwl/Admin/Models/Form/FormItem/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,11 @@ public function render()
return $this->formBuilder->imageGroup($this->name, $this->label, $this->form->getInstance());
}

public function getValidationRules()
{
$rules = parent::getValidationRules();
$rules[] = 'image';
return $rules;
}

}
10 changes: 10 additions & 0 deletions src/SleepingOwl/Admin/Models/Form/Interfaces/FormItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,14 @@ interface FormItemInterface
*/
public function render();

/**
* @return string
*/
public function getName();

/**
* @return array|null
*/
public function getValidationRules();

}
7 changes: 7 additions & 0 deletions src/SleepingOwl/Admin/Models/ModelItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ public function getModelClass()
return $this->modelClass;
}

public function getModelTable()
{
$instance = new $this->modelClass;
return $instance->getTable();
}

/**
* @return array
*/
Expand Down Expand Up @@ -356,4 +362,5 @@ public function __call($method, $param)
}
throw new MethodNotFoundException(get_class($this), $method);
}

}
3 changes: 2 additions & 1 deletion src/SleepingOwl/Admin/Repositories/ModelRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public function update($id)
*/
protected function save()
{
$this->instance->validate($data = $this->request->all());
$rules = $this->modelItem->getForm()->getValidationRules();
$this->instance->validate($data = $this->request->all(), $rules);
foreach ($data as &$value)
{
if ( ! is_string($value)) continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ interface ValidationModelInterface
{
/**
* @param $data
* @param array $rules
* @throws \SleepingOwl\Admin\Exceptions\ValidationException
* @return void
*/
public function validate($data);
public function validate($data, $rules = []);

/**
* @return array
Expand Down
30 changes: 27 additions & 3 deletions src/SleepingOwl/Models/Traits/ValidationModelTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,41 @@ trait ValidationModelTrait
{
/**
* @param $data
* @return bool
* @param array $rules
* @throws ValidationException
* @return bool
*/
public function validate($data)
public function validate($data, $rules = [])
{
$validator = Validator::make($data, $this->getValidationRules(), Lang::get('admin::validation'), ['id' => $this->id]);
$rules = $this->mergeValidationRules($rules);
$validator = Validator::make($data, $rules, Lang::get('admin::validation'), ['id' => $this->id]);

if ($validator->fails())
{
throw new ValidationException($validator->errors());
}
return true;
}

/**
* @param $rules
* @return array
*/
protected function mergeValidationRules($rules)
{
foreach ($this->getValidationRules() as $field => $rule)
{
if (!is_array($rule))
{
$rule = explode('|', $rule);
}
$rules[$field] = array_merge($rules[$field], $rule);
}
return $rules;
}

public function getValidationRules()
{
return [];
}
}

0 comments on commit 1dfe0fb

Please sign in to comment.