在Laravel中,我有projects表.一个项目可以是另一个项目的子项目.我将此parent-child关系保留在同一projects表中.
In Laravel, i have projects table. A project can be sub-project of another project. I keep this parent-child relationship in the same projects table.
编辑项目时,用户从选择框中选择父项目.但是我想防止用户选择正在编辑的相同项目;作为父项目.这会导致循环,从而导致错误.
When editing a project, the user chooses parent project from a select box. However i want to prevent the user from selecting the same project that is being edited; as a parent project. This would cause loops, thus errors.
我该如何实现?我能想到的最佳解决方案是编写自定义验证规则,但是,在那种情况下,如何获取id属性以检查project_id字段?还是除了验证者以外,还有更好的解决方案吗?
How can i achieve this? Best solution i can think of is to write a custom validation rule, however, in that case, how can i get the id attribute to check against project_id field? Or are there better solutions other than validators?
我知道我只能在选择框中排除该项目,但这不是适当的解决方案,以防万一有人篡改过帐数据并修改project_id值(将其设置为与正在编辑的项目相同的值),则无限循环将发生.
I know i can just exclude that project in select box however it is not proper solution, in case someone tampers post data and modifies the project_id value, (setting it to same value of the project being edited), the infinite loop will happen.
更新: 这是我用于父项目输入生成的代码:
Update: Here is my the code for parent project input generation:
<div class="form-group"> {{ Form::label('project_id', 'Parent Project:') }} {{ Form::select('project_id', [null => null] + Project::lists('name', 'id'), null, ['class' => 'form-control']) }} </div>如上所述,在选择框中不包括当前项目ID"是不合适的解决方案.该应用程序仍然容易受到攻击.应该以更适当的方式防止它.
As i stated above, "not including current project id in the select box" is not a proper solution. The application is still vulnerable. It should be prevented in a more proper way.
推荐答案事实证明,该解决方案非常简单,就像Laravel的各个方面一样.
It comes out that the solution is pretty simple, just like in every aspect of Laravel.
足以包含
'project_id' => 'different:id',模型中的
验证规则. ( Ardent 语法)
此处都是具有Laravel 4解释的验证类型.
Here are all validation types with explanations as of Laravel 4.
更多推荐
Laravel父母与子女的关系,请勿将自己视为父母
发布评论