ValidationRules Last updated: 2022-10-01
You need to check the values entered during the process of storing or updating your database
We are aware of your intentions and are working to provide the best and fastest ways that suit you and reduce the consumption of time and effort
Well let's see that we have a field to store the name
text()->make('Name', 'name')
There are three methods that can be used during the data validation process
The methods are
text()->make('Name', 'name')->rule()
// or
text()->make('Name', 'name')->ruleWhenCreate()
// or
text()->make('Name', 'name')->ruleWhenUpdate()
rule()
It is generally applied during store or update
ruleWhenCreate()
It is generally applied during store
ruleWhenUpdate()
It is generally applied during update
We can set an example forcing the user to enter a value while checking the type of the value
text()->make('Name', 'name')->rule('required','string')
// or as array
text()->make('Name', 'name')->rule(['required','string'])
//The same method can be applied to other methods ruleWhenCreate() , ruleWhenUpdate()
can be used Illuminate\Validation\Rule
Object
Another example with email address and and with current ID from resource according to Model
email()->make('Email Address', 'email')
->ruleWhenUpdate('required',
'email',
// 'unique:users,email,'.$this->id,
Rule::unique('users')->ignore($this->id)
)->ruleWhenCreate('unique:users', 'email'),
See we have used the first two cases during store while making sure that the value is not repeated during store
The second is not to verify the same entered value and to search for repetition of the value in other data when Update
Another idea, the three methods can be used again
email()->make('Email Address', 'email')
->rule('required','email') // applied with store and update
->ruleWhenUpdate( // check for unique email when update
// 'unique:users,email,'.$this->id,
Rule::unique('users')->ignore($this->id)
)->ruleWhenCreate('unique:users', 'email'), // applied when store