5

I have a string column in my table e.g status which contains 2 string values as constants, i am wanting to add the ability so users can update a status only on these 2 string values. The main purpose of the question is am wanting to add validation for the status in my controller which the user can only select online or offline as options

so basically the status column can be these 2 string values

  protected $validation = [
        'status' => 'string:online, offline
    ];

I know the above doesnt work but i need something similar to validate multiple constant values for the string column.

these are the constants for the status that i need to be using

public const ONLINE = 'online';
public const OFFLINE = 'offline';

is there a way you can add in validation for the visibility field using these constants? some help would be great.

full class file below:

protected $request;

protected $user;


protected $validation = [
    'name' => 'max:10'
];

protected $required = [
    'name'
];

public function __construct(Request $request = null)
{
    $this->request = $request;
}
2

3 Answers 3

7

You can use like this in Request class:

public function rules()
{
    return [
        'status' => 'in:online,offline'
}
Sign up to request clarification or add additional context in comments.

Comments

6

You can use the constants to define the possible options:

use Illuminate\Validation\Rule;

$validation = [
        'status' => [Rule::in([Class::ONLINE,Class::OFFLINE])]
    ];

Or better, define the constant STATUSES listing all the possible statuses in your class

public const STATUS_ONLINE = 'online';
public const STATUS_OFFLINE = 'offline';
public const STATUSES = [self::ONLINE, self::OFFLINE];

and use that constant

use Illuminate\Validation\Rule;

$validation = [
        'status' => [Rule::in(Class::STATUSES)]
    ];

7 Comments

does this not define the status as an array? with [Class::ONLINE,Class::OFFLINE]
It does, having it in the Model class is more practical, for example when you add a new status, you just have to add it to the STATUSES constant, and no need to cycle through all the form validations you had in place
does that now mean that status will now expect an array instead of a string value this isnt what i want surely if im wanting the result to be a string?
@lilnoobcoder no, it means the status must be one of the values present in the array (online or offline) and no it doesnt define status as an array. misunderstood what you meant with your first question
when i add this rule to the validation array i get this error expression is not allowed as field default value and the whole array red lines
|
1

If you just need to check if the string received is one of those two, you could simply do that:

protected $validation = [
    'status' => Rule::in(['online', 'offline'])
];

Link to the documentation.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.