-1

I am making a website using Yii framework and XAMPP. It's a part of the school project so we need to make some basic stuff. I used phpmyadmin as a tool to create a database which I use for this site. I generated relations between its tables and made some forms for entering data into the database on the site (e.g. like professors of the university, courses, etc). Now I would like to implement the option of informing the user of an error when he enters lets say the same professor (so basically wants to make a new database table with same contents).

I don't have any clue as to how to do this do I make it part of the code of the website or do I have to set up something in phpmyadmin, so any sort of help is welcome.

2
  • You have already created the CRUD operations elements alias code for Create, Read, Update, Delete, index/list.. Model Controller and View? Y Which framework version you are using Yii1 or Yii2?
    – ScaisEdge
    Jan 31, 2016 at 14:44
  • yes I've created everything you mentioned and I am using Yii2. But when I create a new table entry using that Create class I'd like for it to report an error if the table with those same entries already exists Jan 31, 2016 at 16:52

1 Answer 1

1

You can use the validation rules in model

In this case you need a unique validator (see this doc)

public function rules()
{
    return [
        .....
        ['your_field', 'unique'],
        ... 
        ....
    ];
}
5
  • Thanks ... Now you can also rate the answer as useful good work
    – ScaisEdge
    Jan 31, 2016 at 17:19
  • is there a way I could make these validators work the following way, for example I want to schedule some classes on monday from 8 to 9, now as you suggested I can check if 8 is unique, but in this case I need to check if it is unique on this day. This table has all those propreties "from_time", "to_time", "day", etc. ? Jan 31, 2016 at 17:28
  • 1
    Seems this is possibile look at the various (brief ) sample in doc in ansewer .. seems is possibile use a set of field [['a1', 'a2'], 'unique', 'targetAttribute' => ['a1', 'a2']]
    – ScaisEdge
    Jan 31, 2016 at 17:36
  • 1
    Just two suggestion .. for avoiding downvote to your next question . post some code of what you are trying to do .. (question without code frequently are downvoted).. if you use Yii2 add tag yii2 too..
    – ScaisEdge
    Jan 31, 2016 at 17:39
  • yeah I see that now thanks, I just didn't post any because I didn't know if it was code related at all or it had to be configured in the actual database, but I get what are you saying Jan 31, 2016 at 19:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.