如果大于0不在laravel中工作,则需要

时间:2017-04-15 11:36:39

标签: laravel-5

我正在使用laravel 5.1

如果另一个字段值大于0,我需要一个字段。

我试过这个

public class MainActivity extends Activity {

// global instances
private static  RadioGroup radio_g;
private static  RadioButton radio_b;
private static Button button_sbm;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    onClickListenerButton();
}

public void onClickListenerButton() {
    radio_g = (RadioGroup)findViewById(R.id.genderButton);
    button_sbm = (Button)findViewById(R.id.getBMI);

    button_sbm.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int selected_id = radio_g.getCheckedRadioButtonId();
                    radio_b = (RadioButton)findViewById(selected_id);
                    Toast.makeText(MainActivity.this,
                            radio_b.getText().toString(),Toast.LENGTH_SHORT ).show();
                }
            }
    );

 }

 }

如果项目字段值为== 1,但是如果项目值为== 2则该代码正常工作,那么它无效

请给我适当的解决方案

4 个答案:

答案 0 :(得分:1)

您可以使用以下内容:

$v = Validator::make($data, [
    'project' => 'required|integer',
]);

$v->sometimes('scope', 'required', function ($input) {
    return $input->project > 0;
});
  希望现在还为时不晚!我搜索了同样的问题然后我找到了这个解决方案

了解更多信息! check this link to Docs

答案 1 :(得分:0)

来自Laravel documentation

  

<强> required_if:anotherfield,值,...

     

验证字段必须存在,如果是,则不为空   anotherfield字段等于任何值。

因此,您无法使用>与您传递的值进行比较。在我看来,你有两个选择。您可以1)反转需求的逻辑,以防低于1的值非常有限(即数字可以是0或1,但绝不能低于零),或者您创建自定义验证规则。

以下是第一个选项的外观:

'scope' => 'required_unless:project,0,1',

这意味着:&#34;范围&#34;仅当项目不是0或1(或大于1)时才需要。同样,这只有在项目不能小于零或0到1之间的任何小数等时才有效。

答案 2 :(得分:0)

您可以像这样用gt运算符进行检查

'scope' => 'required_if:project,gt,1'

答案 3 :(得分:0)

更优雅的方式–将验证移至单独的文件。

1步骤:生成请求

php artisan make:request StoreScope

2步骤:编辑验证规则

文件 /app/Http/Requests/StoreScope.php

<?php
namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreScope extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'scope' => 'nullable|integer',
        ];
    }

    /**
     * Configure the validator instance.
     *
     * @param Illuminate\Validation\Validator $validator
     * @return void
     */
    public function withValidator($validator)
    {
        $validator->sometimes('scope', 'required', function($input) {
            return $input->project > 1;
        });
    }
}

3步骤:检查控制器中的验证

文件 /app/Http/Controllers/ScopeController.php

<?php
namespace App\Http\Controllers;

use App\Http\Requests\StoreOrder;

class ScopeController extends Controller
{

    /**
     * Store a newly created resource in storage.
     *
     * @param StoreOrder $request
     * @throws \Illuminate\Validation\ValidationException
     */
    public function storeAction(StoreOrder $request)
    {
        $data = $request->validated();

        print_r($data);
        die;
    }
}
?>