The following rules are used to validate an item code before it can be persisted to the database.
- code must have five digits
- must start with 5
- must be unique within the codes table
- The first three characters of a code must be the same as the first three characters of the parent code
For validation I am using the following form request rules
class StoreItemCodeRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
Validator::extend('in_category', function ($attribute, $value, $parameters, $validator){
$category = $this->get('parent_code');
return substr($value, 0, 3) === substr($category, 0, 3);
});
return [
'code' => 'required|http://regex:/^5(\d{4})$/|in_category|unique:exp_codes,code,' . $this->get('id'),
'name' => 'required|max:50|unique:exp_codes,name,' . $this->get('id'),
'parent_code' => 'exists:exp_categories,code'
];
}
}
Inserting a new code works without problems.
However, trying to update an existing code displays The code format is invalid. error against the code input field.
I am using Laravel 5.3 and would appreciate if someone can point the cause of the error that is preventing updating.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire