1

In my Laravel Application when someone edits their Profile and waits till admin approval, they cannot edit the profile again.I need to implement this rule in FormRequest while user profile is edited.

My User model hasMany profile, but I am taking only the active profile,

public function profile()
{
    return $this->hasMany(Profile::class)->where('active', 1);
}

So when user edits profile I will insert into Profile table as active = 0, and update the flag in my User table "profile_review_pending = 1"

Now what I need is I need to define some rule in FormRequest like if profile_review_pending =1 then don't allow editing. Can this be done using exists or something like that?

4
  • Are you wanting an error to be returned or are you just wanting to prevent the user from updating their profile? Please could you also share your route definition and the code you have in your FormRequest so far. Commented Mar 13, 2020 at 6:42
  • try required_if:anotherfield,value,... The field under validation must be present and not empty if the anotherfield field is equal to any value. Commented Mar 13, 2020 at 6:46
  • @Rwd I want to prevent user from updating, its just a resource route. Also I havnt written anything in FormRequest. Now what I think is instead of handling this is in Rule, what I can do to prevent user from going to edit form. Commented Mar 13, 2020 at 6:59
  • 1
    That's exactly what I was going to suggest. As a quick solution you could use the authorize() method in the FormRequest to prevent the profile from being updated. A more robust way, that you can then use throughout your application, would be to make a policy (laravel.com/docs/7.x/authorization#creating-policies). Commented Mar 13, 2020 at 7:16

1 Answer 1

1

You need to define a custom validation rule, implement the Illuminate\Contracts\Validation\Rule interface or use a Closure. The custom rule is then used directly in a validator.

use Illuminate\Contracts\Validation\Rule;

class ReviewPendingValidationRule implements Rule
{
    public function passes($attribute, $value)
    {
        return $value == 1;
    }

    public function message()
    {
        return ':Review is pending';
    }
}

In your controller

public function store()
{
    $this->validate(request(), [
        'profile_review_pending' => [new ReviewPendingValidationRule]
    ]);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.