Validation
You will most likely want to apply validation rules to your crud configs. This should be done by setting the rules() on the config, usually within the configure() call.
public function configure()
{
$this->rules([
'data_name' => 'rules'
]);
}
Rules will work using any Laravel rules you have access to for your version of Laravel.
Differences to Laravel
The key for validation rules is non-standard. Since data is returned in a format matching the structure of Create and Edit forms, field data will be nested within sections. So, if you have a main section with a title field, your validation key would be main.title.
As such, you will need to manually define column names that Laravel might otherwise determine itself, for example within a 'unique' rule. You would typically do:
[
'title' => ['unique:users'],
]
This would validate that title is unique on the users table by taking the key as the implied column name. However, because of the nested data structure, laravel WILL try to validate against main.title as the column name, which won't exists. You will need to specify these:
[
'main.title' => ['unique:users,title'],
]
Special Cases
Unique Validation
Enso has special handling of unique rules. For store requests, the rules will be applied as specified. During update requests, an exclusion will be automatically applied for the model being updated.
This functionality can be change if required. You Config should have a property (this exists by default), defined in the HasValidationRules trait:
/**
* Name of the property that contains the validation rules
*
* @return string
*/
protected function getAutoUniqueRulesProperty(): string
{
return 'auto_unique_rules';
}
This property has three potential states:
- true. This is the defualt state. In this state, automatic model exclusions will be applied to ALL validation rules.
- false. In this state, automatic model exclusions will not be applied.
- an array. In this state, only the key names specified in the array will have automatic model exclusions applied. Note you will need the full nested keys here such as
main.title
See Yadda\Enso\Crud\Traits\HasValidationRules for more details.